Assume we have a file my_file.txt
with contents:
foo
bar
and another file my_other_file.txt
containing:
baz
I would like to read the contents of these two files using turtle
so that I get a Shell
of lines which will produce:
foo
bar
baz
In Haskell's turtle
library one can read a list of files by using input
, for instance:
view $ input "my_file.txt"
We have that
input :: FilePath -> Shell Line
And Shell
has no Monoid
instances (which I think makes sense since we cannot associate IO
operations), so the only operator I can think of using is (<|>)
:
view $ foldl (<|>) empty $ map input ["my_file.txt", "my_other_file.txt"]
While this produces the desired effect, I wonder whether there is a library in the turtle
eco-system that takes care of this, or whether there is a traverse
like operation that can be use on Alternative
's.
EDIT: the effect above could be also achieved by using asum
:
asum $ input <$> ["my_file.txt", "my_other_file.txt"]
Line
has aMonoid
instance. If we have a list ofLine
s, we canmconcat
them into a single one:Since
Shell
has anApplicative
instance, we can usetraverse
to useinput
over a list of files:We end up with a
Shell [Line]
. SinceShell
is aFunctor
, we canfmap
mconcat
(orfold
if you don't use a list):