This is why you should learn a functional programming language

Arturo Jiménez
Jul 25, 2018 · 3 min read

Functional Programming features

// Kotlin val double = fun(x: Int): Int { return x * 2 } 
# Elixir double = fn(x) -> x * 2 end
// Kotlin 
val benchmark = fun(startInMilliseconds: Int): (Int) -> Int {
return fun(endInMilliseconds: Int): Int {
return endInMilliseconds - startInMilliseconds
}
}
val timeLapse = benchmark(1000)
timeLapse(5000) // result: 4000
# Elixir
benchmark = fn(start_milliseconds) ->
fn(end_milliseconds) ->
end_milliseconds - start_milliseconds
end
end
// Kotlin fun printValue(x: Int) { println(x) } # Elixir def print_value(x), do: IO.inspect(x+1)
// Kotlin 
val benchmark = fun(startInMilliseconds: Int): (Int) -> Int {
return fun(endInMilliseconds: Int): Int {
return endInMilliseconds - startInMilliseconds
}
}
val timeLapse = benchmark(1000)
timeLapse(5000) // result: 4000
# Elixir
benchmark = fn(start_milliseconds) ->
fn(end_milliseconds) ->
end_milliseconds - start_milliseconds
end
end
// Kotlin 
val person = Person("John Doe")
person.name = "Not John Doe" // Fails. Val cannot be reassigned.
# Elixir
map_set = MapSet.new
foo_set = MapSet.put(map_set, "foo")
# map_set = MapSet<[]>
# foo_set = MapSet<["foo"]>

Object-oriented Programming VS Functional Programming: fight!


Arturo Jiménez

Written by

Android Developer at FREE NOW (formerly mytaxi). Software Craftsman.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade