Giter VIP home page Giter VIP logo

algebrick's Introduction

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

algebrick's People

Contributors

madlep avatar olleolleolle avatar pitr-ch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

algebrick's Issues

Other-fields consumtion

It would be great if algebrick could be used for defining the data comming from external services over REST and others.

Usually, one doesn't care about all the data that come from the REST, and it would cause more harm than benefits to try doing that. The idea is to define only the fields
that we care about in our code-base and don't care about the rest that much.

Let's say, I get this hash from the REST:

p = {"person": {"name":"Petr", "created_at":"2013-12-28"}}

I care only about the name, so instead of having to define all the fields that might occur (and breaking immediately, when something new is added), I want to ignore the rest of store that in some additional field:

Person = Algebrick.type do
  fields name: "Petr"
  ignore_other_fields
end

or

Person = Algebrick.type do
  fields name: "Petr"
  store_other_fields :others
end

person = Person[p]
person[:name] # => "Petr"
person[:others][:created_at] # => "2013-12-28"

List type

Hi,

Really like the look of this library, though I'm struggling to use or define a/the list type. I tried defining my own and I've tried using the one in lib/algebrick/types.rb - I'm not even sure I should be calling that directly. I'm at a bit of a loss. Is there an example for this somewhere, or do you know of a library using it? (real world examples are always best:)

Any help or insight would be much appreciated.

Regards,
iain

Better file structure

For better code orientation, it would be nice to have the code placed in more smaller files, than one big algebrick.rb. The same for tests. What do you think?

Better description for anonymous types

Envelope = Algebrick.type do
  fields! request_id: Integer,
            sender_id: String,
            receiver_id: type { variants String, AnyExecutor = atom, UnknownWorld = atom },
            message: type { variants Request, Response }

With, Envolope.to_s it produces NoName, where it could descibe, the message as the variant of other types

Add TypedArray and TypedHash

I want to define that person has more phone numbers, something like:

Person = Algebrick.type do
  fields name: String
         phone_numbers: TypedArray[String]
end

Enumerables or pattern-matching in type definition support

I tent to like this gem more and more every day:) I would like to use that for loading hashes from REST APIs (possibly with integration with apipie-rails later) and others

Let's say I have a hash:

petr_data = { name: "Petr", sex: "male" }
ada_data  = { name: "Ada", sex: "female" } 

I would like to do something like this:

Person = Algebrick.type do
    fields name: String,
             sex: [:male, :female]
end

people = [Person[petr_data], Person[ada_data]]

if people.any? { |person| person[:sex] == :male } ||
   people.any? { |person| person[:sex] == :female }
  puts "Congrats, your code is coeducated"
end

Maybe the :male and :female could be defined with separate types, and include there pattern matching:

Gender = Algebrick.type do |gender|
   variants gender::Male = atom, gender::Female = atom
   match do |value|
       case value
       when "male", :male
          gender::Male
       when "female", :female
          gender::Female 
       end
   end
end
Person = Algebrick.type do
    fields name: String,
             sex: Gender
end

So the match would provide a way how to typecast the values.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.