gleam-lang / suggestions

📙 A place for ideas and feedback
26 stars 2 forks source link

Function composition operator #113

Closed sporto closed 3 years ago

sporto commented 3 years ago

Function composition feels quite verbose currently.

e.g.

things
|> list.map(
  string.trim
  |> function.compose(string.replace(_, ",", ""))
  |> function.compose(int.parse)
)

It would be nice to have an operator to make something like this more succint and readable e.g.

things
|> list.map(
  string.trim
  >> string.replace(_, ",", "")
  >> int.parse
)

Thanks

lpil commented 3 years ago

No plans to add more function operators as they're difficult for newcomers to understand or look up.

Less importantly I also feel that there's a lot of overlap between a composition operator and the pipe application operator, I've not seen an example where one could not be rewritten into the other clearly.

The given example can be written more concisely using pipes like so:

things
|> list.map(fn(s) {
  string.trim(s)
  |> string.replace(",", "")
  |> int.parse
})
lpil commented 3 years ago

Going to close this as we don't have any clear way to move forwards, and the pipe operator alternative can be used.

I would be open to proposals for enhancements to making anonymous functions with the pipe operator more consise.