From 12f16ca8cd75acfca2e2b182f192b2a3b78da210 Mon Sep 17 00:00:00 2001 From: "Joseph T. Lyons" Date: Sat, 9 Nov 2024 14:22:46 -0500 Subject: [PATCH] Update README.md examples --- README.md | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 0f0769b..566666a 100644 --- a/README.md +++ b/README.md @@ -12,46 +12,66 @@ 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" |> 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" |> 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" |> float.parse |> io.debug + // Error(Nil) // Parse a string containing an integer + "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 " |> 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" |> int.parse |> io.debug + // Error(Nil) // Parse a binary string + "1000_0000" |> lenient_parse.to_int_with_base(base: 2) |> io.debug // Ok(128) + // No Gleam stdlib support // Parse a hexadecimal string "DEAD_BEEF" |> lenient_parse.to_int_with_base(base: 16) |> io.debug // Ok(3735928559) + // No Gleam stdlib support } + ``` ## Developer Setup