Created
September 11, 2019 18:59
-
-
Save shamansir/9ee308fff9da644c221276f08c768d65 to your computer and use it in GitHub Desktop.
TreeAna
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- from: https://gist.github.com/kapranov-anton/86d44e4d382c2c38ffb5d59e331bc93a | |
module Main where | |
import Prelude | |
import Data.List (List) | |
import Data.List as List | |
import Control.Monad.Eff (Eff) | |
import Data.Foldable (fold) | |
import TryPureScript (DOM, h1, h2, p, text, list, indent, link, render, code) | |
data TreeF v a | |
= Node v (List a) | |
treeMap :: forall a v b. (a -> b) -> TreeF v a -> TreeF v b | |
treeMap f (Node v xs) = | |
Node v $ f <$> xs | |
data Tree v | |
= Tree (TreeF v (Tree v)) | |
treeAna :: forall v a. (a -> TreeF v a) -> a -> Tree v | |
treeAna alg = | |
Tree <<< treeMap (treeAna alg) <<< alg | |
-- treeAna alg a = | |
-- Tree <<< treeMap (treeAna alg) $ alg a | |
treeCoalgebra :: Int -> TreeF Int Int | |
treeCoalgebra a = | |
Node 0 List.Nil | |
instance showTree :: Show v => Show (Tree v) | |
where show (Tree (Node v trees)) = show v <> (fold $ show <$> trees) | |
main :: Eff (dom :: DOM) Unit | |
main = | |
render $ fold | |
[ h1 (text $ show $ treeAna treeCoalgebra 99) | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment