Skip to content

Instantly share code, notes, and snippets.

@robotlolita
Last active December 27, 2015 19:49
Show Gist options
  • Save robotlolita/7379356 to your computer and use it in GitHub Desktop.
Save robotlolita/7379356 to your computer and use it in GitHub Desktop.
filters = [
function isEven(a) { return a % 2 === 0 },
function isPositive(a) { return a > 0 }
]
var items = [-4, -3, -2, -1, 0, 1, 2, 3, 4]
// <*> :: [(a -> b)] -> [a] -> [b]
function ap(fs){ return function(y) {
return fs.map(function(f){ return f(y) })}}
// any :: (a -> b) -> [a] -> Bool
function any(f){ return function(xs) {
return xs.some(f) }}
// (f . g) :: (b -> c) -> (a -> b) -> a -> c
function compose(f, g) { return function(a) {
return f(g(a)) }}
// The thing
items.filter(compose(any(Boolean), ap(filters)))
filters = [
function is-even(a) => a % 2 === 0
function is-positive(a) => a > 0
]
items = [-4, -3, -2, -1, 0, 1, 2, 3, 4]
# <*> :: [(a -> b)] -> [a] -> [b]
ap = (fs, a) --> fs.map (f) -> f a
# any :: (a -> b) -> [a] -> Bool
any = (f, xs) --> xs.some f
# The thing
items.filter (any Boolean . ap filters)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment