Project

algebrick

0.06
No release in over 3 years
Low commit activity in last 3 years
There's a lot of open issues
Provides algebraic type definitions and pattern matching
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
 Dependencies

Development

~> 1.13
~> 5.10
~> 10.4
~> 0.9
 Project Readme

Algebrick

Build Status

Typed structs on steroids based on algebraic types and pattern matching seamlessly integrating with standard Ruby features.

What is it good for?

  • Well defined data structures.
  • Actor messages see new Actor implementation in concurrent-ruby.
  • Describing message protocols in message-based cross-process communication. Algebraic types play nice with JSON de/serialization.
  • and more...

Quick example

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                                        # => 0
Leaf[10].depth                                     # => 1
Node[Leaf[4], Empty].depth                         # => 2
Node[Empty, Node[Leaf[1], Empty]].depth            # => 3