DEV Community

loading...

Discussion on: Daily Challenge #66- Friend List

Collapse
aminnairi profile image
Amin

My take at the challenge written in Elm

Source-Code

$ touch src/FriendList.elm
module FriendList exposing (friendList)


surround : String -> String -> String -> String
surround left right string =
    left ++ string ++ right


toUpperTuples : String -> String
toUpperTuples =
    String.split ":"
        >> List.reverse
        >> String.join ", "
        >> surround "(" ")"
        >> String.toUpper


friendList : String -> String
friendList =
    String.split ";"
        >> List.map toUpperTuples
        >> List.sort
        >> String.join ""

Unit test source-code

$ touch tests/FriendListTest.elm
module FriendListTest exposing (suite)

import Expect
import FriendList exposing (friendList)
import Test exposing (Test)


suite : Test
suite =
    Test.describe "Friend list"
        [ Test.test "It should return the correct friend list" <|
            \_ ->
                let
                    expectations =
                        "(CORWILL, ALFRED)(CORWILL, FRED)(CORWILL, RAPHAEL)(CORWILL, WILFRED)(TORNBULL, BARNEY)(TORNBULL, BETTY)(TORNBULL, BJON)"

                    friends =
                        "Fred:Corwill;Wilfred:Corwill;Barney:Tornbull;Betty:Tornbull;Bjon:Tornbull;Raphael:Corwill;Alfred:Corwill"

                    result =
                        friendList friends
                in
                Expect.equal expectations result
        ]