[Nix-dev] isFloat

Rok Garbas rok at garbas.si
Mon Jan 4 17:12:05 CET 2016


should we add this to nixpkgs/lib? it is usefull (using it right now) :)

Quoting Игорь Пашев (2015-12-29 23:32:52)
> OMG :-) Thank you. I didn't know about regexes. Now we are saved!
> 
> 29 дек. 2015 г. 23:38 пользователь "Harald van Dijk" <harald at gigawatt.nl>
> написал:
> 
>     On 29/12/2015 13:20, Игорь Пашев wrote:
>     > Is anybody needs floats in options :-)
>     >
>     >    isFloat = x: isInt x ||
>     >        ( isString x &&
>     >          3 > length (splitString "." x) &&
>     >          all (s: 2 == length (splitString s " 0123456789. "))
>     > (stringToCharacters x)
>     >        );
> 
>     Nice. Here are some corner cases to consider though :)
> 
>       isFloat (sub 0 1) -> true
>     yet
>       isFloat (toString (sub 0 1)) -> false
> 
>       isFloat "" -> true
>       isFloat ".." -> true
>       isFloat "1.2." -> true
>       isFloat "1 2" -> true
> 
>     How about this alternative using a regex?
> 
>       isFloat = x: with builtins;
>         match "-?([0-9]+(\\.[0-9]*)?|\\.[0-9]+)" (toString x) != null;
> 
>     This assumes "1" is valid, "1.1" is valid, "1." is valid, ".1" is valid,
>     but plain "." is invalid. It allows an optional leading minus sign
>     regardless of whether the argument is an integer or a string. It doesn't
>     allow any spaces, not even leading or trailing spaces.
> 
>     If the precise syntax of allowable floating point numbers changes, for
>     instance if "." must always be followed by a digit, if a leading "+" is
>     also allowed, if exponential notation ("1E10") is supposed to be allowed,
>     if some spaces should be allowed, etc., it can be tweaked fairly easily.
> 
>     Cheers,
>     Harald van Dijk
> 


--
Rok Garbas - http://www.garbas.si
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: signature
Url : http://lists.science.uu.nl/pipermail/nix-dev/attachments/20160104/bdc2e456/attachment.bin 


More information about the nix-dev mailing list