Skip to content

Commit be1b9b9

Browse files
committed
Completed largestAndSmallest
1 parent 758d71e commit be1b9b9

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

chapter4/src/FileOperations.purs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ module FileOperations where
22

33
import Prelude
44
import Control.MonadZero (guard)
5-
import Data.Array (uncons, length, filter, concatMap, (:), (..))
6-
import Data.Array.Partial (tail, head)
5+
import Data.Array (snoc, uncons, length, filter, concatMap, (:), (..))
6+
import Data.Array.Partial (last, init, tail, head)
77
import Data.Foldable (foldl)
88
import Data.Maybe (Maybe(Just, Nothing))
9-
import Data.Path (Path, ls)
9+
import Data.Path (size, isDirectory, Path, ls)
1010
import Partial.Unsafe (unsafePartial)
1111

1212
allFiles :: Path -> Array Path
@@ -94,3 +94,17 @@ count p = count' 0
9494

9595
reverse :: forall a. Array a -> Array a
9696
reverse = foldl (\acc n -> n : acc) []
97+
98+
onlyFiles :: Path -> Array Path
99+
onlyFiles = filter (not isDirectory) <<< allFiles
100+
101+
largestAndSmallest :: Path -> Array Path
102+
largestAndSmallest = foldl check [] <<< onlyFiles
103+
where
104+
check [] path = [path, path]
105+
check acc path =
106+
if size path > size (unsafePartial head acc)
107+
then path : (unsafePartial tail acc)
108+
else if size path < size (unsafePartial last acc)
109+
then (unsafePartial init acc) `snoc` path
110+
else acc

0 commit comments

Comments
 (0)