Notice that it actually is processing int results! This is because 123
looks like an integer to me, but maybe it looks like a float to you. If you had
int : undefiend, floats would need a decimal like 1.0 in every case. If you
like explicitness, that may actually be preferable!
Note: This function does not check for weird trailing characters in the
current implementation, so parsing 123abc can succeed up to 123 and then
move on. This is helpful for people who want to parse things like 40px or
3m, but it requires a bit of extra code to rule out trailing characters in
other cases.
Parse a bunch of different kinds of numbers without backtracking.
Remarks
Example
A parser for Elm would need to handle integers, floats, and hexadecimal like this:
Example
Float
If you wanted to implement the
float
parser, it would be like this:Notice that it actually is processing
int
results! This is because123
looks like an integer to me, but maybe it looks like a float to you. If you hadint : undefiend
, floats would need a decimal like1.0
in every case. If you like explicitness, that may actually be preferable!Note: This function does not check for weird trailing characters in the current implementation, so parsing
123abc
can succeed up to123
and then move on. This is helpful for people who want to parse things like40px
or3m
, but it requires a bit of extra code to rule out trailing characters in other cases.