| 1 | const __provide__ = :| any all sum repeat |
|
| 2 |
|
| 3 | func any(list) {
|
| 4 | ### Returns true if any value in the list is truthy.
|
| 5 | # Empty list: returns false
|
| 6 |
|
| 7 | for item in (list) {
|
| 8 | if (item) {
|
| 9 | return (true)
|
| 10 | }
|
| 11 | }
|
| 12 | return (false)
|
| 13 | }
|
| 14 |
|
| 15 | func all(list) {
|
| 16 | ### Returns true if all values in the list are truthy.
|
| 17 | # Empty list: returns true
|
| 18 |
|
| 19 | for item in (list) {
|
| 20 | if (not item) {
|
| 21 | return (false)
|
| 22 | }
|
| 23 | }
|
| 24 | return (true)
|
| 25 | }
|
| 26 |
|
| 27 | func sum(list; start=0) {
|
| 28 | ### Returns the sum of all elements in the list.
|
| 29 | # Empty list: returns 0
|
| 30 |
|
| 31 | var sum = start
|
| 32 | for item in (list) {
|
| 33 | setvar sum += item
|
| 34 | }
|
| 35 | return (sum)
|
| 36 | }
|
| 37 |
|
| 38 | func repeat(x, n) {
|
| 39 | ### Returns a list with the given string or list repeated
|
| 40 |
|
| 41 | # Like Python's 'foo'*3 or ['foo', 'bar']*3
|
| 42 | # negative numbers are like 0 in Python
|
| 43 |
|
| 44 | var t = type(x)
|
| 45 | case (t) {
|
| 46 | Str {
|
| 47 | var parts = []
|
| 48 | for i in (0 ..< n) {
|
| 49 | call parts->append(x)
|
| 50 | }
|
| 51 | return (join(parts))
|
| 52 | }
|
| 53 | List {
|
| 54 | var result = []
|
| 55 | for i in (0 ..< n) {
|
| 56 | call result->extend(x)
|
| 57 | }
|
| 58 | return (result)
|
| 59 | }
|
| 60 | (else) {
|
| 61 | error "Expected Str or List, got $t"
|
| 62 | }
|
| 63 | }
|
| 64 | }
|