feat: add List.where method for filtering elements with a predicate function

Implement a `where` method on the List class that takes a function `f` and returns a new list containing only elements for which `f.call(element)` returns true. The implementation is added both in the builtin core.wren source and embedded in the C library source string in wren_core.c. Also includes a new test file `test/list/where.wren` verifying filtering with matching and non-matching predicates.
This commit is contained in:
Bob Nystrom
2014-02-15 19:21:24 +00:00
3 changed files with 27 additions and 3 deletions
+8
View File
@@ -0,0 +1,8 @@
var a = [1, 2, 3]
var moreThan1 = fn (x) { return x > 1 }
var moreThan10 = fn (x) { return x > 10 }
var b = a.where(moreThan1)
var c = a.where(moreThan10)
IO.print(b) // expect: [2, 3]
IO.print(c) // expect: []