tomo-koans/lesson-templates/lesson-16-reducers.tm

32 lines
621 B
Plaintext
Raw Normal View History

2025-03-24 19:29:28 -07:00
# Reductions
func main():
# Reductions fold collections into a single value:
>> (+: [1, 2, 3])!
= ???
>> (*: [2, 3, 4])!
= ???
# If an empty argument is given, a `none` value is returned
empty := [:Int]
>> (+: empty)
= none
# Use `or` to provide a fallback:
>> (+: empty) or 100
= ???
# `_min_` and `_max_` work as reducers:
>> (_max_: [10, 30, 20])!
= ???
>> (_min_.length: ["x", "abcd", "yz"])!
= "???"
# Comprehensions inside reductions:
>> (+: i * 2 for i in [1, 2, 3])!
= ???
>> (+: i for i in 10 if i:is_prime())!
2025-03-25 11:17:14 -07:00
= ???