r/functionalprogramming • u/spacepopstar • Jun 02 '23
Question Edge Cases Requiring State
I am coming up against this problem a lot at work, where I design a solution that gets very close to being a pure function. Then as development continues there is an edge case that really breaks the functional nature of my solution. How do you handle such cases smoothly?
For example a function that takes a list of numbers and prints them. Except if the number is “3”. If the number is 3, I need to print something about the callers state, like a thread ID.
5
Upvotes
7
u/Migeil Jun 02 '23
If you're printing numbers, you don't have a pure function.
I don't know what else you can do other than "if x == 3 then doSomethingWith3() else print(x)"
2
u/redchomper Jun 04 '23
To be quite honest, and I'm going a bit out on a limb here, I think the answer is to buckle down and change the signature of your function -- along with whatever else that may entail.
Too often I see a junior dev act like whatever a senior wrote is some golden truth that cannot be violated, so they endeavor to change as little as possible. But that's completely wrong. The senior dev just wrote according to his or her best understanding at the time, in full confidence that things will change whenever the understanding does. Now you have that understanding. If you need state, add state. (Use a monad if that makes you happy.) If you need another parameter, add that other parameter. If you need experience, make mistakes.
Oh, and if you're unsure of yourself, ask a lead for advice. They'll probably tell you the same thing, but they might know something special about your particular code-base.
2
u/unqualified_redditor Jun 03 '23 edited Jun 03 '23
I don't think I understand the question. You can model state purely with this type:
With the appropriate functor, applicative, monad implementations and related combinators you can use this to implicitly thread your state along your subroutines and get/set the state as needed.
You can also do the same thing by explicitly passing your state into your subroutines.
Given that state itself can be modeled purely, I don't see how there can be any edge cases caused by state management.
In your printer example you don't even need any fancy machinery for modifying state. You just need a function that receives your state: