forked from paf31/purescript-book
-
Notifications
You must be signed in to change notification settings - Fork 195
Ch 7 unit tests pass #146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
oldfartdeveloper
wants to merge
8
commits into
purescript-contrib:master
from
oldfartdeveloper:ch7-only-solutions
Closed
Ch 7 unit tests pass #146
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e7b5a61
ch3 solutions
milesfrain 0484ea1
ch6 exercise solutions
milesfrain 59b97fd
Ch4 solutions (#107)
milesfrain 690b9d1
Ch 7 unit tests pass
oldfartdeveloper 8155725
Solutions for chapters 3,4,6 (other chapters in-progress)
milesfrain 678fe4a
Added explanatory comment to 'derive' statement
oldfartdeveloper ccf6e00
Replaced 1st unit test w/ conventional 'prove it works' test
oldfartdeveloper 520636c
Merge branch 'solutions' into ch7-only-solutions
oldfartdeveloper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,159 @@ | ||
module Test.Solutions where | ||
|
||
import Prelude | ||
import Test.Starter | ||
import Data.String.Pattern (Pattern(..)) | ||
import Data.Foldable (foldl) | ||
import Data.Int (rem, quot) | ||
import Data.Path (Path(), filename, isDirectory, ls, root, size) | ||
import Data.Array (cons, filter, head, last, length, tail, (:), (..)) | ||
import Data.Maybe (Maybe(..), fromMaybe, maybe) | ||
import Data.String.Common (split) | ||
import Data.Tuple (Tuple(..), snd) | ||
import Control.MonadZero (guard) | ||
import Data.Array (cons, filter, head, last, length, tail, (..)) | ||
import Data.Foldable (foldl) | ||
import Data.Int (rem, quot) | ||
import Data.Maybe (Maybe(..), fromMaybe, maybe) | ||
import Data.Path (Path(), filename, isDirectory, ls, root, size) | ||
import Data.String.Common (split) | ||
import Data.String.Pattern (Pattern(..)) | ||
import Data.Tuple (Tuple(..), snd) | ||
|
||
isEven :: Int -> Boolean | ||
isEven n = case n of | ||
0 -> true | ||
1 -> false | ||
_ -> isEven $ n - 2 | ||
|
||
oneIfEven :: Int -> Int | ||
oneIfEven n = if isEven n then 1 else 0 | ||
|
||
evenCount :: Array Int -> Int | ||
evenCount ints = evenCount' ints 0 | ||
where | ||
evenCount' :: Array Int -> Int -> Int | ||
evenCount' [] count = count | ||
|
||
evenCount' ints' count = evenCount' (fromMaybe [] (tail ints')) $ add count $ maybe 0 oneIfEven $ head ints' | ||
|
||
squared :: Array Number -> Array Number | ||
squared arr = map (\n -> n * n) arr | ||
|
||
keepNonNegative :: Array Number -> Array Number | ||
keepNonNegative arr = filter (\n -> n >= 0.0) arr | ||
|
||
infix 4 filter as <$?> | ||
|
||
keepNonNegativeRewrite :: Array Number -> Array Number | ||
keepNonNegativeRewrite arr = (\n -> n >= 0.0) <$?> arr | ||
|
||
factors :: Int -> Array (Array Int) | ||
factors n = do | ||
i <- 1 .. n | ||
j <- i .. n | ||
guard $ i * j == n | ||
pure [ i, j ] | ||
|
||
isPrime :: Int -> Boolean | ||
isPrime n = eq 1 $ length $ factors n | ||
|
||
cartesianProduct :: ∀ a. Array a -> Array a -> Array (Array a) | ||
cartesianProduct left right = do | ||
a_ <- left | ||
b_ <- right | ||
[ [ a_, b_ ] ] | ||
|
||
triples :: Int -> Array (Array Int) | ||
triples n = do | ||
i <- 1 .. n | ||
j <- i .. n | ||
k <- j .. n | ||
guard $ i * i + j * j == k * k | ||
pure [ i, j, k ] | ||
|
||
-- | Provide the prime numbers that, multiplied together, make the argument. | ||
factorizations :: Int -> Array Int | ||
factorizations n = factorizations' 2 n [] | ||
where | ||
factorizations' :: Int -> Int -> Array Int -> Array Int | ||
factorizations' _ 1 result = result | ||
|
||
factorizations' divisor dividend result = | ||
let | ||
remainder = rem dividend divisor | ||
in | ||
if remainder == 0 then | ||
factorizations' (divisor) (quot dividend divisor) (cons divisor result) | ||
else | ||
factorizations' (divisor + 1) dividend result | ||
|
||
allTrue :: Array Boolean -> Boolean | ||
allTrue bools = foldl (\acc bool -> acc && bool) true bools | ||
|
||
exclusiveOrThenTrue :: Array Boolean -> Boolean | ||
exclusiveOrThenTrue bools = foldl (==) false bools | ||
|
||
-- | The fib routine in tail recursive form | ||
fib :: Int -> Int | ||
fib n = fib' n 0 0 1 | ||
where | ||
fib' :: Int -> Int -> Int -> Int -> Int | ||
fib' limit count n1 n2 = | ||
if limit == count then | ||
n1 + n2 | ||
else | ||
fib' limit (count + 1) (n1 + n2) n1 | ||
|
||
reverse :: ∀ a. Array a -> Array a | ||
reverse = foldl (\xs x -> [ x ] <> xs) [] | ||
|
||
-- Section for : A Virtual Filesystem exercise | ||
allFiles :: Path -> Array Path | ||
allFiles file = | ||
file | ||
: do | ||
child <- ls file | ||
allFiles child | ||
|
||
onlyFiles :: Path -> Array Path | ||
onlyFiles p = filter (\p' -> not $ isDirectory p') $ allFiles p | ||
|
||
maxSigned32BitInt :: Int | ||
maxSigned32BitInt = 2147483647 | ||
|
||
largestSmallest :: Path -> Array (Tuple String Int) | ||
largestSmallest path = largestSmallestPaths (allFiles path) | ||
where | ||
largestSmallestPaths :: Array Path -> Array (Tuple String Int) | ||
largestSmallestPaths paths = [ outlier (\i j -> i > j) 0 paths, outlier (\i j -> i < j) maxSigned32BitInt paths ] | ||
where | ||
outlier :: (Int -> Int -> Boolean) -> Int -> Array Path -> Tuple String Int | ||
outlier criteria startValue paths' = | ||
foldl | ||
( \acc p' -> | ||
( case size p' of | ||
Just n -> if criteria n $ snd acc then Tuple (filename p') n else acc | ||
Nothing -> acc | ||
) | ||
) | ||
(Tuple "" startValue) | ||
paths' | ||
|
||
allSizes :: Array Path -> Array (Tuple String Int) | ||
allSizes paths = | ||
map | ||
( \p -> case size p of | ||
Just n -> Tuple (filename p) n | ||
Nothing -> Tuple (filename p) 0 | ||
) | ||
paths | ||
|
||
whereIs :: String -> Maybe String | ||
whereIs fileName = head $ whereIs' $ allFiles root | ||
where | ||
whereIs' :: Array Path -> Array String | ||
whereIs' paths = do | ||
path <- paths | ||
child <- ls path | ||
guard $ eq fileName $ fromMaybe "" $ last $ split (Pattern "/") $ filename child | ||
pure $ filename path |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't seem like this is rebased correctly on the latest solutions (or master) branch, since there shouldn't be any changes here outside of ch7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I've noticed (and wondered about) that as well. It's become tricky.