/ fundamentals / reduce_keyword.nu
reduce_keyword.nu
1 # The reduce keyword computes a single value from the list. 2 # It uses a block which takes 2 values, the element and accumulator. 3 4 5 let scores = [3 8 4] 6 7 print $"total = ($scores | reduce {|el, acc| $acc + $el})" 8 9 print "\n" 10 11 print "Same result as above with an easier approach" 12 13 print $"($scores | math sum)" 14 15 print "\n" 16 17 print $"product = ($scores | reduce --fold 1 {|el, acc| $acc * $el })" #--fold is a flag that defines initial value for accumulator 18