Skip to content

Instantly share code, notes, and snippets.

@hugopeixoto
Created March 3, 2015 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugopeixoto/64f8efd36ee7b41dd931 to your computer and use it in GitHub Desktop.
Save hugopeixoto/64f8efd36ee7b41dd931 to your computer and use it in GitHub Desktop.
$cond = -> (b,y,n) { b ? y : n }
$any = -> (e) { -> (p) { e.any? &p } }
$all = -> (e) { -> (p) { e.all? &p } }
$make_value = ->(mho) { ->(obj) { ->(k,v) { $cond[k == :or, mho[v], ->(obj) { obj[k] == v }][obj] } } }
$applier = ->(reducer) { ->(mho) { ->(query) { ->(obj) { reducer[query][$make_value[mho][obj]] } } } }
$ycombinator = ->(f) { ->(x) { x[x] }[ ->(x) { f[->(y) { x[x][y] } ] } ] }
$make_hash_or = $ycombinator[$applier[$any]]
$make_hash_and = $applier[$all][$make_hash_or]
make = $make_hash_and
## test
query = {or: {a: 1, c: 6}, b: 4}
query = make[query]
values = [{a: 1, b: 4, c: 3}, {a: 4, b: 4, c: 6}, {a:4, b: 4, c: 4}, { a: 1, b: 1, c: 6}]
1/0 unless [true, true, false, false] == values.map(&query)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment