paulfioravanti's solution

to Strain in the Elm Track

Published at Jun 30 2019 · 0 comments
Instructions
Test suite
Solution

Implement the `keep` and `discard` operation on collections. Given a collection and a predicate on the collection's elements, `keep` returns a new collection containing those elements where the predicate is true, while `discard` returns a new collection containing those elements where the predicate is false.

For example, given the collection of numbers:

• 1, 2, 3, 4, 5

And the predicate:

• is the number even?

Then your keep operation should produce:

• 2, 4

• 1, 3, 5

Note that the union of keep and discard is all the elements.

The functions may be called `keep` and `discard`, or they may need different names in order to not clash with existing functions or concepts in your language.

Restrictions

Keep your hands off that filter/reject/whatchamacallit functionality provided by your standard library! Solve this one yourself using other basic tools instead.

Elm Installation

Refer to the Installing Elm page for information about installing elm.

Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

Execute the tests with:

``````\$ elm-test
``````

Automatically run tests again when you save changes:

``````\$ elm-test --watch
``````

As you work your way through the test suite, be sure to remove the `skip <|` calls from each test until you get them all passing!

Source

Conversation with James Edward Gray II https://twitter.com/jeg2

Submitting Incomplete Solutions

It is possible to submit an incomplete solution so you can see how others have completed the exercise.

Tests.elm

``````module Tests exposing (even, isFirstLetter, lessThanTen, odd, tests)

import Expect
import String
import Test exposing (..)

even : Int -> Bool
even number =
modBy 2 number == 0

odd : Int -> Bool
odd number =
modBy 2 number == 1

isFirstLetter : String -> String -> Bool
isFirstLetter letter word =
String.left 1 word == letter

lessThanTen : Int -> Bool
lessThanTen num =
num < 10

tests : Test
tests =
describe "Strain"
[ test "empty keep" <|
\() ->
Expect.equal []
(keep lessThanTen [])
, skip <|
test "keep everything" <|
\() ->
Expect.equal [ 1, 2, 3 ]
(keep lessThanTen [ 1, 2, 3 ])
, skip <|
test "keep first and last" <|
\() ->
Expect.equal [ 1, 3 ]
(keep odd [ 1, 2, 3 ])
, skip <|
test "keep nothing" <|
\() ->
Expect.equal []
(keep even [ 1, 3, 5, 7 ])
, skip <|
test "keep neither first nor last" <|
\() ->
Expect.equal [ 2 ]
(keep even [ 1, 2, 3 ])
, skip <|
test "keep strings" <|
\() ->
Expect.equal [ "zebra", "zombies", "zealot" ]
(keep (isFirstLetter "z") [ "apple", "zebra", "banana", "zombies", "cherimoya", "zealot" ])
, skip <|
\() ->
Expect.equal []
, skip <|
\() ->
Expect.equal []
(discard lessThanTen [ 1, 2, 3 ])
, skip <|
test "discard first and last" <|
\() ->
Expect.equal [ 2 ]
(discard odd [ 1, 2, 3 ])
, skip <|
\() ->
Expect.equal [ 1, 3, 5, 7 ]
(discard even [ 1, 3, 5, 7 ])
, skip <|
test "discard neither first nor last" <|
\() ->
Expect.equal [ 1, 3 ]
(discard even [ 1, 2, 3 ])
, skip <|
\() ->
Expect.equal [ "apple", "banana", "cherimoya" ]
(discard (isFirstLetter "z") [ "apple", "zebra", "banana", "zombies", "cherimoya", "zealot" ])
]``````
``````module Strain exposing (discard, keep)

keep : (a -> Bool) -> List a -> List a
keep predicate list =
case list of

else
keep predicate tail

[] ->
[]

discard : (a -> Bool) -> List a -> List a
list
|> keep (not << predicate)``````