# AnyDice: Nested highest/lowest function

I understand how to keep highest or lowest die every time:

\ Keep highest \
output 1@2d6 + 1d6
output [highest 1 of 2d6] + 1d6

\ Keep lowest \
output 2@2d6 + 1d6
output [lowest 1 of 2d6] + 1d6


but how can I take lowest then highest?

Something incorrect happens under the hood when I perform either of the following:

\ Keep lowest then highest \
output 1@(2@2d6 + 1d6) + 1d6
output [highest 1 of ([lowest 1 of 2d6] + 1d6)]


You can't merge two separate dice into one pool in AnyDice. However, the built-in [highest of NUMBER and NUMBER] function will work just fine on two dice:

\ Keep lowest then highest \
output [highest of [lowest 1 of 2d6] and d6]
output [highest of 2@2d6 and d6]


Ps. If you happened to have more than two separate dice that you wanted to take the highest of, you could either nest the functions, e.g. like this:

output [highest of [highest of d8 and d6] and d4]


or write your own function, e.g. like in one of these earlier answers of mine. The last one of those gives a particularly general solution, which works just like the AnyDice @ operator except that it can take multiple pools of dice as parameters:

function: P:s at A:s B:s C:s {
result: P@[sort {A, B, C}]
}

output [1 at 3d4 2d6 1d8] named "highest"
output [6 at 3d4 2d6 1d8] named "lowest"
output [{1,6} at 3d4 2d6 1d8] named "highest + lowest"


(If you need more than three different dice pools, just add more parameters to the function. If you want to use the same function for fewer pools than the number of parameters it takes, you can use {} as a placeholder to represent a pool of zero dice.)