1
votes

I'm learning purescript and trying to log a directory content.

module Main where
import Prelude
import Data.Traversable (traverse)
import Effect (Effect)
import Effect.Console (log)
import Node.FS.Sync (readdir)
fnames = readdir "."
main = do
  travere (\a -> log $ show a) fnames

I want to get folder entries printed in console output. I can not get rid of (or pass through) the Effect which I get from Node.FS.Sync (readdir) (I get Effect (Array String)). And traverse or log or show can not work with Effect in front of Array String.

I get No type class instance was found for Data.Traversable.Traversable Effect.

1

1 Answers

3
votes

Effect is a program, not a value. Effect (Array String) is a program that, when executed, will produce an Array String. You cannot get the Array String out of that program without executing it.

One way to execute this program is to make it part of a larger program, such as, for example, your main program. Like this:

main = do
    ns <- fnames
    traverse (\a -> log $ show a) ns

Of course, there is really no need to put it in a global variable fnames before making it part of the main program. You can include readdir "." directly:

main = do
    ns <- readdir "."
    traverse (\a -> log $ show a) ns