artemeff/fn
More functional Erlang
{ "createdAt": "2015-04-13T18:26:48Z", "defaultBranch": "master", "description": "More functional Erlang", "fullName": "artemeff/fn", "homepage": "", "language": "Erlang", "name": "fn", "pushedAt": "2016-01-07T06:39:18Z", "stargazersCount": 9, "topics": [], "updatedAt": "2024-12-10T09:54:31Z", "url": "https://github.com/artemeff/fn"}fn
[
]!(fn)
Section titled “fn []!(fn)”Function composition (f ∘ g)(x) = f(g(x))
Section titled “Function composition (f ∘ g)(x) = f(g(x))”Fn = fn:compose(fun(X) -> X + X end, fun(X) -> X * 4 end)Fn(10) % => 80Fn(12) % => 96Multiple function composition (f ∘ g ∘ h)(x) = f(g(h(x)))
Section titled “Multiple function composition (f ∘ g ∘ h)(x) = f(g(h(x)))”Fn = fn:compose( [ fun(X) -> X + 3 end , fun(X) -> X * 4 end , fun(X) -> X - 2 end ])Fn(10) % => 50Fn(13) % => 62Naive application f $ g $ h $ x = f(g(h(x)))
Section titled “Naive application f $ g $ h $ x = f(g(h(x)))”Val = fn:naive( [ fun(X) -> X + 3 end , fun(X) -> X * 4 end , fun(X) -> X - 2 end ], 10),Val % => 50Error monad application
Section titled “Error monad application”Val1 = fn:error_monad( [ fun(X) -> {ok, X + 3} end , fun(X) -> {ok, X * 4} end , fun(X) -> {ok, X - 2} end ], 10),Val1 % => {ok, 50}
Val2 = fn:error_monad( [ fun(X) -> {ok, X + 3} end , fun(X) -> {ok, X * 4} end , fun(_) -> {error, something_went_wrong} end ], 10),Val2 % => {error, something_went_wrong}Partial application
Section titled “Partial application”Fn1 = fn:partial(fun lists:map/2, [fun erlang:list_to_atom/1])Fn1(["test", "partial"]) % => [test, partial]
Fn2 = fn:partial(fun lists:foldl/3, [fun(X, Acc) -> X * Acc end, 1])Fn2([2, 5, 10]) % => 100Contributing
Section titled “Contributing”- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request