pitr-ch/algebrick
Typed structs on steroids based on algebraic types and pattern matching.
{ "createdAt": "2013-05-06T18:27:17Z", "defaultBranch": "master", "description": "Typed structs on steroids based on algebraic types and pattern matching.", "fullName": "pitr-ch/algebrick", "homepage": "", "language": "Ruby", "name": "algebrick", "pushedAt": "2021-02-04T11:25:03Z", "stargazersCount": 88, "topics": [], "updatedAt": "2025-01-05T21:11:42Z", "url": "https://github.com/pitr-ch/algebrick"}Algebrick
Section titled “Algebrick”Typed structs on steroids based on algebraic types and pattern matching seamlessly integrating with standard Ruby features.
- Documentation: http://blog.pitr.ch/algebrick
- Source: https://github.com/pitr-ch/algebrick
- Blog: http://blog.pitr.ch/tag/algebrick.html
What is it good for?
Section titled “What is it good for?”- Well defined data structures.
- Actor messages see new Actor implementation in [concurrent-ruby]!(concurrent-ruby.com).
- Describing message protocols in message-based cross-process communication. Algebraic types play nice with JSON de/serialization.
- and more…
Quick example
Section titled “Quick example”Let’s define a Tree
Tree = Algebrick.type do |tree| variants Empty = atom, Leaf = type { fields Integer }, Node = type { fields tree, tree }endNow 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) endendMethod defined in module Tree are passed down to all values of type Tree
Empty.depth # => 0Leaf[10].depth # => 1Node[Leaf[4], Empty].depth # => 2Node[Empty, Node[Leaf[1], Empty]].depth # => 3