Think Like Functional Programmer Pt. 1

Tuesday, November 11, 2025

You often heard from the functional programmers that if you are coming from object-oriented or procedural programming background, then you need to shift the way you write the function programs. Of course, as you know one programming language, you can write programs in other programming language by using what you know. But, often times, programs written in this way are not efficient or effective.

The plan with this series is to literally show you some shift in how you can write programs in functional way. The language we will use are JavaScript and Elixir.

Consider the example, where you will get a text of few words and you need to print the caps of first word from the text. So, for example, if you get “Lorem ipsum delor sit” then you need to print LOREM as the result.

In JavaScript you can do it by,

// save text in variable
const text = "Lorem ipsum delor sit";

// split by space
const words = text.split(" ");

// get the first word
const word = words[0];

// and caps it
console.log(word.toUpperCase());

As we know have working solution, we can better write it as,

const text = "Lorem ipsum delor sit";
console.log(text.split(" ")[0].toUpperCase());

If we need to do the same thing in Elixir then we can map our JavaScript knowledge to Elixir and write similar code.

# save text in variable
text = "Lorem ipsum delor sit"

# split by space
words = String.split(text, " ")

# get the first word
word = Enum.at(words, 0)

# and caps it
IO.puts(String.upcase(word))

As we know have working solution, we can probably better write it as,

text = "Lorem ipsum delor sit"
IO.puts(String.upcase(Enum.at(String.split(text, " "), 0)))

As Elixir is functional programming language, we need to call functions! So, we calling functions within functions within functions that makes our code functional! No.

Functions are not just a syntax but they transform the input to generate the needed output. You need to think about input and output and output as input to other function. Nice thing about Elixir is that it has syntax that support such type of thinking. For example, here is Elixir way to write the same program but in actual function way.

"Lorem ipsum delor sit"
|> String.split(" ")
|> Enum.at(0)
|> String.upcase
|> IO.puts

Yay!

---