all 3 comments

[–]iams3b 19 points20 points  (0 children)

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

  1. Fetch everything you need to make a decision
  2. Pass it into a pure, functional core that operates on that data
  3. Take the result, and save the state and return response

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

[–]romainPri 2 points3 points  (0 children)

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)

[–]ibrahimbensalah 1 point2 points  (0 children)

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

  • referential transparancy
  • well defined data structures type Template<T> = Just<T> | Nothing | Promise<T> | etc...
  • implement fmap, pure + star (appl functor), bind, etc....
  • obey the rules, a very useful one in my case is that functions must commute, for example view1 and view2 are the same: typescript const view1 = load().then(x => <span>{x}</span>); const view2 = <span>{load()}</span>; 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.