[Issue 12272] and(), or(), xor() to compose predicates

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Feb 28 06:05:53 PST 2014


https://d.puremagic.com/issues/show_bug.cgi?id=12272


yebblies <yebblies at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |yebblies at gmail.com


--- Comment #2 from yebblies <yebblies at gmail.com> 2014-03-01 01:05:44 EST ---
(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.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list