Bug 471379 - New direct/reversed infix application operators
Summary: New direct/reversed infix application operators
Status: NEW
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Golo (show other bugs)
Version: unspecified   Edit
Hardware: PC Mac OS X
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords: investigate
Depends on:
Blocks:
 
Reported: 2015-06-29 16:26 EDT by Julien Ponge CLA
Modified: 2016-05-25 10:27 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Julien Ponge CLA 2015-06-29 16:26:47 EDT
What about adding direct/reversed infix application operators. As an example (using caml symbols),

    the direct application operator <| is defined as: f <| x = f(x) with right priority,
    the reverse application operator |> is defined as: x |> f = f(x) with left priority, Such that a function call:

h(g(f (x)))

can be written

h <| g <| f<| x

or

x |> f |> g |> h

(Unix like)

What do you think?

----  jponge commented on 27 Oct 2014

Why not. How does it work with more than 1 parameter?

In any case it's worth exploring!

----  yloiseau commented on 27 Oct 2014

For easy composing of functions, they should have only one argument (which can be a tuple :smile:).
However, all this can already be written as

f:andThen(g):andThen(h)(x)

e.g.

let f = |x| -> x + 1
let g = |x| -> 2 * x
let h = |x| -> "answer: " + x
require(h(g(f(20))) == "answer: 42", "err")
require(f:andThen(g):andThen(h)(20) == "answer: 42", "err")

or using a HOF to do the hard work:

let pipe = |funcs...| -> atoList(funcs): reduce(
  |x| -> x,
  |f, g| -> f:andThen(g)
)
require(pipe(f, g, h)(20) == "answer: 42", "err")

So it may not be necessary after all

----  yloiseau commented on 27 Oct 2014

Btw I'm currently builing a golo lib with useful hof and generic generators, with among other things this pipe function, a curry/uncurry one, and one to convert n-ary functions into a unary function taking a tuple and the other way (useful for composing)

----