r/functionalprogramming • u/Tecoloteller • Aug 02 '25
Question Strategies for Functional Programming in IO-heavy Programs?
Hi all, long time appreciator of this sub! Ive studied some type theory and tried learning functional programming independently. One question that persists is how do you keep with the functional paradigm when the program you're working with involves lots of IO at many stages of the process and the spaces between where everything is just transforming data are relatively thin?
For context, I was helping some coworkers with a Python SDK wrapping some of our backend APIs. Many of their functions required hitting an end point a couple times, short bits of operating on the results, then sending those results to other APIs, rinse and repeat in sequence. From reading Frisby's Guide to Functional Programming in JavaScript (feel free to toss some recs if you want), I know you can use lazy evaluation and monads to manage IO and put off execution/side effects, but since a lot of the code is gluing together API calls it feels like this could get unwieldy quickly.
What do y'all usually do to manage highly side effectful code in a functional style? Is this a "don't let a paradigm get in the way of doing the task" kind of situation? Is this when you have to pay the cost of a decent bit of boilerplate in exchange for keeping with a functional style (lots of monads, handling nested monads, chaining, etc)? Feel free to cite/link to specific examples or libraries, I do want to learn general strategies to the problem however for when I'm in a language like Python or Go where functional libraries solving this problem may not be prevalent.
17
u/yakutzaur Aug 02 '25
Maybe I'm missing something, but I have some decent amount of experience with Haskell and PureSctipt, and a lot of experience with Python and Java and still don't understand the issue. When you need IO in Haskell or PureSctipt, you just do it in corresponding IO monad. IO just becomes explicit.
4
u/_lazyLambda Aug 02 '25
I have worked in what i will bet a decent amount of money on, is one of the most overcomplicated systems a dev has ever seen and you may be right about your analysis but at a deeper level, does it neeeeeed to be that IO heavy?
Heavy is the word im fixated on because its not a simple tiny app where there's just not much at all to do. But there is a lot of processing.
Which is why im projecting that its similar to my experience at that job. So in that job there was a heavy amount of IO too but it was for the wrong reasons. We had over 300 different jobs running in the background and lets pretend they all just increment some input number by 1 (for illustration purposes)
f1 ==> f2 ==> f3 ....
And in this company it would increment 1 and then write it to file, write it to database, etc etc
We needed to look at it and say hmmm we start with an input of 1 and then we dont really do anything interesting or necessary IO wise (like message a user) until f67 is called. Which means that we could easily just chain the logic of f1 through f67 together and instead of input ==> Output ==> input for passing data along, then we can just simply call f1 through f67 as one pure function. In our example with incrementing we'd just output 67 with the original input of 1 instead of serializing and deserializing
6
u/minus-one Aug 02 '25 edited Aug 02 '25
we put all side effects into Observables (rxjs, typescript, client side, should work server side too, but no experience)
Observables are pure (you never subscribe! (well maybe once, to run the app)) and composable (bc they’re a functor and a monad). idk maybe server side you have it easy way, but effects themselves can be highly complicated, huge nested chains with merges, forkJoins, parallel, sequential etc it simply not possible to make them composable in any other way (especially imperative)
and ofc, there is a lot of pure functions/transformations gluing them together
3
u/kbielefe Aug 03 '25
Side effects as data is a really powerful concept. I'm most familiar with functional Scala due to my day job, so as one example I'll point you to a Scala library called fetch.
With fetch, you build up a description of the data you need, and the library deduplicates, batches, and caches the requests. You actually perform the side effects at one place.
You can't do that with imperative-style effects, where you typically run effects as soon as they are defined. That feels easier because you don't have to think about the difference between defining an effect and running it, but it's really very limiting.
FP effects feel like unnecessary overhead when you try to directly transfer imperative patterns to it, but imperative effects feel very clunky once you get used to patterns that are only feasible in functional style.
3
u/garethrowlands Aug 04 '25
Sometimes, a problem is just so IO heavy that it’s going to look like a shell script - it can be best not to worry too much about that.
But one thing that sometimes helps is separating the code that does the IO from the code that decides what needs to happen. That’s natural in Haskell, since IO can call pure functions and not vice versa (so sorry if this is obvious). It’s sometimes known as Functional Core, Imperative Shell. You would typically have a pure function returning a result to the function that runs in IO. You can think of the imperative shell as an interpreter of those results - again, a well known pattern.
2
u/thatdevilyouknow Aug 04 '25
I can provide a concrete example from a project I completed which involves some formal verification and a modernization of some code originally developed at Inria. It is written in F# but incorporates it's own Fiber class and in this example I use Atoms inspired by Clojure. This web server uses no primitives from C# it is F# completely from handling sockets to handling buffers so there is no MS provided web framework as the purpose of it was to compile to a static binary with .NET Native which could simply be copied from machine to machine without installing .NET at all but I digress. Clojure based atoms are used to handle references to incoming streams using a CAS architecture:
An Atom here is holding a reference to a Fiber, which is a continuation, and so for streams we hold the reference and swap them for incoming web requests which are I/O bound.
Atom has a very simple definition along with swap because we just compare and exchange interlock. Previously, a spinlock was used but that has been refactored out.
And it is worth mentioning within CopyTo, which is implemented locally for streams, it contains the following code:
Memory here is "rented" so it can placed back into the ArrayPool as bytes are processed and Span<bytes> is used everywhere it can be in other places. Putting it all together for processing I/O bound streams this atomically handles references and defines the lifetimes of these references using FP. It is really no different than what Clojure would do such as:
1
u/complyue Aug 03 '25
By posting ADTs around, those invoking Algebraic Effects? API services would become just Effect Hosts :D
Web services are already designed this way, more or less, just seldom implemented in strict/formal PLs for soundness check.
0
u/[deleted] Aug 02 '25
[deleted] — view deleted comment