www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9895] New: Add functional style regex pattern-matching

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

           Summary: Add functional style regex pattern-matching
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: GenericNPC gmail.com



Based on Scala's pattern-matching with regular expressions syntax, I created
the function std.regex.regexSwitch(formerly switchRegex). It's main use-case is
for reading textual input fields that can be in multiple formats:

    enum WeekDay
    {
        Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
    }

    struct Schedule
    {
        WeekDay day;
        int hour;
    }

    assert(equal(
                [
                    Schedule(WeekDay.Sunday, 1),
                    Schedule(WeekDay.Monday, 14),
                    Schedule(WeekDay.Tuesday, 3),
                    Schedule(WeekDay.Wednesday, 4),
                    Schedule(WeekDay.Thursday, 17),
                    Schedule(WeekDay.Friday, 6),
                ],
                [
                    "Sunday 1AM",
                    "Monday 2PM",
                    "Tuesday 3",
                    "4AM Wednesday",
                    "5PM Thursday",
                    "6 Friday",
                ].map!(regexSwitch!(
                        `(\w+) (\d+)AM`, (WeekDay day, int hour) =>
Schedule(day, hour % 12),
                        `(\w+) (\d+)PM`, (WeekDay day, int hour) =>
Schedule(day, hour % 12 + 12),
                        `(\w+) (\d+)`, (WeekDay day, int hour) => Schedule(day,
hour),
                        `(\d+)AM (\w+)`, (int hour, WeekDay day) =>
Schedule(day, hour % 12),
                        `(\d+)PM (\w+)`, (int hour, WeekDay day) =>
Schedule(day, hour % 12 + 12),
                        `(\d+) (\w+)`, (int hour, WeekDay day) => Schedule(day,
hour),
                        ))()));

See https://github.com/D-Programming-Language/phobos/pull/1241

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 07 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9895


IdanArye <GenericNPC gmail.com> changed:

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


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 07 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9895


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc



Another way to do this is with the unapply() of Issue 596

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 07 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9895





 Another way to do this is with the unapply() of Issue 596
Yes, it looks like it is possible to do this with unapply(which should be opUnapply to be compatible with D's naming conventions). However: 1) In the GitHub pull request, Dmitry Olshansky suggests to combine all patterns to a single, big pattern, to improve performance. I have no idea how to do it, but it might be done in the future, and it can't be done with the switch+opUnapply version. 2) switch in D is a statement, not an expressions - it does not return a value. 3) Scala's unapply for regular expressions does not convert data types. I don't know if it can be done in D's version - it's too early to tell, seeing that opUnapply has not been implemented yet. 4) opUnapply is not in D yet - it's still an enhancement suggestion. We don't know what complications we are gonna have when we try to use it for regular expressions. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 08 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9895


Dmitry Olshansky <dmitry.olsh gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull
             Status|RESOLVED                    |REOPENED
                 CC|                            |dmitry.olsh gmail.com
         Resolution|FIXED                       |



13:03:10 PDT ---
Only to add:
5) We can always add better abstraction if/when we are confident it's actualy
better.

P.S. Doing minor correction - I'm reopening this untill the pull gets in. Let's
do it pedantically in order ;)

 IdanArye  

What we've unofficially all came to is a 3 step procedure:
1. Report
2. Pull posted there and keyword _pull_ is set.
3. Commits get posted automatically by post-commit hook, then somebody takes
time to check and close it.

For the moment we are (sort of) at the stage 2.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 08 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9895




13:03:43 PDT ---
https://github.com/D-Programming-Language/phobos/pull/1241

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 08 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9895





 Only to add:
 5) We can always add better abstraction if/when we are confident it's actualy
 better.
 
 P.S. Doing minor correction - I'm reopening this untill the pull gets in. Let's
 do it pedantically in order ;)
 
  IdanArye  
 
 What we've unofficially all came to is a 3 step procedure:
 1. Report
 2. Pull posted there and keyword _pull_ is set.
 3. Commits get posted automatically by post-commit hook, then somebody takes
 time to check and close it.
 
 For the moment we are (sort of) at the stage 2.
Oh, sorry. Had no idea about needing the `pull` keyword, or auto-closing when the request is pulled. My previous contribution to Phobos was done entirely on GitHub... Just to be clear - the rule is still to use a single commit message for the contribution, using git's history rewriting functionality to make fixes, right? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 08 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9895




13:33:32 PDT ---


 For the moment we are (sort of) at the stage 2.
Oh, sorry. Had no idea about needing the `pull` keyword, or auto-closing when the request is pulled. My previous contribution to Phobos was done entirely on GitHub... Just to be clear - the rule is still to use a single commit message for the contribution, using git's history rewriting functionality to make fixes, right?
Yup, all the same. Plus this new idea of issue pre new stuff too. I've seen cases where it the hook doesn't do closing. Could be a glitch or that an issue was mentioned but not as "fix issue xyz" but rather simply "issue xyz". -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 08 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9895







 For the moment we are (sort of) at the stage 2.
Oh, sorry. Had no idea about needing the `pull` keyword, or auto-closing when the request is pulled. My previous contribution to Phobos was done entirely on GitHub... Just to be clear - the rule is still to use a single commit message for the contribution, using git's history rewriting functionality to make fixes, right?
Yup, all the same. Plus this new idea of issue pre new stuff too. I've seen cases where it the hook doesn't do closing. Could be a glitch or that an issue was mentioned but not as "fix issue xyz" but rather simply "issue xyz".
issue 9895" to avoid problems... -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 08 2013