News for hackers. Stuff that matters.
Re:Ladies, look at your code. Now back to mine. by mhansen on Saturday August 28, 2010 @04:23AM (#1641465)
It's sad that you can't easily chain these methods. e.g.

    employees.filter(predicate1).filter(predicate2).filter(predicate3)
instead of

    Iterables.filter(Iterables.filter(Iterables.filter(employees,predicate1), predicate2), predicate3)
Re:Ladies, look at your code. Now back to mine. by jrockway on Saturday August 28, 2010 @04:42AM (#1641482)
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.
Re:Ladies, look at your code. Now back to mine. by jlouis on Saturday August 28, 2010 @06:06AM (#1641547)
You 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.
Re:Ladies, look at your code. Now back to mine. by lsb on Saturday August 28, 2010 @10:22AM (#1641765)
Don'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.
Re:Ladies, look at your code. Now back to mine. by uros643 on Saturday August 28, 2010 @08:25PM (#1642740)
Don'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.
Re:Ladies, look at your code. Now back to mine. by lsb on Sunday August 29, 2010 @09:46AM (#1643790)
Yup,

  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!
Re:Ladies, look at your code. Now back to mine. by ced on Saturday August 28, 2010 @10:33AM (#1641775)
Nice. 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?
Re:Ladies, look at your code. Now back to mine. by jrockway on Saturday August 28, 2010 @04:36PM (#1642353)
You can do a lot of weird stuff, but it's not the right place for that. Just use Template Haskell.
Re:Ladies, look at your code. Now back to mine. by xtacy on Saturday August 28, 2010 @01:19PM (#1641995)
The beauty is that with Haskell's Lazy Evaluation, this is what would end up happening.
Re:Ladies, look at your code. Now back to mine. by BonoboBoner on Saturday August 28, 2010 @04:49AM (#1641488)
VarArgs usage would be even better:

employees.filter(predicate1, predicate2, predicate3);

Re:Ladies, look at your code. Now back to mine. by ekiru on Saturday August 28, 2010 @03:02PM (#1642202)
Perl 6 would do that like this:

@employees.grep($predicate1 & $predicate2 & $predicate3);

The predicates can be a function, a regex, a type, or anything that can be smart-matched against.

Alternately, if you want to get the employees that match any of the predicates, you'd do the following:

@employees.grep($predicate1 | $predicate2 | $predicate3);

Or, if you want the employees that match only one of the predicates (think of xor):

@employees.grep($predicate1 ^ $predicate2 ^ $predicate3);

These can also be written, respectively, as:

@employees.grep(all($predicate1, $predicate2, $predicate3)); @employees.grep(any($predicate1, $predicate2, $predicate3)); @employees.grep(one($predicate1, $predicate2, $predicate3));

And, if you want things that match none of the predicates:

@employees.grep(none($predicate1, $predicate2, $predicate3));

You can combine these junction constructors. For example:

@employees.grep(all($predicate1, $predicate2) ^ $predicate3);

Re:Ladies, look at your code. Now back to mine. by Nitramp on Saturday August 28, 2010 @04:38AM (#1641476)
I think you will be, if they manage to get extension methods into this properly. Also, Google's Iterables could return a FilterableIterator or something like that, so you could at least chain after the first step.