# rootulp's solution

## to Strain in the Elm Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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 Exercism help page for Elm installation and learning resources.

## Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed.

``````\$ elm-package install --yes
``````

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's possible to submit an incomplete solution so you can see how others have completed the exercise.

### Tests.elm

``````module Tests exposing (..)

import Expect
import String
import Test exposing (..)

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

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

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

lessThanTen : comparable -> 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" ])
]``````

### elm-package.json

``````{
"version": "3.0.0",
"summary": "Exercism problems in Elm.",
"repository": "https://github.com/exercism/elm.git",
"source-directories": [
".",
".."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "5.0.0 <= v < 6.0.0",
"elm-community/elm-test": "4.0.0 <= v < 5.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}``````
``````module Strain exposing (..)

type alias Predicate comparable = (comparable -> Bool)

keep: Predicate comparable -> List comparable -> List comparable
keep predicate collection =
case collection of
[] -> []
comparable::rest ->
if predicate comparable then
comparable :: keep predicate rest
else
keep predicate rest

discard: Predicate comparable -> List comparable -> List comparable
case collection of
[] -> []
comparable::rest ->
if not (predicate comparable) then
else