Last active
November 13, 2024 21:59
-
-
Save pyRobShrk/d0fecbe74b1eab3d8477e5a1313fb87b to your computer and use it in GitHub Desktop.
A random assortment of LAMBDA functions for Excel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Name: Show Moon Phase Emoji (MOONPHASE) | |
Description: Returns a lunar phase character closest matching to any Excel Date/Time value. | |
If you calculate for daily values at midnight, the lunar cycle will be the same for every 3 or 4 days (3.691 days). | |
ππππππππ | |
*/ | |
MOONPHASE = LAMBDA(datetime,LET( | |
phase,MOD(ROUND(MOD(datetime-1.5,29.53059)/3.69125,0),8)+1, | |
UNICHAR(127760+phase))); | |
/* | |
Name: Show Clock Emoji (SHOWCLOCK) | |
Description: Returns a unicode clock character, to the nearest half hour, of any Excel Date/Time value | |
ππ§ππππππππππ ππ‘ππ’ππ£ππ€ππ₯ππ¦ | |
*/ | |
SHOWCLOCK = LAMBDA(datetime,LET( | |
bump,MOD(SEQUENCE(12,,-1),12)+1, | |
clockseries,UNICHAR(TOCOL(CHOOSEROWS((WRAPCOLS(SEQUENCE(24,,HEX2DEC("1f550")),12)),bump))), | |
INDEX(clockseries,MOD(ROUND(datetime*48,0),24)+1))); | |
/* | |
Name: CALENDAR (CALENDAR) | |
Description: Returns a calendar for the full month of the input Excel Date/Time value, with the first column as Sunday | |
*/ | |
CALENDAR = LAMBDA(datetime,LET( | |
eomon,EOMONTH(datetime,0),eomonday,WEEKDAY(eomon),daysin,DAY(eomon),oneday,MOD(eomonday-daysin,7)+1, | |
cal,WRAPROWS(SEQUENCE(daysin+oneday-1,,-oneday+2),7),IFERROR(IF(cal>0,cal,""),""))) | |
/* | |
Name: Linear Interpolation (INTERP) | |
Description: Performs linear interpolation by lookup, supporting ascending or descending lookup in columns (not rows) | |
*/ | |
INTERP = LAMBDA(x,xLookup,yRange,LET( | |
isAsc,INDEX(xLookup,2)>INDEX(xLookup,1), | |
i,IF(isAsc,MATCH(x,xLookup),MATCH(x,xLookup,-1))-1, | |
TREND(OFFSET(yRange,i,0,2),OFFSET(xLookup,i,0,2),x)); | |
/* | |
Name: Multiply Polynomial (POLYNOMIAL) | |
Description: Given polynomail coefficients, and an X value, this function multiplies out the polynomial. | |
The coefficients are in decreasing order, and must include a constant, just as the output to LINEST. | |
*/ | |
POLYNOMIAL = LAMBDA(x,coeffs, | |
LET(l,COUNT(coeffs), | |
SUM(x^SEQUENCE(,l,l-1,-1)*coeffs))); | |
/* | |
Name: Conway's Game of Life (GAMEOFLIFE) | |
Description: | |
https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life | |
https://buttondown.email/hillelwayne/archive/excel-is-pretty-dang-cool/ | |
This could be implemented as one big ugly, or two tidy LAMBDA functions. | |
Using 1 and 0 rather than Excel's TRUE/FALSE cell values. | |
First, let's see the logic for a single cell: | |
Function Name: gol_cell | |
=LAMBDA(c,LET(x, SUM(OFFSET(c,-1,-1,3,3)),1*OR(x=3,AND(c=1,x=4)))) | |
Not too bad, but now it needs to be applied to a whole generation. | |
Cells can become alive or dead beyond the size of the input so it needs to be expanded. | |
Note the following LAMBDA is calling the previous LAMBDA through the MAP function. | |
Function Name: game_of_life | |
=LAMBDA(rng,LET(r,ROWS(rng),c,COLUMNS(rng),expand,OFFSET(rng,-1,-1,r+2,c+2),MAP(expand,gol_cell))) | |
and... combined into one big double LAMBDA (with indentation): | |
*/ | |
GAMEOFLIFE = LAMBDA(rng, | |
LET(r,ROWS(rng),c,COLUMNS(rng), | |
expand,OFFSET(rng,-1,-1,r+2,c+2), | |
MAP(expand, | |
LAMBDA(cl,LET( | |
x,SUM(OFFSET(cl,-1,-1,3,3)), | |
1*OR(x=3,AND(cl=1,x=4))))))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment