Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/artemeff/fn

More functional Erlang
https://github.com/artemeff/fn

Last synced: about 2 months ago
JSON representation

More functional Erlang

Awesome Lists containing this project

README

        

### fn [![Build Status](https://img.shields.io/travis/artemeff/fn.svg)](https://travis-ci.org/artemeff/fn) [![Hex.pm](https://img.shields.io/hexpm/v/fn.svg)](fn)

---

###### Function composition `(f ∘ g)(x) = f(g(x))`

```erlang
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)))`

```erlang
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)))`

```erlang
Val = fn:naive(
[ fun(X) -> X + 3 end
, fun(X) -> X * 4 end
, fun(X) -> X - 2 end
], 10),
Val % => 50
```

---

###### Error monad application

```erlang
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

```erlang
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
```

---

### Contributing

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