Introducing LAMBDIFY: the function that writes functions

Introducing LAMBDIFY: the function that writes functions

2.295 Lượt nghe
Introducing LAMBDIFY: the function that writes functions
This is a walkthrough of the Snap case from the South African Excel Championships. It's also a chance to introduce you to one of the craziest LAMBDAs I've ever written - a function for writing other functions! Sections 00:00 Introduction & L1 08:10 Level 2 15:01 Level 3 20:00 Level 4 22:22 Level 5 28:32 Bonuses And as promised, here's a function to solve Level 6. It's big, but the idea is simple: * we use a 3x1 array that is each player's cards as a variable (first the player to play next, then the other player, then the cards in the 'pot') * init is the initial variable (each player with their starting hand), and oneStep is a LAMBDA that takes a stack of these variables and adds the next step below (with a row of 0s if the process errors out, meaning the player can't play) * re applies this 200 times (a recursive function that keeps going as long as needed would be more elegant, but also slower to write!) * and then we just find the turn where it breaks =LET(cards,G231, playOrd,SORTBY(WRAPROWS(TEXTSPLIT(cards,"; "),2),-SEQUENCE(26)), init,HSTACK(BYCOL(TEXTBEFORE(playOrd,suits),ARRAYTOTEXT),""), oneStep,LAMBDA(prev,[v],LET( p1_st,INDEX(TAKE(prev,-1),1), p2_st,INDEX(TAKE(prev,-1),2), pot_st,INDEX(TAKE(prev,-1),3), nxtCard,TEXTBEFORE(p1_st,", "), snap,RIGHT(pot_st,LEN(nxtCard))=nxtCard, fnPot, TEXTJOIN(", ",,pot_st,nxtCard), p2_fn,TEXTJOIN(", ",,TEXTAFTER(p1_st,", ",,,,""),IF(snap,fnPot,"")), VSTACK(prev, IFERROR(HSTACK(p2_st,p2_fn,IF(snap,"",fnPot)),{0,0,0})))), re,REDUCE(init,SEQUENCE(200),oneStep), turn,INT(SEQUENCE(201)/2), XLOOKUP(0,TAKE(re,,1),turn))