Skip to content

Instantly share code, notes, and snippets.

@stevenproctor
Last active March 2, 2016 15:39
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 stevenproctor/135be29e7f1d457ab3ec to your computer and use it in GitHub Desktop.
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.
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