Giter Club home page Giter Club logo

elixir-map-deeply's Introduction

DeeplyEnumerable and DataLeafWalker

Traverse and map values of deeply nested data structures: Provides a map_deeply/2 function for Maps and Lists and Keyword Lists.

rroouugghhllyy based on ideas from Perl's Data::Leaf::Walker

Synopsis

data = [1, 2, 3, [{:a, 2}, {:b, 4}], %{ a: 2 } ]
DeeplyEnumerable.map_deeply map, &( &1 + 1 )

# yields
[2, 3, 4,[a: 3, b: 5], %{a: 3}]

Usage

DeeplyEnumerable.map_deeply( data, fn( value ) -> ...transform value... end )

Where data can ba a map, list or keyword list.

Direct usage of DataLeafWalker

Or, you may DataLeafWalker's map/2 directly (discouraged):

DataLeafWalker.map( data, fn( value ) -> ...transform value... end )

The difference beetween the module DataLeafWalker and the protocol DeeplyEnumerable is that by implementing the latter for your structs you may add in extra functionality to the transformation or make it conditionally transform or whatever. I needed this feature, that's why it's here. For Lists and Maps, DeeplyEnumerable is already implemented (simply calling to DataLeafWalker.map/2). Though it seems overkill at first glance, having an extension entry point to your transformations of complex structs is usually a safe thing.

Structs

In order to support structs you may either use DataLeafWalker.map/2 or (better) implement DeeplyEnumerable for you struct type, most often like this:

defimpl DeeplyEnumerable, for: MyStruct do
  def map_deeply( struct, fn_transform ) do
    DataLeafWalker.map( struct, fn_transform )
  end
end

What I use this for

I have to migrate ancient projects that use YAML-serialized in database columns. Unfortunately they are doubly UTF-8 encoded so I convert these deeply-nested maps into maps of the same structure with all values decoded into proper UTF-8.

Installation

The package can be installed as:

  1. Add data_leaf_walker to your list of dependencies in mix.exs:

    def deps do [{:data_leaf_walker, "~> 0.1.0"}] end

Todo

  • make keys subject to mappability, too.
  • support structs.
  • add proper docs
  • really a protocol? Yes
  • add property-based tests. more tests.

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.