Introduction to Functional Programming
The magical approach!
Introduction to Functional Programming
The magical approach!
What is Functional Programming?

Functional Programming
At its core, functional programming (FP) is a paradigm where computation is treated as the evaluation of mathematical functions, without changing state or mutating data. In contrast to imperative programming, where instructions modify the program’s state step-by-step, FP emphasizes immutability and declarative expressions. In an FP approach, functions are “first-class citizens,” meaning they can be passed around as arguments, returned from other functions, and assigned to variables.
let’s analyze a very simple Fibonacci implementation Without FP
def fibonacci(n: Int): Int = {
if (n <= 1) n
else {
var a = 0
var b = 1
for (i <- 2 to n) {
val sum = a + b
a = b
b = sum
}
b
}
}
with FP
def fibonacci(n: Int): Int = n match {
case 0 => 0
case 1 => 1
case _ => fibonacci(n - 1) + fibonacci(n - 2)
}
Therefore you can say that Functional Programming is :

Functional programming depends on abstract mathematical structures that allow for composable and structured computations. They are rooted in category theory and are powerful tools for managing effects, combining data, and transforming values within contexts, since this is about Magic, let's talk about the spells.
Monoid: The Spell of Combination
Imagine you have a spell that can combine two magical items into one. A Monoid is a mystical entity that follows two rules:
- Combine two items: You have a spell called combine that knows how to merge two items together. Example: Combining two magical runes to create a stronger rune.
- An empty item: There’s a neutral, or “identity,” item called empty that doesn’t change anything when combined. Example: You have a rune that doesn’t affect any spell when combined with other runes.
In code terms, if you think of numbers, the + operator can be a combining spell, and the number 0 is the empty item. If you’re combining lists, the empty list [] is the neutral item, and concatenation is the spell.
Key Magic Rules: You can combine items in any order (it’s associative), and using the neutral item doesn’t change the result.
Functors: The Spell of Transformation
Next up, let’s cast a transformation spell on items inside a magical container! A Functor is a container (like a magic chest) that holds items, and it allows you to apply a spell (a function) to each item inside the container without opening it.
In coding terms, a List is a functor: if you have a list of numbers [1, 2, 3], you can apply a transformation spell like +1 to get [2, 3, 4]. The spell doesn’t change the list itself, but it alters everything inside it.
Key Magic Power: You can map a function (your spell) over the contents of a container, and the magic will apply to each item.
Monad: The Spell of Sequencing
Finally, we come to the most mysterious spell, Monad. A Monad is like a wizard who knows how to chain spells together in the proper order, even if each spell produces new magical items or new containers of magic.
In coding terms, a Monad allows you to:
- Wrap something in a magical container. Example: You wrap a number inside a Maybe box, which might contain a value or nothing at all.
- Chain spells: You can combine actions (or functions) that return containers, making sure they run in the correct sequence. Example: You cast a spell to fetch a value from the Maybe container, and then cast another spell based on what comes out. Monads allow you to work with computations that might have side effects (like handling errors, doing IO, etc.) in a clean and controlled way.
Key Magic Power: You can chain operations that involve magical containers while keeping the magic intact and in sequence (the famous flatMap or bind spell in programming).
Since with great power comes great responsibility, Let’s talk about the rules!

- No Side Effects, In an ideal world, when you cast a spell, it always does one thing and one thing only. You know exactly what will happen every time. This is called pure magic — spells that are predictable, reliable, and have no side effects.

- Pure Functions: Functions produce the same output for the same input, and do not have side effects like changing global variables or modifying input arguments.

def add(a: Int, b: Int): Int = a + b
val result = add(3, 4) // Result: 7
- Immutability: Data does not change after it’s created. Instead, new data is derived from existing data.

val numbers = List(1, 2, 3)
// Trying to modify a list element would not work as the list is immutable.
// Instead, you create a new list with the desired modification.
val newNumbers = numbers.map(_ + 1) // Result: List(2, 3, 4)
- First-Class Functions: Functions can be assigned to variables, passed as parameters, and returned from other functions.

val double = (x: Int) => x * 2
def operateOnList(numbers: List[Int], operation: Int => Int): List[Int] = {
numbers.map(operation)
}
val result = operateOnList(List(1, 2, 3), double) // Result: List(2, 4, 6)
- Higher-Order Functions: Functions that take other functions as arguments or return them as results.

You can think of, logic can be transferred like an airplane carrying another airplane.
def applyFunctionTwice(f: Int => Int, x: Int): Int = f(f(x))
val increment = (x: Int) => x + 1
val result = applyFunctionTwice(increment, 5) // Result: 7
- Declarative Style: Describing what should be done rather than how it should be done.

Imperative:
var doubled = List[Int]()
for (n <- numbers) {
doubled = doubled :+ (n * 2) // imperative and mutable
}
Declarative:
val numbers = List(1, 2, 3, 4, 5)
val doubled = numbers.map(_ * 2) // declarative
- Function Composition: Combining simple functions to build more complex operations.

So we can conclude that Functional Programming is :

메타데이터
- post_id
- dc71f6120684
- slug
- introduction-to-functional-programming-dc71f6120684
- url
- https://medium.com/@jorgegfx/introduction-to-functional-programming-dc71f6120684
- canonical_url
- https://medium.com/@jorgegfx/introduction-to-functional-programming-dc71f6120684
- author_url
- https://medium.com/@jorgegfx
- status
- ok
- fetched_at
- 2026-06-27 07:40:21