albertdahlin / elm-posix

Write CLI-scripts in Elm
MIT License
91 stars 6 forks source link

pattern matching IO? #8

Open cobordism opened 23 hours ago

cobordism commented 23 hours ago

Hello, Is there any way I can use pattern matching to get a String from an IO String? I've tried, but the deeper I dig the more confused I get. Relatedly, is there a way to have a map2 for IO? and a function IO ( IO a)) -> IO a ?

I apologise if these are easy questions. I am new at elm and got confused. The context is that I have a function String -> String -> String and I want to read the first two arguments from a text file.

Or is there maybe even an "evil" way to just read in a String instead of IO String from a text file ?

lue-bird commented 23 hours ago

Indeed an IO.map2 would be nice. You can create it yourself with andThen:

ioMap2 combineAB ioA ioB =
    ioA |> IO.andThen (\a -> IO.andThen (\b -> combineAB a b))
-- in
ioMap2
    (\a b -> yourFunction a b)
    (File.open "a.txt" |> IO.exitOnError identity)
    (File.open "b.txt" |> IO.exitOnError identity)
cobordism commented 22 hours ago

Thank you. I also just succeeded ... because I just discovered the new-api branch of this repo. (I wish I had found that sooner. )

The instructions in the readme:

npm install -g @albertdahlin/elm-posix
elm install albertdahlin/elm-posix

gives me the older version.

What is the correct way to install the new elm-posix branch?

lue-bird commented 22 hours ago

Pretty sure the author didn't publish it yet. So you can either try to vendor it, stick with the current API or switch to an alternative like elm-pages-scripts (not sure how) or state-interface etc.

cobordism commented 22 hours ago

pardon my ignorance, but what is 'vendor' as a verb?

lue-bird commented 22 hours ago

haha I'm not a native speaker but as I understood it it basically means copy it to your project directly and build/link/... it there with your project