KitchenSink v1.3.2 KitchenSink.List View Source

this module is for List functions

Link to this section Summary

Functions

Creates an indexed list from a list of maps using a key_function or a path. If a path is given then the Access module will be used

Takes a list of maps, transforms it into a map of maps with their value being the value_key. Basically making a look-up table

A convenient version of what is perhaps the most common use-case for map: extracting a list of property values

Link to this section Functions

Creates an indexed list from a list of maps using a key_function or a path. If a path is given then the Access module will be used.

This function is very similar to group_by, however the values will not be grouped. There should be a 1-to-1 mapping for the key_fun to item in the list. For different mapping ratio duplicate keys will be dropped, according to Map.new/1 logic

Examples

iex> [
...>   %{name: :a, other: 1},
...>   %{name: :b, other: 4}
...> ] |> index_by(:name)
%{
  a: %{name: :a, other: 1},
  b: %{name: :b, other: 4}
}
Link to this function index_on(list_of_maps, take_keys, value_key) View Source
index_on([map], [any], any) :: map

Takes a list of maps, transforms it into a map of maps with their value being the value_key. Basically making a look-up table.

Examples

iex> [
...>   %{a: 1, b: "x"},
...>   %{a: 2, b: "y"},
...>   %{a: 3, b: "z"}
...> ] |> index_on([:a], :b)
%{
  %{a: 1} => "x",
  %{a: 2} => "y",
  %{a: 3} => "z"
}

iex> [
...>   %{a: 1, b: "x"},
...>   %{a: 2, b: "y"},
...>   %{a: 3, b: "z"}
...> ] |> index_on(:a, :b)
%{
  %{a: 1} => "x",
  %{a: 2} => "y",
  %{a: 3} => "z"
}
Link to this function pluck(list_of_maps, key) View Source
pluck([map], any) :: list

A convenient version of what is perhaps the most common use-case for map: extracting a list of property values.

With a List of Maps, extract 1 value defined by the key you give to pluck.

Examples

iex> pluck([%{b: 1}, %{a: 2}, %{a: 3}], :a)
[nil, 2, 3]