Last active
March 2, 2016 15:39
-
-
Save stevenproctor/135be29e7f1d457ab3ec to your computer and use it in GitHub Desktop.
PureScript By Example - Write a fold to determine the largest and smallest files in the filesystem.
This file contains 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
onlyFiles :: Path -> Array Path | |
onlyFiles file = onlyFiles' file [] | |
where | |
onlyFiles' file files | isDirectory file = do | |
child <- ls file | |
onlyFiles' child files | |
onlyFiles' file files = file : files | |
safeMin :: Maybe Path -> Maybe Path -> Maybe Path | |
safeMin Nothing x = x | |
safeMin x Nothing = x | |
safeMin (Just x) (Just y) | size x < size y = Just x | |
safeMin (Just x) (Just y) = Just y | |
safeMax :: Maybe Path -> Maybe Path -> Maybe Path | |
safeMax Nothing x = x | |
safeMax x Nothing = x | |
safeMax (Just x) (Just y) | size x > size y = Just x | |
safeMax (Just x) (Just y) = Just y | |
smallestLargestFiles path = foldl smallestLargest [Nothing, Nothing] (onlyFiles path) | |
where | |
smallestLargest [smallest, largest] file = do | |
let smallest' = safeMin smallest (Just file) | |
largest' = safeMax largest (Just file) | |
[smallest', largest'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment