r/functionalprogramming • u/aviboy2006 • Mar 06 '26
Intro to FP My clean code habits were actually State Monads
Reading Functional Programming in Scala. This is my second post here to share what I am learning.
I have been a full-stack developer for 15 years, mostly using OOP and imperative styles. I always tried to avoid global state because it makes code hard to debug. My solution was usually to pass a "context" or "config" object through my functions to keep things organised.
When I reached the chapter on State, I realised that what I was doing manually has a formal name: the State Monad.
A simple code example:
Imagine we are counting how many times a user clicks a button.
The Imperative Way (Hidden changes): In this version, the function reaches outside of itself to change a variable. This is a "side effect.":
count = 0
def increment():
global count
count += 1
return "Clicked!"
# You call it, and the 'count' variable changes in the background.
result = increment()Functional Style (The State pattern):
def increment_pure(current_count):
new_count = current_count + 1
return ("Clicked!", new_count)
# You call it, and you get back the result AND the new state.
result, final_count = increment_pure(0)
# Usage
result, final_state = add_user_pure(initial_db_state, "Avinash")What I learned from this:
- Honest Functions: In the first example, you don't know the function changes anything just by looking at its name. In the second example, the code tells you exactly what it needs and what it gives back.
- Easier Testing: I don't need to "setup" a global variable to test my code. I just give the function a number and check if it adds 1 correctly.
- Safe for Growth: When your app gets big, having functions that don't touch global data makes it much harder to break things by accident.
It is interesting to see that "best practices" I learned through trial and error are actually core principles in functional programming. I am still learning.
For those who moved from OOP to FP What other patterns were you already using before you knew the formal FP name for them?
41
u/WhiskyStandard Mar 06 '26 edited Mar 06 '26
I started writing service classes that wrapped DBs and remote API calls and noticed that they pretty much didn’t have any mutable properties. The methods would do unsafe things, but only because of the lower level calls they made.
I realized that
new FooService(host, apiKey).checkout(shoppingCart)was pretty much the justfooCheckout(host, apiKey, shoppingCart)with the ability to partially evaluate a limited subset of the terms. AfooServiceobject and a function that could takeshoppingCartwere pretty much the same thing, especially if you were properly segmenting interfaces.I started calling classes degenerate functions and people stopped talking to me.
11
u/kbielefe Mar 06 '26
The state monad takes this one step further and basically automatically transforms the first example into the second one internally. You assemble a state object that represents changes to state, then run it, providing the initial state.
7
u/bigkahuna1uk Mar 06 '26
You're example is not a monad at all. It doesn't obey the monadic rules. Unless I'm missing something, isn't this just a pure function?
15
u/teckhooi Mar 06 '26 edited Mar 06 '26
What you described was not a State Monad. It is a pure function e.g.
def evenOnly (x, xs) = if x % 2 ==0 then (x, x::xs) else (x, xs)State Monad supportsbindandmap. Reading the book further will explain State Monad and how to use it2
u/Datamance Mar 06 '26
There’s an ancient saying about this - “Classes are a poor man’s closure”
2
u/simon-or-something Mar 07 '26
Isnt this what Go does too? Returning errors / success as values from function calls? The OOP equivalent would probably be dependency injection or something
2
u/sent1nel Mar 06 '26 edited Mar 08 '26
I really like EitherT[F, Throwable, Option[Thing]] for effectful functions that either return something or throw an error. They’re super useful for combining effectful operations and managing what happens when you have to deal with errors.