Skip to content

Commit a17696e

Browse files
committed
Update README.md examples
1 parent 9a01f5f commit a17696e

File tree

1 file changed

+50
-30
lines changed

1 file changed

+50
-30
lines changed

README.md

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ and `int()` functions for parsing strings into float and integer values. Use
88
`lenient_parse` when `int.parse` and `float.parse` from the Gleam
99
[stdlib](https://github.com/gleam-lang/stdlib) are too strict.
1010

11-
- `float("3.14")` -> `"3.14" |> lenient_parse.to_float`
12-
- `int("42")` -> `"42" |> lenient_parse.to_int`
13-
- `int("1010", base=2)` -> `"1010" |> lenient_parse.to_int_with_base(2)`
11+
- `float("3.14")` -> `lenient_parse.to_float("3.14")`
12+
- `int("42")` -> `lenient_parse.to_int("42")`
13+
- `int("1010", base=2)` -> `lenient_parse.to_int_with_base("1010", 2)`
1414

1515
## Installation
1616

@@ -23,72 +23,92 @@ gleam add lenient_parse
2323
```gleam
2424
import gleam/float
2525
import gleam/int
26-
import gleam/io
2726
import gleam/list
2827
import lenient_parse
2928
3029
pub fn main() {
3130
// Parse a string containing an integer value into a float
3231
33-
"1" |> lenient_parse.to_float |> io.debug // Ok(1.0)
34-
"1" |> float.parse |> io.debug // Error(Nil)
32+
let _ = echo lenient_parse.to_float("1")
33+
// Ok(1.0)
34+
let _ = echo float.parse("1")
35+
// Error(Nil)
3536
3637
// Parse a string containing a negative float
3738
38-
"-5.001" |> lenient_parse.to_float |> io.debug // Ok(-5.001)
39-
"-5.001" |> float.parse |> io.debug // Ok(-5.001)
39+
let _ = echo lenient_parse.to_float("-5.001")
40+
// Ok(-5.001)
41+
let _ = echo float.parse("-5.001")
42+
// Ok(-5.001)
4043
4144
// Parse a string containing a complex float with scientific notation
4245
43-
"-1_234.567_8e-2" |> lenient_parse.to_float |> io.debug // Ok(-12.345678)
44-
"-1_234.567_8e-2" |> float.parse |> io.debug // Error(Nil)
46+
let _ = echo lenient_parse.to_float("-1_234.567_8e-2")
47+
// Ok(-12.345678)
48+
let _ = echo float.parse("-1_234.567_8e-2")
49+
// Error(Nil)
4550
4651
// Parse a string containing an integer
4752
48-
"123" |> lenient_parse.to_int |> io.debug // Ok(123)
49-
"123" |> int.parse |> io.debug // Ok(123)
53+
let _ = echo lenient_parse.to_int("123")
54+
// Ok(123)
55+
let _ = echo int.parse("123")
56+
// Ok(123)
5057
5158
// Parse a string containing a negative integer with surrounding whitespace
5259
53-
" -123 " |> lenient_parse.to_int |> io.debug // Ok(-123)
54-
" -123 " |> int.parse |> io.debug // Error(Nil)
60+
let _ = echo lenient_parse.to_int(" -123 ")
61+
// Ok(-123)
62+
let _ = echo int.parse(" -123 ")
63+
// Error(Nil)
5564
5665
// Parse a string containing an integer with underscores
5766
58-
"1_000_000" |> lenient_parse.to_int |> io.debug // Ok(1000000)
59-
"1_000_000" |> int.parse |> io.debug // Error(Nil)
67+
let _ = echo lenient_parse.to_int("1_000_000")
68+
// Ok(1000000)
69+
let _ = echo int.parse("1_000_000")
70+
// Error(Nil)
6071
6172
// Parse a string containing a binary number with underscores
6273
63-
"1000_0000" |> lenient_parse.to_int_with_base(2) |> io.debug // Ok(128)
64-
"1000_0000" |> int.base_parse(2) |> io.debug // Error(Nil)
74+
let _ = echo lenient_parse.to_int_with_base("1000_0000", 2)
75+
// Ok(128)
76+
let _ = echo int.base_parse("1000_0000", 2)
77+
// Error(Nil)
6578
6679
// Parse a string containing a hexadecimal number with underscores
6780
68-
"DEAD_BEEF" |> lenient_parse.to_int_with_base(16) |> io.debug // Ok(3735928559)
69-
"DEAD_BEEF" |> int.base_parse(16) |> io.debug // Error(Nil)
81+
let _ = echo lenient_parse.to_int_with_base("DEAD_BEEF", 16)
82+
// Ok(3735928559)
83+
let _ = echo int.base_parse("DEAD_BEEF", 16)
84+
// Error(Nil)
7085
7186
// Use base 0 to automatically detect the base when parsing strings with prefix indicators
7287
7388
let dates = [
74-
"0b11011110000", "0o3625", "1865", "0x7bc", "0B11110110001", "1929",
75-
"0O3507", "0X7a9", "0b11011111011",
89+
"0b11011110000",
90+
"0o3625",
91+
"1865",
92+
"0x7bc",
93+
"0B11110110001",
94+
"1929",
95+
"0O3507",
96+
"0X7a9",
97+
"0b11011111011",
7698
]
7799
78-
dates
79-
|> list.map(lenient_parse.to_int_with_base(_, 0))
80-
|> io.debug()
100+
let _ = echo list.map(dates, lenient_parse.to_int_with_base(_, 0))
81101
// [Ok(1776), Ok(1941), Ok(1865), Ok(1980), Ok(1969), Ok(1929), Ok(1863), Ok(1961), Ok(1787)]
82102
83-
dates
84-
|> list.map(int.base_parse(_, 0))
85-
|> io.debug()
103+
let _ = echo list.map(dates, int.base_parse(_, 0))
86104
// [Error(Nil), Error(Nil), Error(Nil), Error(Nil), Error(Nil), Error(Nil), Error(Nil), Error(Nil), Error(Nil)]
87105
88106
// Nice errors
89107
90-
"12.3e_3" |> lenient_parse.to_float |> io.debug // Error(InvalidUnderscorePosition(5))
91-
"12.3e_3" |> float.parse |> io.debug // Error(Nil)
108+
let _ = echo lenient_parse.to_float("12.3e_3")
109+
// Error(InvalidUnderscorePosition(5))
110+
let _ = echo float.parse("12.3e_3")
111+
// Error(Nil)
92112
}
93113
```
94114

0 commit comments

Comments
 (0)