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
View all comments
Show parent comment
2
u/anton_pechenin Jun 02 '23 edited Jun 02 '23
If i got you right you are having some root function that process list, apply some transformation to it and require some additional data for transformation. So the exactly place for transformation function depends on context) Either additional data is available during the execution of root function or not. If yes i would just placed transformation into the root function. In other hand monad combination approach looks suitable. Let s imagine you read items from database and such operation might be asynchronous. So you have some monad as result Monad[List[A]] And for transformation list you have to download some data from web. As result of second operation you got the Monad[SomeData]] Monads are stuff designed for combining via bind method As a consequence you might write something like yourListMonad.bind{list => youdDataMonad.map{ data => List and data } }
and you will get new monad with transformation)
Sorry for my English (