Giter Club home page Giter Club logo

adt's Introduction

Algebraic Data Types

This module implements algebraic data types inspired by the Haskell syntax. See below for limitations of the current implementation.

Here's a code example for you:

# define the algebraic data type
my $adt = q{Tree = Branch Tree left, Tree right | Leaf Str storage}
my %res = create_adt($adt);

# capture the Tree class
my \Tree = %res<Tree>;

# create a tree using named parameters
my $t =
    Tree.new-branch(
        :left(Tree.new-branch(
            :left(Tree.new-leaf(:storage(1))),
            :right(Tree.new-leaf(:storage(2))))),
        :right(Tree.new-leaf(:storage(3))));
# pretty-print the tree
say $t.gist;

# create a tree using positional arguments
my $t2 =
    Tree.new-branch(
        Tree.new-branch(
            Tree.new-leaf(1),
            Tree.new-leaf(2)),
        Tree.new-leaf(3));
say $t2.gist;

# capture the subtypes to pattern-match in a given block.
my \Branch = %res<Branch>;
my \Leaf = %res<Leaf>;

# map over a whole tree changing all storages.
sub treemap($t, *&code) {
    given $t {
        when Branch { return Tree.new-branch(treemap($t.left, &code), treemap($t.right, &code)) }
        when Leaf { return Tree.new-leaf(code($t.storage)) }
    }
}

# multiply every leaf node by 10
say treemap($t2, * * 10).gist;

Limitations

Compile-time availability

Since rakudo doesn't yet allow the EXPORT sub of a package to return a hash of things to export, the ADT cannot be used to define multi subs, because the symbols are not available at compile time.

When EXPORT works, code like this will be possible:

use ADT "data Tree = Branch Tree left, Tree right | Leaf Str storage";
multi sub treemap(Branch $t, *&c) { ... }
multi sub treemap(Leaf $l, *&c) { ... }

Compile-time checks

One of the neat features of ADT is that the compiler can ensure, that the program is prepared to deal with every defined case (Branch or Leaf, Just or Nothing, ...). A macro can probably be devised to implement such a compile-time check.

"is parsed" macros

When Rakudo gets support for "is parsed" macros, definitons of ADT can move from the "use" line into the mainline of the code, so that they look tidyer and the programmer can put comments nearby etc.

adt's People

Contributors

retupmoca avatar timo avatar

Watchers

 avatar  avatar

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.