From 8f985847d47ff4e041a2a881c5581c02ba9507fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Sat, 28 Feb 2015 21:45:39 +0100 Subject: [PATCH 1/2] Added Sequence.any as complement to Sequence.all --- builtin/core.wren | 7 +++++++ doc/site/core/sequence.markdown | 16 ++++++++++++++-- src/wren_core.c | 7 +++++++ test/list/any.wren | 10 ++++++++++ test/list/any_non_bool_returning_fn.wren | 1 + test/list/any_non_function_arg.wren | 1 + 6 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 test/list/any.wren create mode 100644 test/list/any_non_bool_returning_fn.wren create mode 100644 test/list/any_non_function_arg.wren diff --git a/builtin/core.wren b/builtin/core.wren index 8fe04790..328d0627 100644 --- a/builtin/core.wren +++ b/builtin/core.wren @@ -22,6 +22,13 @@ class Sequence { 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 af55277e..a36e394b 100644 --- a/doc/site/core/sequence.markdown +++ b/doc/site/core/sequence.markdown @@ -11,13 +11,25 @@ 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 it returns `false`, stops iterating and returns `false`. Otherwise, returns -`true`. +If its return value evaluates to `false`, stops iterating and returns `false`. +Otherwise, returns `true`. :::dart [1, 2, 3].all {|n| n > 2} // False. [1, 2, 3].all {|n| n < 4} // True. +### **any**(predicate) + +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`. + + :::dart + [1, 2, 3].any {|n| n < 1} // False. + [1, 2, 3].any {|n| n > 2} // True. + ### **join**(sep) Returns a string representation of the list. The string representations of the diff --git a/src/wren_core.c b/src/wren_core.c index b437a7d1..e20c3666 100644 --- a/src/wren_core.c +++ b/src/wren_core.c @@ -67,6 +67,13 @@ static const char* libSource = " 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/list/any.wren b/test/list/any.wren new file mode 100644 index 00000000..79c1cfca --- /dev/null +++ b/test/list/any.wren @@ -0,0 +1,10 @@ +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/list/any_non_bool_returning_fn.wren b/test/list/any_non_bool_returning_fn.wren new file mode 100644 index 00000000..cdd2b575 --- /dev/null +++ b/test/list/any_non_bool_returning_fn.wren @@ -0,0 +1 @@ +IO.print([1, 2, 3].any {|x| "truthy" }) // expect: true diff --git a/test/list/any_non_function_arg.wren b/test/list/any_non_function_arg.wren new file mode 100644 index 00000000..00973dfa --- /dev/null +++ b/test/list/any_non_function_arg.wren @@ -0,0 +1 @@ +[1, 2, 3].any("string") // expect runtime error: String does not implement 'call(_)'. From f10e0121b60cc6455bcf2d8631b56afbcb9a1424 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Sun, 1 Mar 2015 15:18:58 +0100 Subject: [PATCH 2/2] Two small doc fixes * Fixed inconsistency in Lua vs. Wren example * Fixed broken sentence --- doc/site/qa.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/site/qa.markdown b/doc/site/qa.markdown index 130bff8b..c1dd6f24 100644 --- a/doc/site/qa.markdown +++ b/doc/site/qa.markdown @@ -61,12 +61,12 @@ Here's the same example in Wren: } // create and use an Account - var acc = new Account(100) + var acc = new Account(1000) acc.withdraw(100) Classes have a reputation for complexity because most of the widely used languages with them are quite complex: C++, Java, C#, Ruby, and Python. I hope -to show with Wren that is those languages that are complex, and not classes +to show with Wren that it is those languages that are complex, and not classes themselves. Smalltalk, the language that inspired most of those languages, is famously