diff --git a/builtin/core.wren b/builtin/core.wren index a3722dd9..2ee9951d 100644 --- a/builtin/core.wren +++ b/builtin/core.wren @@ -1,9 +1,25 @@ class Sequence { + all(f) { + var result = true + for (element in this) { + result = f.call(element) + if (!result) return result + } + return result + } + + any(f) { + var result = false + for (element in this) { + result = f.call(element) + if (result) return result + } + return result + } + contains(element) { for (item in this) { - if (element == item) { - return true - } + if (element == item) return true } return false } @@ -19,9 +35,7 @@ class Sequence { count(f) { var result = 0 for (element in this) { - if (f.call(element)) { - result = result + 1 - } + if (f.call(element)) result = result + 1 } return result } @@ -42,20 +56,6 @@ class Sequence { return result } - all(f) { - for (element in this) { - if (!f.call(element)) return false - } - return true - } - - any(f) { - for (element in this) { - if (f.call(element)) return true - } - return false - } - reduce(acc, f) { for (element in this) { acc = f.call(acc, element) diff --git a/doc/site/core/sequence.markdown b/doc/site/core/sequence.markdown index 807e6c84..a7d10022 100644 --- a/doc/site/core/sequence.markdown +++ b/doc/site/core/sequence.markdown @@ -13,8 +13,8 @@ core [iterator protocol][] can extend this to get a number of helpful methods. Tests whether all the elements in the sequence pass the `predicate`. Iterates over the sequence, passing each element to the function `predicate`. -If its return value evaluates to `false`, stops iterating and returns `false`. -Otherwise, returns `true`. +If it returns something [false](../control-flow.html#truth), stops iterating +and returns the value. Otherwise, returns `true`. :::dart [1, 2, 3].all {|n| n > 2} // False. @@ -25,8 +25,8 @@ Otherwise, returns `true`. Tests whether any element in the sequence passes the `predicate`. Iterates over the sequence, passing each element to the function `predicate`. -If its return value evaluates to `true`, stops iterating and returns `true`. -Otherwise, returns `false`. +If it returns something [true](../control-flow.html#truth), stops iterating and +returns that value. Otherwise, returns `false`. :::dart [1, 2, 3].any {|n| n < 1} // False. diff --git a/src/vm/wren_core.c b/src/vm/wren_core.c index ea75e896..a0f96caa 100644 --- a/src/vm/wren_core.c +++ b/src/vm/wren_core.c @@ -45,11 +45,27 @@ // This string literal is generated automatically from core. Do not edit. static const char* libSource = "class Sequence {\n" +" all(f) {\n" +" var result = true\n" +" for (element in this) {\n" +" result = f.call(element)\n" +" if (!result) return result\n" +" }\n" +" return result\n" +" }\n" +"\n" +" any(f) {\n" +" var result = false\n" +" for (element in this) {\n" +" result = f.call(element)\n" +" if (result) return result\n" +" }\n" +" return result\n" +" }\n" +"\n" " contains(element) {\n" " for (item in this) {\n" -" if (element == item) {\n" -" return true\n" -" }\n" +" if (element == item) return true\n" " }\n" " return false\n" " }\n" @@ -65,9 +81,7 @@ static const char* libSource = " count(f) {\n" " var result = 0\n" " for (element in this) {\n" -" if (f.call(element)) {\n" -" result = result + 1\n" -" }\n" +" if (f.call(element)) result = result + 1\n" " }\n" " return result\n" " }\n" @@ -88,20 +102,6 @@ static const char* libSource = " return result\n" " }\n" "\n" -" all(f) {\n" -" for (element in this) {\n" -" if (!f.call(element)) return false\n" -" }\n" -" return true\n" -" }\n" -"\n" -" any(f) {\n" -" for (element in this) {\n" -" if (f.call(element)) return true\n" -" }\n" -" return false\n" -" }\n" -"\n" " reduce(acc, f) {\n" " for (element in this) {\n" " acc = f.call(acc, element)\n" diff --git a/test/core/list/all.wren b/test/core/list/all.wren deleted file mode 100644 index c3ec24e5..00000000 --- a/test/core/list/all.wren +++ /dev/null @@ -1,9 +0,0 @@ -var a = [1, 2, 3] -var b = a.all {|x| x > 1 } -IO.print(b) // expect: false - -var d = a.all {|x| x > 0 } -IO.print(d) // expect: true - -var e = [].all {|x| false } -IO.print(e) // expect: true diff --git a/test/core/list/all_non_bool_returning_fn.wren b/test/core/list/all_non_bool_returning_fn.wren deleted file mode 100644 index ca7e46ed..00000000 --- a/test/core/list/all_non_bool_returning_fn.wren +++ /dev/null @@ -1 +0,0 @@ -IO.print([1, 2, 3].all {|x| "truthy" }) // expect: true diff --git a/test/core/list/any.wren b/test/core/list/any.wren deleted file mode 100644 index 79c1cfca..00000000 --- a/test/core/list/any.wren +++ /dev/null @@ -1,10 +0,0 @@ -var a = [1, 2, 3] - -var b = a.any {|x| x > 3 } -IO.print(b) // expect: false - -var d = a.any {|x| x > 1 } -IO.print(d) // expect: true - -var e = [].any {|x| true } -IO.print(e) // expect: false diff --git a/test/core/list/any_non_bool_returning_fn.wren b/test/core/list/any_non_bool_returning_fn.wren deleted file mode 100644 index cdd2b575..00000000 --- a/test/core/list/any_non_bool_returning_fn.wren +++ /dev/null @@ -1 +0,0 @@ -IO.print([1, 2, 3].any {|x| "truthy" }) // expect: true diff --git a/test/core/sequence/all.wren b/test/core/sequence/all.wren new file mode 100644 index 00000000..5b26f05a --- /dev/null +++ b/test/core/sequence/all.wren @@ -0,0 +1,10 @@ +var a = [1, 2, 3] +IO.print(a.all {|x| x > 1 }) // expect: false +IO.print(a.all {|x| x > 0 }) // expect: true +IO.print([].all {|x| false }) // expect: true + +// Returns first falsey value. +IO.print(a.all {|x| x < 2 ? null : false }) // expect: null + +// Returns last truthy value. +IO.print(a.all {|x| x }) // expect: 3 \ No newline at end of file diff --git a/test/core/list/all_non_function_arg.wren b/test/core/sequence/all_non_function_arg.wren similarity index 100% rename from test/core/list/all_non_function_arg.wren rename to test/core/sequence/all_non_function_arg.wren diff --git a/test/core/sequence/any.wren b/test/core/sequence/any.wren new file mode 100644 index 00000000..c60d70c8 --- /dev/null +++ b/test/core/sequence/any.wren @@ -0,0 +1,10 @@ +var a = [1, 2, 3] +IO.print(a.any {|x| x > 3 }) // expect: false +IO.print(a.any {|x| x > 1 }) // expect: true +IO.print([].any {|x| true }) // expect: false + +// Returns first truthy value. +IO.print(a.any {|x| x }) // expect: 1 + +// Returns last falsey value. +IO.print(a.any {|x| x < 2 ? null : false }) // expect: false diff --git a/test/core/list/any_non_function_arg.wren b/test/core/sequence/any_non_function_arg.wren similarity index 100% rename from test/core/list/any_non_function_arg.wren rename to test/core/sequence/any_non_function_arg.wren