Skip to content

Instantly share code, notes, and snippets.

@searls
Created January 17, 2012 03:11
Show Gist options
  • Save searls/1624323 to your computer and use it in GitHub Desktop.
Save searls/1624323 to your computer and use it in GitHub Desktop.
recursively find all values that pass a truth test
foo = {}
bar = {}
baz = {}
o =
z: foo
foo:
a: bar
b: 3
z:
lah: baz
findAllValuesThat = (root, test) ->
_(root).reduce( (memo, value)->
_(memo).union(findAllValuesThat(value,test))
,if test(root) then [root] else [])
describe "recursing", ->
When -> @result = findAllValuesThat(o, (value) -> value in [foo,bar,baz])
Then -> expect(@result.length).toBe(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment