Bug 12272 – and(), or(), xor() to compose predicates
Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-02-27T03:14:00Z
Last change time
2014-02-28T19:50:16Z
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2014-02-27T03:14:00Z
In Java8 there are functions like and() or() xor() that are handy to compose predicates:
Predicate<String> startsWithJ = (n) -> n.startsWith("J");
Predicate<String> fourLetterLong = (n) -> n.length() == 4;
names.stream().filter(startsWithJ.and(fourLetterLong))
In D:
auto startsWithJ = (string n) => n.startsWith("J");
auto fourLetterLong = (string n) => n.length == 4;
names.filter!(and!(startsWithJ, fourLetterLong))
Comment #1 by devw0rp — 2014-02-27T04:04:26Z
I like it.
Comment #2 by yebblies — 2014-02-28T06:05:44Z
(In reply to comment #0)
> In Java8 there are functions like and() or() xor() that are handy to compose
> predicates:
>
> Predicate<String> startsWithJ = (n) -> n.startsWith("J");
> Predicate<String> fourLetterLong = (n) -> n.length() == 4;
> names.stream().filter(startsWithJ.and(fourLetterLong))
>
> In D:
>
> auto startsWithJ = (string n) => n.startsWith("J");
> auto fourLetterLong = (string n) => n.length == 4;
> names.filter!(and!(startsWithJ, fourLetterLong))
Lambdas make this pretty easy...
names.filter!(s => s.startsWithJ && s.fourLetterLong)
Slightly longer, but _much_ more flexible.
Comment #3 by bearophile_hugs — 2014-02-28T06:18:45Z
(In reply to comment #2)
> Lambdas make this pretty easy...
>
> names.filter!(s => s.startsWithJ && s.fourLetterLong)
>
> Slightly longer, but _much_ more flexible.
With those templates you can define a new predicate in point-free style (std.functional.compose!() does something similar for arbitrary functions):
alias two = and!(startsWithJ, fourLetterLong);
With lambdas it becomes similar to:
enum two = (in string s) => s.startsWithJ && s.fourLetterLong;
So are you suggesting to close down this ER because it's not useful enough?
Comment #4 by yebblies — 2014-02-28T08:00:09Z
(In reply to comment #3)
>
> With those templates you can define a new predicate in point-free style
> (std.functional.compose!() does something similar for arbitrary functions):
IIRC std.functional.compose predates lambdas
>
> alias two = and!(startsWithJ, fourLetterLong);
>
> With lambdas it becomes similar to:
>
> enum two = (in string s) => s.startsWithJ && s.fourLetterLong;
I expect we will soon be able to do this:
alias two = s => s.startsWithJ && s.fourLetterLong;
>
> So are you suggesting to close down this ER because it's not useful enough?
I don't think this is worth adding to phobos. Of course what you do with this ER is up to you.
Comment #5 by bearophile_hugs — 2014-02-28T09:46:41Z
(In reply to comment #4)
> I expect we will soon be able to do this:
>
> alias two = s => s.startsWithJ && s.fourLetterLong;
I see no plans for this yet.
> Of course what you do with this ER is up to you.
Your opinion is always welcome.
> I don't think this is worth adding to phobos.
OK, closed.
Comment #6 by yebblies — 2014-02-28T19:50:16Z
(In reply to comment #5)
> (In reply to comment #4)
>
> > I expect we will soon be able to do this:
> >
> > alias two = s => s.startsWithJ && s.fourLetterLong;
>
> I see no plans for this yet.
>
The plans are in my head. You can currently do this:
alias Alias(alias a) = a;
alias two = Alias!(s => s.startsWithJ && s.fourLetterLong);
So the limitation appears to just be syntactical.