#!/usr/bin/env p2sh fn mapfn(arr, f) { let iter = fn(arr, accumulated) { if len(arr) == 0 { accumulated; } else { push(accumulated, f(first(arr))); iter(rest(arr), accumulated); } }; iter(arr, []); } fn reduce(arr, initial, f) { let iter = fn(arr, result) { if len(arr) == 0 { result; } else { iter(rest(arr), f(result, first(arr))); } }; iter(arr, initial); } fn sum(arr) { reduce(arr, 0, fn(initial, el) { initial + el }); } let double = fn(x) { x * 2; }; let arr = [1, 2, 3, 4, 5]; let arr_double = mapfn(arr, double); let total = sum(arr_double); println("Sum = {}", total);