I'm trying to implement a recursive function to remove empty directories in purescript. For the following code I get an error about matching Effect with Array.
module Test where
import Prelude
import Data.Array as Array
import Effect (Effect)
import Node.Buffer.Class (toArray)
import Node.FS.Stats (isDirectory)
import Node.FS.Sync as FS
import Node.Path (FilePath)
import Prim.Boolean (False)
rmEmptyDirs :: FilePath -> Effect Unit
rmEmptyDirs path = do
stats <- FS.stat path
if isDirectory stats then do
files <- FS.readdir path
if Array.length files == 0 then
FS.rmdir path
else do
file <- files
rmEmptyDirs file
else
pure unit
Here is the error message:
Could not match type
Effect
with type
Array
while trying to match type Effect Unit
with type Array t0
while checking that expression rmEmptyDirs file
has type Array t0
in binding group rmEmptyDirs
where t0 is an unknown type
I understand that the innermost do block is in an Array context. I don't know how to "strip off" the Effect from the recursive call to rmEmptyDirs. Putting Array.singleton $ before the call doesn't help. liftEffect has the opposite effect of what I want to do. How do I get this compile?