Created
July 12, 2017 10:49
-
-
Save devonestes/ced2a309cf3c988e051063fda610567c to your computer and use it in GitHub Desktop.
This file contains 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
iex(1)> defmodule Tester do | |
...(1)> defstruct [:first, :second] | |
...(1)> def foo_to_bar(struct = %{__struct__: struct_module}) do | |
...(1)> updated_list = struct | |
...(1)> |> Map.from_struct | |
...(1)> |> Enum.map(fn({key, value}) -> | |
...(1)> if value == :foo do | |
...(1)> {key, :bar} | |
...(1)> else | |
...(1)> {key, value} | |
...(1)> end | |
...(1)> end) | |
...(1)> struct(struct_module, updated_list) | |
...(1)> end | |
...(1)> end | |
{:module, Tester, | |
<<70, 79, 82, 49, 0, 0, 11, 100, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 243, | |
131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115, | |
95, 118, 49, 108, 0, 0, 0, 4, 104, 2, ...>>, {:foo_to_bar, 1}} | |
iex(2)> s = %Tester{first: :foo, second: :baz} | |
%Tester{first: :foo, second: :baz} | |
iex(3)> Tester.foo_to_bar(s) | |
%Tester{first: :bar, second: :baz} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment