3 ms·
That's what's nice about functional programming, where functions are functions, not thingies inside your object. You would write something like: filter pre
by jrockway 16y ago
That's what's nice about functional programming, where functions are functions, not thingies inside your object. You would write something like:
filter predicate1 . filter predicate2 . filter predicate3 $ employees
Where . is function composition.
- jlouis 16y agoYou still need to hope the fusion optimization kicks in there. It can be done faster with one filter over the list and then checking all three predicates at each element in some cases. I think the following will do the right thing: filter (\x -> p1 x && p2 x && p3 x) employees which can then be made points-free.
- lsb 16y agoDon't just hope, write it yourself! {-# RULES "filter/filter" forall f g xs. filter f (filter g xs) = filter (f.g) xs #-} It's a programmable programming language.
- ced 16y agoNice. What's the limit on what you can do with these RULES? Could you extend the language? Write a DSL? Implement a mini-prolog à la On Lisp?
- jrockway 16y agoYou can do a lot of weird stuff, but it's not the right place for that. Just use Template Haskell.
- uros643 16y agoDon't you mean forall f g xs. map f (map g xs) = map (f.g) xs ? As for filter, I presume something like forall f g xs. filter f (filter g xs) = filter (\x. g x && f x) xs would work.
- lsb 16y agoYup, forall f g xs . filter f (filter g xs) = filter (\x -> g x && f x) xs was exactly what I meant, thanks for the catch!
- xtacy 16y agoThe beauty is that with Haskell's Lazy Evaluation, this is what would end up happening.