Skip to content

Instantly share code, notes, and snippets.

@dpraimeyuu
Last active November 15, 2016 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpraimeyuu/ab3b436019f5efb757fb4898d0b0f8b7 to your computer and use it in GitHub Desktop.
Save dpraimeyuu/ab3b436019f5efb757fb4898d0b0f8b7 to your computer and use it in GitHub Desktop.
Having fun with 'Learn PureScript" - naive 'whereIs' function implementation
module Data.FileOperations where
import Prelude
import Data.Maybe (Maybe(Nothing), Maybe(Just))
import Data.String (contains, Pattern(Pattern))
import Data.Array (foldl)
import Data.Path
whereIs :: Path -> String -> Maybe Path
whereIs path filePattern =
foldl (whereIsReducerFactory filePattern) Nothing $ transform (ls path)
where
transform :: Array Path -> Array (Maybe Path)
transform arr =
map (\item -> Just item) arr
whereIsReducerFactory :: String -> Maybe Path -> Maybe Path -> Maybe Path
whereIsReducerFactory file acc curr =
case acc, curr of
Just accValue, _ -> searchForFile accValue file
_, Just currValue -> searchForFile currValue file
_, _ -> Nothing
searchForFile :: Path -> String -> Maybe Path
searchForFile p f =
case isDirectory p of
true -> whereIs p f
_ -> checkFile p f
checkFile :: Path -> String -> Maybe Path
checkFile p f =
case contains (Pattern f) (filename p) of
true -> Just p
_ -> Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment