r/functionalprogramming • u/sahand-javid • Mar 25 '23
Question How to design an API using Functionally?
I’m trying to find what is norm in designing APIs using Functional Programming.
The way I usually see it is a Layered Architecture. We have Presentation, Application, Domain and Infrastructure layers. (May be Repository layer between Domain and Infrastructure as well).
The issue is the dependencies. From what I learned, The ideal approach would be define dependencies at the highest layer (Representation) and pass that down using Reader, Free Monad, Curried function or … .
The issue is, what if we have many many dependencies? Then we need to have the same dependencies drilled down all the way from top all the way to lowest layer?
What do you recommend? I’m pretty familiar with TypeScript. Do you know any good examples of such an api code in for example github that can connect to database, connect to external services using for example http, have loggers …?
20
u/iams3b Mar 25 '23
The thing you will see most often is the "imperative shell, functional core" or an "impure sandwich", where an outer layer that deals with IO and then a pure internal
Business logic goes into the functional core, all the IO stuff stays in the bread layer. This layer can still use classes, DI, or whatever you're comfortable with the outside, and the ingredients 🤌 is pure and testable
My favorite article I've read so far is this one: https://fullsteak.dev/posts/fullstack-rescript-architecture-overview It's written in rescript (great alternative to typescript!) but it goes over some pretty good ideas and includes code snippets that should be generally followable
3
u/romainPri Mar 25 '23
Actually working on that at the moment as well :)
What about a redux-like architecture ?
It favour unidirectionnal dataflow and immutability.
Side effect can be encapsulate into middleware or saga (redux-saga)
2
u/ibrahimbensalah Mar 25 '23
Great question! I myself am trying to answer the same question in my TypeScript project called xania. This is a general purpose UI library that must be composed to any level of complexity and there lies my motivation for FP,
So far I had incredible results having the fastest implementation in the JS Fx Benchmarks
The most important rules I try to follow are
type Template<T> = Just<T> | Nothing | Promise<T> | etc...I feel like most of the other patterns are implied automatically from the above. I explicitly excluded immutability from the list because it is confusing. People assume immutability is about not mutating data (I thought that was the case for years), but instead it is about structure preserving transformations. This does not make much sense for me more that referential transparancy.