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


--- Comment #0 from bearophile_hugs eml.cc 2014-02-27 03:14:00 PST ---
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


--- Comment #1 from w0rp <devw0rp gmail.com> 2014-02-27 04:04:26 PST ---
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


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



--- Comment #3 from bearophile_hugs eml.cc 2014-02-28 06:18:45 PST ---
(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? -- 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



--- Comment #4 from yebblies <yebblies gmail.com> 2014-03-01 03:00:09 EST ---
(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. -- 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


--- Comment #5 from bearophile_hugs eml.cc 2014-02-28 09:46:41 PST ---
(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. -- 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



--- Comment #6 from yebblies <yebblies gmail.com> 2014-03-01 14:50:16 EST ---
(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. -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 28 2014