Last active
August 22, 2021 02:19
-
-
Save okabe-yuya/9317abc95e8583795e450815aef26aca to your computer and use it in GitHub Desktop.
構造体へのFunctor / fmapのオリジナル定義
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
defmodule Product do | |
defstruct name: "", price: 0 | |
def fmap(func, %Product{ name: name, price: price }) do | |
{ n_name, n_price } = func.(name, price) | |
%Product{ name: n_name, price: n_price } | |
end | |
end | |
stones = %Product{ name: "大人気!その辺に落ちていた石の詰め合わせ", price: 2000 } | |
summer_sale = Product.fmap(fn name, price -> { "[SummerSale]: #{name}", price - 200 } end, stones) | |
# %Product{ | |
# name: "[SummerSale]: 大人気!その辺に落ちていた石の詰め合わせ", | |
# price: 1800 | |
# } | |
price_hike = Product.fmap(fn name, price -> { "[市場価格高騰]: #{name}", price + 2000 } end, stones) | |
# %Product{ | |
# name: "[市場価格高騰]: 大人気!その辺に落ちていた石の詰め合わせ", | |
# price: 4000 | |
# } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment