Skip to content

Instantly share code, notes, and snippets.

@pyRobShrk
Last active June 15, 2023 09:36
Show Gist options
  • Save pyRobShrk/d0fecbe74b1eab3d8477e5a1313fb87b to your computer and use it in GitHub Desktop.
Save pyRobShrk/d0fecbe74b1eab3d8477e5a1313fb87b to your computer and use it in GitHub Desktop.
A random assortment of LAMBDA functions for Excel
/*
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,29.5275)/3.691,0)-2,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: 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