Last active
March 16, 2016 11:25
-
-
Save gutschilla/ce29727964f9939786af to your computer and use it in GitHub Desktop.
Elixir tramnsform snippets on my way from Perl to Elixir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Perl: | |
my %map = ( a => 1, b => 2 ); | |
my $fn_transform = sub { shift( @_ ) + 1 }; | |
map {( $_ => $fn_transform->( $map{$_} ) )} keys %map; | |
# transform all values of a map | |
map = %{ a: 1, b: 2 } | |
fn_transform = &( &1 + 1) | |
Enum.into map, %{}, fn({k, v}) -> {k, fn_transform.(v) } end | |
# same as | |
Enum.map( map, fn({k, v}) -> {k, fn_transform.(v) } end ) |> Map.new | |
# factor-out more | |
fn_mapper = fn({k, v}) -> {k, fn_transform.(v) } end | |
Enum.into map, %{}, fn_mapper | |
Enum.map( map, fn_mapper ) |> Map.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So, apparently, both Map.into and Enum.map allow to map on values (and keys). Enum.into returns a map