Last active
August 29, 2015 14:21
-
-
Save tjaskula/df162110ab7e9b66f19a to your computer and use it in GitHub Desktop.
Defining SelectMany method's signature for Parser type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var p1 = startSriper.ToParser(); | |
var p2 = endSriper.ToParser(); | |
var composite = p1.Compose(p2); | |
var result2 = composite(content); | |
var parsers = from a in p1 | |
from b in p2 | |
select b; | |
var result3 = parsers(content); | |
public static Parser<TSource, TResult> Bind<TSource, TResultTemp, TResult>(this Parser<TSource, TResultTemp> p, Func<TResultTemp, ParseResult<TResult>> func) | |
{ | |
return input => | |
{ | |
var r1 = p(input); | |
var ar1 = r1 as Success<TResultTemp>; | |
if (ar1 == null) | |
{ | |
return new Error<TResult>(((Error<TResultTemp>)r1).Message); | |
} | |
return func(ar1.Parsed); | |
}; | |
} | |
public static Parser<TSource, TResult> SelectMany<TSource, TResultTemp, TCollection, TResult>(this Parser<TSource, TResultTemp> source, | |
Func<TResultTemp, Parser<TResultTemp, TCollection>> collectionSelector, | |
Func<TResultTemp, TCollection, TResult> resultSelector) | |
{ | |
return source.Bind(a => collectionSelector(a).Bind(b => resultSelector(a, b).ToParseResult())(a)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment