DEV Community

Discussion on: Daily Challenge #71 - See you next Happy Year

Collapse
 
aminnairi profile image
Amin • Edited

Elm

module NextHappyYear exposing (nextHappyYear)

import List exposing (length)
import String exposing (any, fromChar, fromInt, indexes)


isMultiple : List anything -> Bool
isMultiple list =
    length list > 1


nonUniqueCharacterIn : String -> Char -> Bool
nonUniqueCharacterIn string character =
    indexes (fromChar character) string
        |> isMultiple


nextHappyYear : Int -> Int
nextHappyYear year =
    let
        nextYear : Int
        nextYear =
            year + 1

        yearString : String
        yearString =
            nextYear
                |> fromInt

        nonHappyYear : Bool
        nonHappyYear =
            yearString
                |> any (nonUniqueCharacterIn yearString)
    in
    if nonHappyYear then
        nextHappyYear nextYear

    else
        nextYear

Tests

module NextHappyYearTest exposing (suite)

import Expect exposing (equal)
import NextHappyYear exposing (nextHappyYear)
import Test exposing (Test, describe, test)


suite : Test
suite =
    describe "Next happy year"
        [ test "It should return 7712 when passing 7801" <|
            \_ ->
                equal 7801 <| nextHappyYear 7712
        , test "It should return 1001 when passing 1023" <|
            \_ ->
                equal 1023 <| nextHappyYear 1001
        , test "It should return 2019 when passing 2018" <|
            \_ ->
                equal 2019 <| nextHappyYear 2018
        ]

Edit

Just realized there is a native Set module in Elm by looking at the comments made in JavaScript which is awesome, but I'll keep this as is for the record. I'm edgy anyway!

Edit2

Turns out, after an interesting talk with the folks at the Elm Slack, and some experimentations, there is no way to to Set that keeps the order of the List due to the Haskell implementation behind, preventing to have an easy solution. So this means either use an extra library (which I tend to avoid since I wanted to show a native Elm solution) or use a combination just like I did!