Code Monkey home page Code Monkey logo

rewritable-tree's Introduction

Rewritable tree

https://travis-ci.com/Primetalk/rewritable-tree.svg?branch=feature%2Frewritable-tree

libraryDependencies += "ru.primetalk" %% "rewritable-tree" % "0.1.0"

This library contains some tools for working with immutable data structures represented with algebraic data types. These data structures can be thought of as a tree. One of the tools is the rewrite function that helps to optimize the data structure based on some rules.

Primer

Let's try to implement a distributive property of elementary algebra:

a * (b + c) == a * b + a * c

We can model arithmetic expressions using the following data structure:

sealed trait Expr
case class Number(i:Int) extends Expr
case class Add(a: Expr, b: Expr) extends Expr
case class Mul(a: Expr, b: Expr) extends Expr

The distributive property (a * (b + c) == a * b + a * c) can be illustrated with trees:

   Mul              Add
   / \              / \
  a  Add    ==   Mul  Mul
     / \         / \  / \
    b  c        a  b a  c

and it can implemented as the following pattern matching rule:

case Mul(a@_, Add(b@_, c@_)) => Add(Mul(a, b), Mul(a, c))

If we want to apply this rule through the whole expression we need a way to traverse the tree and reconstruct it in case of replacement.

def rewrite(rule: Expr => Option[Expr])(tree: Expr): Expr

It's analogous to Functor.map with the difference that we are not mapping the data inside the tree but rather the "spine", the structure of the tree.

Fold (aka catamorphism)

We can construct another data structure from our tree, or calculate some statistics about it.

def fold[A](zero: A)(f: A => Expr => A): A

For instance, to count tree nodes:

val count = fold(0)(i => _ => i + 1)

There are two options for fold - whether we traverse the tree depth-first or width-first.

Collect data

Apart from rewriting trees it's often the case when we want to collect some data from the tree. This can be achieved with collect method

def collect[A](tree: Expr)(pf: PartialFunction[Expr, A]): Seq[A]

It can be used when we want to collect some of the tree elements.

rewritable-tree's People

Contributors

primetalk avatar

Stargazers

A ghost. avatar

Watchers

James Cloos avatar  avatar

Forkers

singhakanksha

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.