Skip to content

[P1] No way to parse a string to a number #18

@ecto

Description

@ecto

Today there is no string to number conversion. [int "42"] and
[float "3.14"] both return (); there is no parse-int,
parse-float, to-int, or equivalent in the builtin set.

This is the single most surprising gap when writing real programs:
you cannot read a number from stdin, args, env, or a file.

Proposal

Add parse-int and parse-float, both returning Option
(or Result for richer errors):

[parse-int "42"]   ; [Some 42]
[parse-int "x"]    ; None
[parse-float "3.14"]

Acceptance criteria

  • Builtins available in the VM and listed in web/src/pages/ref/builtins.loon.
  • A test covering valid input, invalid input, leading/trailing whitespace,
    negative numbers, and out-of-range integers.

Found while completing the hyperpolyglot ml column.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions