

Self-Memoizing HTML Rendering via Mutually Recursive Data Types - lelf
https://ocharles.org.uk/blog/posts/2014-11-11-memo-html.html

======
edem
Please add the programming language used at the top. I looked at the code and
it was obvious that it was not java/lisp/python/php (the ones I know). I
guessed Haskell and I had to do a text search to find it out.

------
amelius
Look here [1] for a generalization of this idea.

[1] [http://www.umut-acar.org/self-adjusting-computation](http://www.umut-
acar.org/self-adjusting-computation)

------
munro
Nice optimization for rerendering HTML, I wonder if React uses this technique.

Also, sometimes I wonder if it's worth pulling out separate concepts from a
data structure ... I usually just embed them, but I always consider building a
parallel data structure.

    
    
        data HTML = Text String | Element String [HTML]
        data MemoizedHTML = MemoizedText String | MemoizedElement String [MemoizedHTML]
        
        rendered :: (HTML, MemoizedHTML)
        rendered = (
                Element "foo" [
                    Element "bar" [],
                    Text "hello"
                ],
                MemoizedElement "<foo><bar />hello</foo>" [
                    MemoizedElement "<bar />" [],
                    MemoizedText "hello"
                ]
            )

