www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12272] New: and(), or(), xor() to compose predicates

reply d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12272

           Summary: and(), or(), xor() to compose predicates
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



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))

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 27 2014
next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12272


w0rp <devw0rp gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |devw0rp gmail.com



I like it.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 27 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12272


yebblies <yebblies gmail.com> changed:

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




 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: -------
Feb 28 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12272






 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? -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 28 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12272





 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. -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 28 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12272


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID





 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. -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 28 2014
prev sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12272






 
 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. -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 28 2014