项目作者: am-kantox

项目描述 :
Handy enumerable operations implementation.
高级语言: Elixir
项目地址: git://github.com/am-kantox/elixir-iteraptor.git
创建时间: 2016-09-09T16:13:59Z
项目社区:https://github.com/am-kantox/elixir-iteraptor

开源协议:MIT License

下载


Iteraptor

Build Status
Inline docs
Hex.pm

Handy enumerable operations

bonus:

  • Iteraptor.jsonify/2 to prepare the term for JSON interchange; it basically converts keys to strings and keywords to maps because JSON encoders might have issues with serializing keywords.

  • Iteraptor.Extras.bury/4
    to store the value deeply inside nested term (the intermediate keys are created as
    necessary.)

HexDocs

Usage

Iterating, Mapping, Reducing

  1. # each
  2. iex> %{a: %{b: %{c: 42}}} |> Iteraptor.each(&IO.inspect/1, yield: :all)
  3. # {[:a], %{b: %{c: 42}}}
  4. # {[:a, :b], %{c: 42}}
  5. # {[:a, :b, :c], 42}
  6. %{a: %{b: %{c: 42}}}
  7. # map
  8. iex> %{a: %{b: %{c: 42}}} |> Iteraptor.map(fn {k, _} -> Enum.join(k) end)
  9. %{a: %{b: %{c: "abc"}}}
  10. iex> %{a: %{b: %{c: 42}}}
  11. ...> |> Iteraptor.map(fn
  12. ...> {[_], _} = self -> self
  13. ...> {[_, _], _} -> "YAY"
  14. ...> end, yield: :all)
  15. %{a: %{b: "YAY"}}
  16. # reduce
  17. iex> %{a: %{b: %{c: 42}}}
  18. ...> |> Iteraptor.reduce([], fn {k, _}, acc ->
  19. ...> [Enum.join(k, "_") | acc]
  20. ...> end, yield: :all)
  21. ...> |> :lists.reverse()
  22. ["a", "a_b", "a_b_c"]
  23. # map-reduce
  24. iex> %{a: %{b: %{c: 42}}}
  25. ...> |> Iteraptor.map_reduce([], fn
  26. ...> {k, %{} = v}, acc -> {{k, v}, [Enum.join(k, ".") | acc]}
  27. ...> {k, v}, acc -> {{k, v * 2}, [Enum.join(k, ".") <> "=" | acc]}
  28. ...> end, yield: :all)
  29. {%{a: %{b: %{c: 42}}}, ["a.b.c=", "a.b", "a"]}
  30. # filter
  31. iex> %{a: %{b: 42, e: %{f: 3.14, c: 42}, d: %{c: 42}}, c: 42, d: 3.14}
  32. ...> |> Iteraptor.filter(fn {key, _} -> :c in key end, yield: :none)
  33. %{a: %{e: %{c: 42}, d: %{c: 42}}, c: 42}

Flattening

  1. iex> %{a: %{b: %{c: 42, d: [nil, 42]}, e: [:f, 42]}}
  2. ...> |> Iteraptor.to_flatmap(delimiter: "_")
  3. #⇒ %{"a_b_c" => 42, "a_b_d_0" => nil, "a_b_d_1" => 42, "a_e_0" => :f, "a_e_1" => 42}
  4. iex> %{"a.b.c": 42, "a.b.d.0": nil, "a.b.d.1": 42, "a.e.0": :f, "a.e.1": 42}
  5. ...> |> Iteraptor.from_flatmap
  6. #⇒ %{a: %{b: %{c: 42, d: [nil, 42]}, e: [:f, 42]}}

Extras

  1. iex> Iteraptor.jsonify([foo: [bar: [baz: :zoo], boo: 42]], values: true)
  2. %{"foo" => %{"bar" => %{"baz" => "zoo"}, "boo" => 42}}
  3. iex> Iteraptor.Extras.bury([foo: :bar], ~w|a b c d|a, 42)
  4. [a: [b: [c: [d: 42]]], foo: :bar]

As of version 1.2.0 there is an experimental AST traversal feature:

  1. iex> Iteraptor.AST.reduce((quote do: 42), [], fn e, acc -> [e | acc], yield: :all)
  2. '*'

Installation

Add iteraptor to your list of dependencies in mix.exs:

  1. def deps, do: [{:iteraptor, "~> 1.5"}]

Changelog

1.14.0

Updated dependecies to modern Elixir

1.13.0

keys: :reverse configuration option in all traversion functions to simplify pattern matching on leaf keys

1.10.0

Iteraptor.jsonify/2 for deep conversion of keyword lists to maps.

1.8.0

Iteraptor.Config for deep substitution of {:system, "VAR"} tuples with the
values taken from the system environment in runtime.

1.7.0

Iteraptor.Array with Access support. Basically, Array is the list with Access support.

  • 1.7.2 → fixed bug with type recognition for MapSet and Iteraptor.Array.

1.6.0

Iteraptor.jsonify/2.

1.5.0

All iterators do now accept structs: :values keyword argument to prevent nested iteration into structs.

Experimental support for Iteraptable protocol.

1.4.0

Extended support for Iteraptor.Iteraptable:

1.3.0

We now support MapSets.

1.0.0-rc1

Better documentation, Iteraptor.Extras.bury/3.

0.9.0

Complete refactoring, Iteraptor.map/3, Iteraptor.reduce/4, Iteraptor.map_reduce/4.

0.5.0

NB: This functionality is experimental and might not appear in 1.0.0 release.

use Iteraptor.Iteraptable inside structs to make them both
Enumerable and
Collectable:

  1. defmodule Iteraptor.Struct do
  2. @fields [field: nil]
  3. def fields, do: @fields
  4. defstruct @fields
  5. use Iteraptor.Iteraptable
  6. end
  7. iex> %Iteraptor.Struct{field: 42}
  8. ...> |> Enum.each(fn e -> IO.inspect(e) end)
  9. #⇒ {:field, 42}

0.4.0

Experimental: support for structs on input.
Structs will be automagically created on |> Iteraptor.from_flatmap from
keys like StructName%field if a) this structure is known to the system
and b) keys are consistent (e. g. there are no subsequent elements,
belonging to different structs: ["S1%f" => 42, "S2%f" => 3.14].)

0.3.0

Support for Keyword on input,
but it will be output as map for |> Iteraptor.to_flatmap |> Iteraptor.from_flatmap
back and forth transformation.