Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A variant of Apply() that works with any TextParser<TextSpan> #33

Merged
merged 1 commit into from
May 21, 2018

Conversation

nblumhardt
Copy link
Member

While working through #32 I couldn't find an idiomatic/allocation-free way for a text parser to break up a number like 20180521 into numeric parts. This led me to a bit of a general pattern, like this:

var input = new TextSpan("1234");
var twodigits = Span.Length(2).Apply(Numerics.IntegerInt32);
var result = twodigits(input);
Assert.Equal(12, result.Value);

This variant of Apply() makes it possible to split the tasks of delimiting the span to be parsed (Span.Length(2)) from its translation into the value type (here, taking 12 and parsing an int).

Not sure how frequently this will get used - definitely quite pleasant for length-oriented formats like this one - but it's a nice complement to the existing token-oriented Apply() combinator, so might prove fruitful and/or stimulating to include.

@nblumhardt nblumhardt merged commit b9e36e7 into datalust:dev May 21, 2018
@nblumhardt nblumhardt mentioned this pull request Jun 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant