DEV Community

Discussion on: Daily Challenge #210 - Separate Capitalization

Collapse
 
aminnairi profile image
Amin • Edited

Elm

Note: returning a tuple because returning a List String would mean it can be a list of 0 element as it can be a list of 10, 100, 1000, Infinity elements. Returning a Tuple gives the information to the user that this function will always return two elements in this case, which is suggested by the goal of this challenge. It will be then possible to extract them easily with the Tuple.first & Tuple.second functions.

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


odd : Int -> Bool
odd integer =
    not <| even integer


upperIf : ( Int -> Bool ) -> Int -> Char -> Char
upperIf predicate index character =
    if predicate index then
        Char.toUpper character

    else
        character


capitalizeIf : ( Int -> Bool ) -> String -> String
capitalizeIf predicate string =
    string
        |> String.toList
        |> List.indexedMap ( upperIf predicate )
        |> String.fromList


capitalize : String -> ( String, String )
capitalize string =
    ( capitalizeIf even string, capitalizeIf odd string ) 
Collapse
 
not_jffrydsr profile image
@nobody

This is a hallmark of proper functional programming. Elm looks intimidating than more traditional grammars but it's essentially strongly-typed function composition. I love it, and the outer function reads like english.

Collapse
 
aminnairi profile image
Amin • Edited

I won't lie, since this was my first real functional programming language, it was tough, really tough. And this is essentially due to the fact that I was not used to no parens (or not at the same position at least) and function composition mainly.

But functional programming is like a muscle. The best competitors are working hard to obtain a great physical condition. This is the same thing for people like you and me that are used to procedural or oriented-object programming. It requires a lot of practices to get out of our habits.

But when you practiced enough, you'll start to love functional programming and that is the only certainty you have. The rest is up to you. And actually, since I'm a Web Developer, and doing most of my stuff in JavaScript at work (I hope I get to include Elm one day) this has made me a better overall JavaScript developer. There is no time wasted on learning this language, trust me.