Sha256: fc6243b69f6ca4ff7eee1355a435f9b91b5458ef03b55028bba9eb3990f231d7
Contents?: true
Size: 835 Bytes
Versions: 6
Compression:
Stored size: 835 Bytes
Contents
# Let's define a Tree Tree = Algebrick.type do |tree| variants Empty = atom, Leaf = type { fields Integer }, Node = type { fields tree, tree } end # Now types `Tree(Empty | Leaf | Node)`, `Empty`, `Leaf(Integer)` and `Node(Tree, Tree)` are defined. # Let's add a method, don't miss the **pattern matching** example. module Tree # compute depth of a tree def depth match self, (on Empty, 0), (on Leaf, 1), # ~ will store and pass matched parts to variables left and right (on Node.(~any, ~any) do |left, right| 1 + [left.depth, right.depth].max end) end end # # Method defined in module `Tree` are passed down to **all** values of type Tree. Empty.depth Leaf[10].depth Node[Leaf[4], Empty].depth Node[Empty, Node[Leaf[1], Empty]].depth
Version data entries
6 entries across 6 versions & 1 rubygems