Skip to content
vic

artemeff/fn.json
{
"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"
}

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) % => 80
Fn(12) % => 96

Multiple 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) % => 50
Fn(13) % => 62

Naive 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 % => 50

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}

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]) % => 100

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request