Skip to content

Commit

Permalink
Update README.md examples
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephTLyons committed Nov 9, 2024
1 parent c352cfb commit 24ba090
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,53 @@ A Gleam library providing lenient parsing functions for converting strings to fl
gleam add lenient_parse
```
```gleam
import lenient_parse
import gleam/float
import gleam/int
import gleam/io
import lenient_parse
pub fn main() {
// --- Float parsing
// Parse a string containing an integer
"1" |> lenient_parse.to_float |> io.debug
// Ok(1.0)
"1" |> lenient_parse.to_float |> io.debug // Ok(1.0)
"1" |> float.parse |> io.debug // Error(Nil)
// Parse a string containing a negative float
"-5.001" |> lenient_parse.to_float |> io.debug
// Ok(-5.001)
"-5.001" |> lenient_parse.to_float |> io.debug // Ok(-5.001)
"-5.001" |> float.parse |> io.debug // Ok(-5.001)
// Parse a more complex float with scientific notation
"-1_234.567_8e-2" |> lenient_parse.to_float |> io.debug
// Ok(-12.345678)
// --- Integer parsing
"-1_234.567_8e-2" |> lenient_parse.to_float |> io.debug // Ok(-12.345678)
"-1_234.567_8e-2" |> float.parse |> io.debug // Error(Nil)
// Parse a string containing an integer
"123" |> lenient_parse.to_int |> io.debug
// Ok(123)
"123" |> lenient_parse.to_int |> io.debug // Ok(123)
"123" |> int.parse |> io.debug // Ok(123)
// Parse a string containing a negative integer with surrounding whitespace
" -123 " |> lenient_parse.to_int |> io.debug
// Ok(-123)
" -123 " |> lenient_parse.to_int |> io.debug // Ok(-123)
" -123 " |> int.parse |> io.debug // Error(Nil)
// Parse a string containing an integer with underscores
"1_000_000" |> lenient_parse.to_int |> io.debug
// Ok(1000000)
"1_000_000" |> lenient_parse.to_int |> io.debug // Ok(1000000)
"1_000_000" |> int.parse |> io.debug // Error(Nil)
// Parse a binary string
"1000_0000" |> lenient_parse.to_int_with_base(base: 2) |> io.debug
// Ok(128)
"1000_0000" |> lenient_parse.to_int_with_base(base: 2) |> io.debug // Ok(128)
// No direct Gleam stdlib support
// Parse a hexadecimal string
"DEAD_BEEF" |> lenient_parse.to_int_with_base(base: 16) |> io.debug
// Ok(3735928559)
"DEAD_BEEF" |> lenient_parse.to_int_with_base(base: 16) |> io.debug// Ok(3735928559)
// No direct Gleam stdlib support
}
```

## Developer Setup
Expand Down

0 comments on commit 24ba090

Please sign in to comment.