# Is partial function application in Erlang a good idea?

**URL:** <https://erlangforums.com/t/is-partial-function-application-in-erlang-a-good-idea/473>\
**Category:** Chat / Discussions\
**Tags:** syntax, idea\
**Created:** [November 5, 2021, 12:53pm UTC](https://erlangforums.com/t/is-partial-function-application-in-erlang-a-good-idea/473 "2021-11-05T12:53:18Z")\
**Posts on this page:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![Maria-12648430](https://erlangforums.com/user_avatar/erlangforums.com/maria-12648430/32/604_2.png) [@Maria-12648430](https://erlangforums.com/u/Maria-12648430)\
**Post date:** [November 12, 2021, 4:49pm UTC](https://erlangforums.com/t/is-partial-function-application-in-erlang-a-good-idea/473/21 "2021-11-12T16:49:53Z")

</div>

🤷‍♀️

Guess I’ll leave that to @Gwaeron, if he likes 😅

---

<div class="post-metadata">

**Author:** ![Hanabi](https://erlangforums.com/letter_avatar_proxy/v4/letter/h/dec6dc/32.png) [@Hanabi](https://erlangforums.com/u/Hanabi)\
**Post date:** [December 8, 2021, 6:10pm UTC](https://erlangforums.com/t/is-partial-function-application-in-erlang-a-good-idea/473/22 "2021-12-08T18:10:45Z")

</div>

I love this idea! It reminds me of scala’s usage of \_ for ignored params.  
It working well with a future pipe operator alone in my opinion justifies its existence.

Here’s a random scala snippet I tried to translate to idiomatic erlang, with my understanding of this proposal implemented:

```erlang
val items = Seq(("candy", 2, true), ("cola", 7, false), ("apple", 3, false), ("milk", 4, true))
val itemsToBuy = items
  .filter(_._3) // filter in only available items (true)
  .filter(_._2 > 3) // filter in only items with price greater than 3
  .map(_._1) // return only the first element of the tuple; the item name
assertEquals(itemsToBuy, Seq("milk"))

```

```erlang
Items = [{candy, 2, true}, {cola, 7, false}, {apple, 3, false}, {milk, 4, true}],
ItemsToBuy = lists:filter(element(3, _) , Items)
          |> lists:filter(element(2, _) > 3, _ )
          |> lists: map(element(1, _) , _ ).

```

This probably begs the question that if `Items` were a list of lists, should `lists:filter(_._3, Items)` be valid with `_._N` being an alias for `lists:nth(N, _)`  
Though I’m not particularly interested in answering that question.

---

<div class="post-metadata">

**Author:** ![Gwaeron](https://erlangforums.com/user_avatar/erlangforums.com/gwaeron/32/833_2.png) [@Gwaeron](https://erlangforums.com/u/Gwaeron)\
**Post date:** [December 9, 2021, 2:07am UTC](https://erlangforums.com/t/is-partial-function-application-in-erlang-a-good-idea/473/23 "2021-12-09T02:07:31Z")

</div>

Yeah, that usage is pretty much what I had in mind, except the second filter which compares a fun `element(2, _)` to an integer `3` and would crash since there’s no function clause matching `lists:filter(bool(), [any()])`.  
Though I think each of those filter functions could be replaced by a list comprehension for easier reading, this demonstrates how the `fun`-ifying with missing elements can be useful for piping.

I haven’t responded here in a while due to being busy, but I intend to try to rewrite some non-toy examples in OTP modules that I think will demonstrate, as requested, how this could be used to generate cleaner code, and then maybe send it to the mailing list if there is some interest.

---

<div class="post-metadata">

**Author:** ![Hanabi](https://erlangforums.com/letter_avatar_proxy/v4/letter/h/dec6dc/32.png) [@Hanabi](https://erlangforums.com/u/Hanabi)\
**Post date:** [December 9, 2021, 3:21am UTC](https://erlangforums.com/t/is-partial-function-application-in-erlang-a-good-idea/473/24 "2021-12-09T03:21:25Z")

</div>

> except the second filter which compares a fun `element(2, _)` to an integer `3` and would crash since there’s no function clause matching `lists:filter(bool(), [any()])`.

Oh right… would surrounding the expression in parentheses like `(element(2, _) > 3)` signify to be interpreted as `fun(Elem) -> element(2, Elem) > 3` or would that not be possible at this point?

---

<div class="post-metadata">

**Author:** ![Gwaeron](https://erlangforums.com/user_avatar/erlangforums.com/gwaeron/32/833_2.png) [@Gwaeron](https://erlangforums.com/u/Gwaeron)\
**Post date:** [December 9, 2021, 4:28am UTC](https://erlangforums.com/t/is-partial-function-application-in-erlang-a-good-idea/473/25 "2021-12-09T04:28:10Z")

</div>

In a prototype I made using parse transforms, operators were supported `_ > 3`, but I didn’t add any way to change the scope of the placeholder `_` to a surrounding expression.

With the suggestion made above to keep the `fun` keyword I guess it could be possible to control the scope of the placeholder, something like `fun (element(2, _) > 3)` but I don’t think that looks like Erlang any more, a normal `fun` would be better. I wouldn’t be surprised if there are also other implications I didn’t consider yet that would make this use inconsistent.

---

<div class="post-metadata">

**Author:** ![lpil](https://erlangforums.com/user_avatar/erlangforums.com/lpil/32/222_2.png) [@lpil](https://erlangforums.com/u/lpil)\
**Post date:** [December 11, 2021, 7:56pm UTC](https://erlangforums.com/t/is-partial-function-application-in-erlang-a-good-idea/473/26 "2021-12-11T19:56:36Z")

</div>

It has been quite fun to read this thread as Gleam has precisely this syntax for partial function application 😁

---

<div class="post-metadata">

**Author:** ![Maria-12648430](https://erlangforums.com/user_avatar/erlangforums.com/maria-12648430/32/604_2.png) [@Maria-12648430](https://erlangforums.com/u/Maria-12648430)\
**Post date:** [January 26, 2022, 11:51am UTC](https://erlangforums.com/t/is-partial-function-application-in-erlang-a-good-idea/473/27 "2022-01-26T11:51:44Z")

</div>

What became of this project? @Gwaeron?

---

<div class="post-metadata">

**Author:** ![Gwaeron](https://erlangforums.com/user_avatar/erlangforums.com/gwaeron/32/833_2.png) [@Gwaeron](https://erlangforums.com/u/Gwaeron)\
**Post date:** [January 29, 2022, 4:06am UTC](https://erlangforums.com/t/is-partial-function-application-in-erlang-a-good-idea/473/28 "2022-01-29T04:06:40Z")

</div>

Sorry for the delay, I’ve been in the middle of a move and didn’t have time to do a write-up yet, but it’s definitely still on the to-do list! Look forward to it Soon™.

---

<div class="post-metadata">

**Author:** ![Maria-12648430](https://erlangforums.com/user_avatar/erlangforums.com/maria-12648430/32/604_2.png) [@Maria-12648430](https://erlangforums.com/u/Maria-12648430)\
**Post date:** [January 31, 2022, 9:52am UTC](https://erlangforums.com/t/is-partial-function-application-in-erlang-a-good-idea/473/29 "2022-01-31T09:52:40Z")

</div>

> [@Gwaeron](#):
>
> Look forward to it Soon™.

Will do 🙂

[Previous page](https://erlangforums.com/t/is-partial-function-application-in-erlang-a-good-idea/473.md?page=1)
