r/functionalprogramming • • Feb 28 '23

Question Is JSX a hidden monad bind?

Say we have this example

function Component() {
   return <Layout><Page /></Layout>
}

And lets assume this is compiled to

function Component() {
   return jsx(Layout, jsx(Page));
}

where type of jsx is (fn, JSX.Element) => JSX.Element. For simplicity ignore that we can have multiple elements in the second argument.

jsx is not in the form of monadic bind (JSX.Element, (fn) => JSX.Element) => JSX.Element. If we ignore the laws, can we say jsx is a special case of the monadic bind function nevertheless?

15 Upvotes

25 comments sorted by

View all comments

Show parent comment

3

u/antonivs Mar 01 '23

Reason for my question is also that I always learned from others that FP is slow

Compared to what? The major functional languages like Haskell, Ocaml, SML, and some Scheme implementations are significantly faster than Python, at least as fast as Java, and in some cases match or exceed C performance, e.g.: https://ericnormand.me/podcast/how-is-haskell-faster-than-c

But, what you may be referring to is that doing functional programming in a non-functional language like Javascript is slow. That’s going to depend a lot in what exactly you’re doing. A higher order function evaluating lots of closures is likely to be slower than an inline for loop, for example. That’s because the language doesn’t do any relevant optimizing compilation.

2

u/ibrahimbensalah Mar 02 '23

Compared to what?

imperative programming.

I don't know much about Python but I don't think comparing to Python is a fair comparison

If we compare the fastest language of each category then I predict that the common conception of most of us developer is that imperative Language are much faster. I just created this Poll maybe this could provide some insight on what the community presumes as the fastest.

https://www.reddit.com/r/functionalprogramming/comments/11fr9o8/what_type_of_languages_are_the_fastest/

A more interesting comparison is of FP patterns and _imperative_ patterns within same language. That is basically what JS Framework benchmark is all about. It is a big surprise, at least for me, that the absolute fastest framework is based on FP patterns.

FP patterns, how I see it, treats everything as data. even closures, higher order function and for loops. In .NET for example we have LINQ, which is the most amazing feature of C# designed by Eric Meijer. LINQ feels like a piece of Haskell inside C#. Closures inside a LINQ expression are just data / AST tree, officially called lambda expression.

The type of optimizations that we can do when everything is data is incredible.

Take e.g. matrix multiplication. we can compress unlimited number of transformation to a single matrix. The same idea can be applied to compress the operations to be executed on the DOM. e.g. navigation from one node to another.

Admittedly optimizations are not as extreme as matrix multiplication but I would argue the most optimal way possible.

Btw did you know rust compiles iterators to for loops. xania also has this type of optimization in some way during rendering.

And more importantly, at the same time, xania does not compromise on developer experience, courtesy of FP.