r/functionalprogramming • u/ibrahimbensalah • 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
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?
14
Upvotes
View all comments
Show parent comment
3
u/ibrahimbensalah Feb 28 '23
The exact method does not exist, but also it does, the pattern is implemented ah-hoc-ely because I wasnt fully aware of what I am dealing with. I had e.g. a lot of duplication of the pattern matching logic which motivated this exploration.
Template can have strings, int, Date, or in fact any object. Template encapsulate the type provided, a string template is represented as follows:
Template<int>can be constructed with:textNode(5)or equalytMap(textNode("xania"), length) === textNode(5)If we peek into
textNode(5)then it would look like this:So
Template<T>is not a string, but rather a discriminated union of variety of types, one of is theTextTemplatementioned above.There is even support for Promises and Observables and there is a constructor for each that produces a Template. but I didnt want to complicate the discussion.
Transforming Template to HTML happens only in side effect land, after we call
renderat the end.