#Am I thinking about ingesting numeric input from the CLI at all correctly?

1 messages · Page 1 of 1 (latest)

fringe abyss
#
pub fn main() {
  erlang.get_line("Enter a number:\n")
  |> result.unwrap("0")
  |> string.trim_right
  |> int.parse
  |> result.unwrap(0)
  |> io.debug
}

I mean it works, but it feels wrong.
Is there a more idiomatic way to do such?

humble hedge
#

Is zero a reasonable default in this case?

fringe abyss
#

Let's say yes.

humble hedge
#
"Enter a number:\n"
|> erlang.get_line
|> result.nil_error
|> result.map(with: string.trim)
|> result.try(apply: int.parse)
|> result.unwrap(or: 0)
|> io.debug
#

You could also use case and pattern matching instead.

fringe abyss
#

My understanding is w/ pattern matching strings I'd need to reverse it to ignore the n\ from the newline before I could match.
Becomes just as gross. 🤷‍♂️

Might just be needfully gnarly.
Thanks for the suggestions!

humble hedge
#

I meant pattern matching the Result, not the string.

fringe abyss
#

Interesting.
So, in my mind the thing that feels off is the number of steps to get to a properly parsed int.
I'm sensing that in yours it's the stepwise processing w/o the safety of results. That a fair characterization?

humble hedge
#

Not sure. I don't like doing a bunch of unwraps and repetitive stuff like that.