Last active
November 15, 2016 21:42
-
-
Save dpraimeyuu/ab3b436019f5efb757fb4898d0b0f8b7 to your computer and use it in GitHub Desktop.
Having fun with 'Learn PureScript" - naive 'whereIs' function implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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