Intro to Functional Programming, With Emojis πŸ’°πŸΊπŸ”₯

by

Preface: The intention of this article is to provide an interesting and simplified starting point for introducing someone to functional programming topics. It is not intended to be a mathematically provable language or even a full featured programming language.

Before we begin discussing functional programming, we should first define our emoji language. Functions (which are very important in FUNCTIONal programming) in our language look like this:

functionName : parameter β†’ result
goToWork : ⏰ β†’ πŸ’°
goToTheBar :Β πŸ’°Β β†’Β πŸΊ
makePopcorn : πŸ”₯ β†’ 🌽 β†’ 🍿

The function goToWork takes ⏰ as a parameter and returns πŸ’°. goToTheBar takes πŸ’° as a parameter and returns 🍺. makePopcorn accepts two parameters, πŸ”₯ and 🌽, then returns 🍿.

One important feature of functional languages, is the separation of data from it’s behavior. In other languages, especially object oriented ones, data and behavior are often tightly coupled. Lets take a look at an example:

πŸ“­.sendMail(βœ‰οΈ)
πŸ“­.hasMail() β†’ βœ…

In the non-functional language example above, the functions sendMail and hasMail are part of the mailbox data-structure itself. A functional equivalent might look like this:

sendMail : πŸ“­ β†’ βœ‰οΈ β†’ πŸ“¬
hasMail : πŸ“¬ β†’ βœ…

Our functional equivalents of sendMail and hasMail take the πŸ“­ and βœ‰οΈ as parameters, rather than being part of the πŸ“­.

Many functional languages have immutable variables and values. Immutable values cannot be changed once they are created. They can be used in functions, used to calculate new values, and even “modified”. However the original value will always remain as it was.

myCorn = 🌽
makePopcorn : 🌽 β†’ 🍿
myPopcorn = makePopcorn myCorn
myPopcorn β†’ 🍿
myCorn β†’ 🌽

In the above example we declare a variable myCorn and pass it to the function makePopcorn. In the real world heating up corn mutates it and turns it into popcorn. In our functional language myCorn is immutable remaining as 🌽 after being passed to makePopcorn.

Functional languages support passing functions as parameters. Passing functions as arguments doesn’t automatically call the function, you’re actually passing a reference to the function. map is a function that takes function and a list as parameters. It then passes every value in the list to the function, then returns a list of the results.

iHateCoconut : πŸ₯₯ β†’ πŸ₯
map iHateCoconut [πŸ₯₯, πŸ₯₯, πŸ₯₯, πŸ₯₯, πŸ₯₯] β†’ [πŸ₯, πŸ₯, πŸ₯, πŸ₯, πŸ₯]

In the above example we use map and the function iHateCoconut to turn a list of πŸ₯₯ into a list of πŸ₯.

One of the draws of functional programming is that you can treat functions like Legos, putting them together to create completely new structures. This is called function composition. This is so common in functional languages that some include a built in operator to make the process easier.

badJoke :Β πŸ˜€ β†’ 😐
scaryStory : 😐 β†’ 😱
writeBlogPost = badJoke >> scaryStory
writeBlogPost : πŸ˜€ β†’ 😱

The final functional programming topic I’d like to cover in this article is partial application. Partial application is when you pass a function less parameters than it needs. This creates a new function that takes the remaining parameters, but has the initial parameters fixed.

combineThings : πŸ…°οΈ β†’ πŸ…±οΈ β†’ πŸ†Ž
cook = combineThings πŸ”₯
cook 🌽 β†’ 🍿
cook πŸ₯” β†’ 🍟

Above we have a function combineThings that takes two things of different types and then combines them. We partially apply combineThings with πŸ”₯ to create a new function cook. We can then use the new cook function to cook various different foods.

I encourage everyone to give a functional language a try at least once in their programming careers. Even if you find the style is not for you, it will change and improve the way you think about programming problems. If you’re looking for a language, I recommend Elm. It is fairly easy for beginners to pick up and has an active and helpful community. Additionally, the language used in this article is heavily inspired by Elm.

1 Comment

Leave a Reply to Alex Cancel reply

Your email address will not be published. Required fields are marked