Catamorphism: Difference between revisions

→‎{{header|Excel}}: Added an Excel LAMBDA version
(Add APL)
(→‎{{header|Excel}}: Added an Excel LAMBDA version)
Line 634:
{55,3628800,"12345678910"}
</pre>
 
=={{header|Excel}}==
===LAMBDA===
 
Excel provides a good number of standard catamorphisms like SUM, PRODUCT, LEN etc out of the box, but in recent builds of Excel we can write more general catamorphisms as LAMBDA expressions, and bind names to them in the WorkBook Name Manager.
 
Excel's compound data type is a non-empty array, for which we could write, for example, specialised column or row instances of fold, whether rightward or leftward.
 
Here is an example of binding the name FOLDLROW to a left fold over a row of Excel cells.
 
As an example of a binary operator to fold, with an accumulator, over a series of character values, we can define a custom:
 
'''updateBracketDepth(accumulator)(character)''' which:
 
# Increments the nesting depth given a "[" character
# reduces it given a "]" character
# leaves the nesting depth unchanged for any other character
# updates the accumulator no further if the nesting depth ever becomes negative.
 
 
(See [https://www.microsoft.com/en-us/research/blog/lambda-the-ultimatae-excel-worksheet-function/ LAMBDA: The ultimate Excel worksheet function])
 
{{Works with|Office 365 betas 2021}}
<lang lisp>FOLDROW
=LAMBDA(op,
LAMBDA(a,
LAMBDA(xs,
LET(
b, op(a)(HEADROW(xs)),
 
IF(1 < COLUMNS(xs),
FOLDROW(op)(b)(
TAILROW(xs)
),
b
)
)
)
)
)
 
 
updatedBracketDepth
=LAMBDA(depth,
LAMBDA(c,
IF(0 <= depth,
IF("[" = c,
1 + depth,
IF("]" = c,
depth - 1,
depth
)
),
depth
)
)
)
 
 
HEADROW
=LAMBDA(xs,
LET(REM, "The first item of each row in xs",
 
INDEX(
xs,
SEQUENCE(ROWS(xs)),
SEQUENCE(1, 1)
)
)
)
 
 
TAILROW
=LAMBDA(xs,
LET(REM,"The tail of each row in the grid",
n, COLUMNS(xs) - 1,
 
IF(0 < n,
INDEX(
xs,
SEQUENCE(ROWS(xs), 1, 1, 1),
SEQUENCE(1, n, 2, 1)
),
NA()
)
)
)
 
 
CHARSROW
=LAMBDA(s,
MID(s,
SEQUENCE(1, LEN(s), 1, 1),
1
)
)</lang>
 
{{Out}}
{| class="wikitable"
|-
|||style="text-align:right; font-family:serif; font-style:italic; font-size:120%;"|fx
! colspan="3" style="text-align:left; vertical-align: bottom; font-family:Arial, Helvetica, sans-serif !important;"|=FOLDROW( updatedBracketDepth )( 0 )( CHARSROW(C2) )
|- style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff;"
|
| A
| B
| C
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 1
|
| style="font-weight:bold" | Final bracket nesting depth
| style="font-weight:bold" | Sample string
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 2
|
| style="text-align:center; background-color:#cbcefb" | 0
| [simply bracketed]
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 3
|
| style="text-align:center" | 1
| [[ ]
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 4
|
| style="text-align:center" | -1
| [ ]]
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 5
|
| style="text-align:center" | 0
| [[[ [] ]]]
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 6
|
| style="text-align:center" | 0
| [ [[[ [] ]]] [[[ ]]] [[[ [] ]]] ]
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 7
|
| style="text-align:center" | 1
| [ [[[ [ ]]] [[[ ]]] [[[ [] ]]] ]
|}
 
=={{header|F_Sharp|F#}}==
9,655

edits