Skip to content

Typed structs on steroids based on algebraic types and pattern matching.

License

Notifications You must be signed in to change notification settings

pitr-ch/algebrick

Folders and files

NameName
Last commit message
Last commit date

Latest commit

53ffe48 · Dec 13, 2017
Mar 20, 2015
Jan 12, 2017
Jan 12, 2017
Jan 12, 2017
Jan 12, 2017
Jan 12, 2017
Sep 16, 2013
Oct 2, 2013
Dec 9, 2017
Dec 18, 2014
Apr 29, 2017
Sep 29, 2017
Sep 29, 2017
Dec 18, 2014

Repository files navigation

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

About

Typed structs on steroids based on algebraic types and pattern matching.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages