Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Is there a combinator for "post processing" a parser result</p> </blockquote> <p>It depends on what you want to do if you fail. You can always do <code>|&gt;&gt;</code> to get your <code>DateTime</code> out. Failing it is equally interesting, I think your example could be (given a parser <code>sp</code> that gets the correct string, note it would be of type <code>Parser&lt;string,'u&gt;</code>)</p> <pre><code>sp &gt;&gt;= (fun s -&gt; match DateTime.TryParse s with | true,result -&gt; preturn result | false,_ -&gt; fail) </code></pre> <p>Here I am taking in the resultant string and calling the <code>TryParse</code> method, and returning either a <code>preturn</code> or a <code>fail</code> depending on whether it succeeds. I couldn't find any of the methods that worked exactly like that.</p> <p>Note that <code>&gt;&gt;=?</code> would cause a backtrack if it failed.</p> <blockquote> <p>Is it possible to apply a predicate to a string (as satisfy does for char)?</p> </blockquote> <p>You would have to call the predicate for every character (<code>2</code>, <code>20</code>, <code>201</code>) which is usually not ideal. I am pretty sure you could whip up something like this if you wanted, but I don't think it is ideal for that reason, not to mention handling partial matches becomes harder.</p> <blockquote> <p>Is there a better approach for parsing date/time?</p> </blockquote> <p>The biggest factor is "What do you know about the date/time?" If you know it is in that syntax exactly then you should be able to use a post process and be fine (since hopefully the error case will be rare)</p> <p>If you aren't sure, for instance if <code>PM</code> is optional, but would be unambiguously detailed, then you will probably want to break up the definition and combine it at the end. Note that here I have relaxed the character counts a little, you could add some <code>opt</code> to get even more relaxed, or replace the <code>pint32</code> with <code>digit</code> and manual conversions.</p> <pre><code>let pipe6 = //Implementation left as an exercise let dash = skipChar '-' let space = skipChar ' ' let colon = skipChar ':' pipe6 (pint32 .&gt;&gt; dash) //Year (pint32 .&gt;&gt; dash) //Month (pint32 .&gt;&gt; space) //Day (pint32 .&gt;&gt; colon) //Hour (pint32 .&gt;&gt; space) //Minute (anyString) //AM/PM (fun year month day hour minute amPm -&gt; DateTime(year, month, day, hour + (if amPm.Equals("PM", StringComparison.InvariantCultureIgnoreCase) then 12 else 0), minute, 0)) //No seconds </code></pre> <p>Writing all that out I am not sure if you are better or worse off...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload