Hello,
Can somebody explain me why in the below code I need to use filterA in order to filter the Array that readdir returns? In theory the signature for readdir is
readdir :: FilePath -> Aff (Array FilePath)
So once you unwrap the AFF to files, why can’t I just make a normal filer? Is it because the filter function wraps everything on an applicative? According to type inference pure
returns an Applicative, so using filterA seems to make sense, but I don’t understand why pure
is needed here or why does it returns an applicative instead of something else. It is too smart for me…
import Data.Array (filterA)
import Data.Maybe (maybe)
import Data.String.CodeUnits (charAt, singleton)
import Effect (Effect)
import Effect.Aff (launchAff_)
import Effect.Class (liftEffect)
import Effect.Console (log)
import Node.FS.Aff (stat, readdir)
import Node.FS.Stats (isDirectory)
main :: Effect Unit
main = launchAff_ do
files <- readdir "."
files' <- flip filterA files \file -> do
stat <- stat file
pure $ isDirectory stat
&& (maybe false (singleton >>> (_ /= ".")) $ charAt 0 file)
liftEffect $ log $ show files'
On a side note, why is launchAff now returning a fiber so we need to use lauchAff_ instead? And, why don’t change the code to use fibers instead of just use launchAff_?
Thanks in advance.