Reduce is a function or method that is used to take the values in an array or a list and apply a function to successive members of the list to produce (or reduce them to), a single value.



Task

Show how reduce (or foldl or foldr etc), work (or would be implemented) in your language.



See also

 Wikipedia article:   Fold
 Wikipedia article:   Catamorphism


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
        )
    )
)
 
 
bracketCount
=LAMBDA(a,
    LAMBDA(c,
        IF(ISNUMBER(FIND(c, "[]", 1)),
            1 + a,
            a
        )
    )
)
 
 
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
    )
)