Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of rootulp

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

While your discard operation should produce:

  • 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 Strain exposing (discard, keep)
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 <|
            test "empty discard" <|
                \() ->
                    Expect.equal []
                        (discard lessThanTen [])
        , skip <|
            test "discard everything" <|
                \() ->
                    Expect.equal []
                        (discard lessThanTen [ 1, 2, 3 ])
        , skip <|
            test "discard first and last" <|
                \() ->
                    Expect.equal [ 2 ]
                        (discard odd [ 1, 2, 3 ])
        , skip <|
            test "discard nothing" <|
                \() ->
                    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 <|
            test "discard strings" <|
                \() ->
                    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",
    "license": "BSD3",
    "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
discard predicate collection =
    case collection of
        [] -> []
        comparable::rest ->
            if not (predicate comparable) then
                comparable :: discard predicate rest
            else
                discard predicate rest

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of petemcfarlane

Do you think there is some duplication between the keep/discard methods? Can you write one in terms of the other?

What can you learn from this solution?

A huge amount can be learned from reading other peopleโ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?