Coder Social home page Coder Social logo

zipper's Introduction

Generic zipper implementation in Erlang.

Zippers: what are they good for?

Zippers let you traverse immutable data structures with ease and flexibility.

Contact Us

For questions or general comments regarding the use of this library, please use our public hipchat room.

If you find any bugs or have a problem while using this library, please open an issue in this repo (or a pull request :)).

And you can check all of our open-source projects at inaka.github.io

Usage

For a map tree structure like the following:

Root = #{type => planet,
         attrs => #{name => "Earth"},
         children => [
           #{type => continent,
             attrs => #{name => "America"},
             children => [
               #{type => country,
                 attrs => #{name => "Argentina"},
                 children => []},
               #{type => country,
                 attrs => #{name => "Brasil"},
                 children => []}
             ]
            },
           #{type => continent,
             attrs => #{name => "Europe"},
             children => [
               #{type => country,
                 attrs => #{name => "Sweden"},
                 children => []},
               #{type => country,
                 attrs => #{name => "England"},
                 children => []}
             ]
            }
         ]
        },

You can build a zipper by providing three simple functions:

  • IsBranchFun: takes a node and returns true if it is a branch node or false otherwise.
  • ChildrenFun: takes a node and returns a list of its children.
  • MakeNodeFun: takes a node and a list of children and returns a new node containg the supplied list as children.

This is an example of how you would define a zipper and then use it to traverse the map tree structure above:

%% Create the zipper
IsBranchFun = fun
                  (#{children := [_ | _]}) -> true;
                  (_) -> false
              end,
ChildrenFun = fun(Node) -> maps:get(children, Node) end,
MakeNodeFun = fun(Node, Children) -> Node#{children => Children} end,
Zipper = zipper:new(fun is_map/1, ChildrenFun, MakeNodefun, Root),

%% Traverse the zipper with next
Zipper1 = zipper:next(Zipper),
Zipper2 = zipper:next(Zipper),

%% Get the current zipper node
Argentina = zipper:node(Zipper2).
io:format("~p", [Argentina]),
%%= #{type => country,
%%=   attrs => #{name => "Argentina"},
%%=   children => []}

%% Go up and get the node
Zipper3 = zipper:up(Zipper2).
America = zipper:node(Zipper2).
io:format("~p", [America]),
%%= #{type => country,
%%=   attrs => #{name => "America"},
%%=   children => [#{...}, #{...}]}

Tests

Circular dependency in test environment (Katana Test -> Elvis Core -> Zipper) is fixed by including Zipper as a dep in the test profile in rebar.config

...
{profiles, [
  {test, [
    {deps, [
      %% The tag wil be replaced by the rebar.config.script
      {zipper,      {git, "https://github.com/inaka/zipper.git", {tag, "irrelevant"}}},
      ...
    ]}
  ]}
]}.
...

but then, we still replace the tag with the current branch. This is done in rebar.config.script. Therefore, it's really important to have the branch updated and pushed to github before running the tests with rebar3 ct.

References

zipper's People

Contributors

demian711 avatar elbrujohalcon avatar euen avatar harenson avatar igaray avatar jfacorro avatar spike886 avatar

Watchers

 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.