list_reduce.gravity 412 B

123456789101112131415161718192021222324
  1. #unittest {
  2. name: "List reduce.";
  3. result: true;
  4. };
  5. func main() {
  6. var list = [0, 1, 2, 3, 4, 5, 6]
  7. func x(a, b) {
  8. return a - b
  9. }
  10. var reduced = list.reduce(-1, x) //-22
  11. if (reduced != -22) {
  12. return false
  13. }
  14. var list2 = ["this","is","a","test"]
  15. func y(a, b) {
  16. return a + b
  17. }
  18. reduced = list2.reduce("", y)
  19. if (reduced != "thisisatest") {
  20. return false
  21. }
  22. return true
  23. }