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


HAI 1.3
 
HOW IZ I reducin YR array AN YR size AN YR fn
    I HAS A val ITZ array'Z SRS 0
    IM IN YR LOOP UPPIN YR i TIL BOTH SAEM i AN DIFF OF size AN 1
        val R I IZ fn YR val AN YR array'Z SRS SUM OF i AN 1 MKAY
    IM OUTTA YR LOOP
    FOUND YR val
IF U SAY SO
 
O HAI IM array
    I HAS A SRS 0 ITZ 1
    I HAS A SRS 1 ITZ 2
    I HAS A SRS 2 ITZ 3
    I HAS A SRS 3 ITZ 4
    I HAS A SRS 4 ITZ 5
KTHX
 
HOW IZ I add YR a AN YR b, FOUND YR     SUM OF a AN b, IF U SAY SO
HOW IZ I sub YR a AN YR b, FOUND YR    DIFF OF a AN b, IF U SAY SO
HOW IZ I mul YR a AN YR b, FOUND YR PRODUKT OF a AN b, IF U SAY SO
 
VISIBLE I IZ reducin YR array AN YR 5 AN YR add MKAY
VISIBLE I IZ reducin YR array AN YR 5 AN YR sub MKAY
VISIBLE I IZ reducin YR array AN YR 5 AN YR mul MKAY
 
KTHXBYE