map, filter, reduce in Julia
The functional trio, plus transforms built into the folds.
exposures = [120, 300, 90, 240, 60]
# map transforms, filter keeps, reduce folds.
minutes = map(s -> s / 60, exposures)
println(minutes)
long_subs = filter(s -> s >= 120, exposures)
println("Long subs: $long_subs")
total = reduce(+, exposures)
println("Total: $total s")
# sum, maximum and friends take a transform as first argument.
println("In minutes: ", sum(s -> s / 60, exposures))
println("Longest: ", maximum(exposures), " s")
How it works
mapandfiltertake the function first.reduce(+, xs)folds with any binary operation.sum(f, xs)andmaximum(xs)fold and transform in one call.
The run, in numbers
- Lines
- 15
- Characters to type
- 437
- Tokens
- 98
- Three-star pace
- 70 tpm
At the three-star pace of 70 tokens a minute, this run takes about 84 seconds.
Step 2 of 3 in Broadcasting, step 14 of 27 in Language basics.