caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Infix function composition operator
@ 2010-11-10  3:19 Arlen Christian Mart Cuss
  2010-11-10  3:45 ` [Caml-list] " Yaron Minsky
  2010-11-10 13:23 ` Michael Ekstrand
  0 siblings, 2 replies; 5+ messages in thread
From: Arlen Christian Mart Cuss @ 2010-11-10  3:19 UTC (permalink / raw)
  To: caml-list

Hi all,

I know this was asked at least 12 years ago[1], but is there any
consensus or reason for there not being a "compose" function in standard
OCaml, nor an infix operator?

At the moment I tend to "let compose" or "let (<<-) f g x = f (g x)",
but I wish I didn't have to!

Thanks,
Arlen

[1]
http://webcache.googleusercontent.com/search?q=cache:TcqI7o37il8J:pauillac.inria.fr/caml/caml-list/0720.html+ocaml+function+compose&cd=2&hl=en&ct=clnk&client=ubuntu


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Infix function composition operator
  2010-11-10  3:19 Infix function composition operator Arlen Christian Mart Cuss
@ 2010-11-10  3:45 ` Yaron Minsky
  2010-11-10  4:37   ` Arlen Christian Mart Cuss
  2010-11-10 10:06   ` DS
  2010-11-10 13:23 ` Michael Ekstrand
  1 sibling, 2 replies; 5+ messages in thread
From: Yaron Minsky @ 2010-11-10  3:45 UTC (permalink / raw)
  To: Arlen Christian Mart Cuss; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 1378 bytes --]

This is probably a minority opinion, but I have written and read quite a lot
of OCaml code over the years, and I've seen surprisingly few effective uses
of the composition operator.  Somehow, I usually find that code that avoids
it is simpler and easier to read.

I'm not averse to infix operators.  At Jane Street we've found the following
sequencing operator to be highly useful:

let ( |! ) x f = f x

and it is indeed part of the default include in Jane Street's Core library.

y

On Tue, Nov 9, 2010 at 10:19 PM, Arlen Christian Mart Cuss <
arlen@noblesamurai.com> wrote:

> Hi all,
>
> I know this was asked at least 12 years ago[1], but is there any
> consensus or reason for there not being a "compose" function in standard
> OCaml, nor an infix operator?
>
> At the moment I tend to "let compose" or "let (<<-) f g x = f (g x)",
> but I wish I didn't have to!
>
> Thanks,
> Arlen
>
> [1]
>
> http://webcache.googleusercontent.com/search?q=cache:TcqI7o37il8J:pauillac.inria.fr/caml/caml-list/0720.html+ocaml+function+compose&cd=2&hl=en&ct=clnk&client=ubuntu
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

[-- Attachment #2: Type: text/html, Size: 2345 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Infix function composition operator
  2010-11-10  3:45 ` [Caml-list] " Yaron Minsky
@ 2010-11-10  4:37   ` Arlen Christian Mart Cuss
  2010-11-10 10:06   ` DS
  1 sibling, 0 replies; 5+ messages in thread
From: Arlen Christian Mart Cuss @ 2010-11-10  4:37 UTC (permalink / raw)
  To: yminsky; +Cc: caml-list

Hi Yaron,

On Tue, 2010-11-09 at 22:45 -0500, Yaron Minsky wrote:
> This is probably a minority opinion, but I have written and read quite
> a lot of OCaml code over the years, and I've seen surprisingly few
> effective uses of the composition operator.  Somehow, I usually find
> that code that avoids it is simpler and easier to read.

I know what you mean - it can make some code more obtuse than just doing
the same thing with a lambda. But I can't help but get the feeling it
has a place here: (note: example is semi-contrived and there are plenty
of better ways to do this, but just as an example)

module CGI =
struct
(* ... *)
  let escape =
    let replace = Str.global_replace <<- Str.regexp_string in
    (replace "\"" "&quot;") <<- (replace "'" "&#039;") <<- (replace "&"
"&amp;")

Particularly the second line of `escape', where the escaping mechanism
is indeed a composition of the three replaces as specified.

Comparing that to this:

let escape s = 
  let replace = (* ... *) in
  replace "\"" "&quot;" (replace "'" "&#039;" (replace "&" "&amp;" s))

.. I prefer the former, as the latter forces me to specify an argument,
and nest the calls to replace. I'm not sure if there are any performance
benefits/losses as a result of computing the function `escape' in the
first example, either, but it's a consideration.

> I'm not averse to infix operators.  At Jane Street we've found the
> following sequencing operator to be highly useful:
> 
> let ( |! ) x f = f x
> 
> and it is indeed part of the default include in Jane Street's Core
> library.

That looks neat, and I imagine it to be useful, but I can't think of a
concrete use-case off the cuff. Could you give an example?

> y

Cheers,
Arlen

> On Tue, Nov 9, 2010 at 10:19 PM, Arlen Christian Mart Cuss
> <arlen@noblesamurai.com> wrote:
>         Hi all,
>         
>         I know this was asked at least 12 years ago[1], but is there
>         any
>         consensus or reason for there not being a "compose" function
>         in standard
>         OCaml, nor an infix operator?
>         
>         At the moment I tend to "let compose" or "let (<<-) f g x = f
>         (g x)",
>         but I wish I didn't have to!
>         
>         Thanks,
>         Arlen
>         
>         [1]
>         http://webcache.googleusercontent.com/search?q=cache:TcqI7o37il8J:pauillac.inria.fr/caml/caml-list/0720.html+ocaml+function+compose&cd=2&hl=en&ct=clnk&client=ubuntu
>         
>         _______________________________________________
>         Caml-list mailing list. Subscription management:
>         http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
>         Archives: http://caml.inria.fr
>         Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>         Bug reports: http://caml.inria.fr/bin/caml-bugs
> 
> 
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Infix function composition operator
  2010-11-10  3:45 ` [Caml-list] " Yaron Minsky
  2010-11-10  4:37   ` Arlen Christian Mart Cuss
@ 2010-11-10 10:06   ` DS
  1 sibling, 0 replies; 5+ messages in thread
From: DS @ 2010-11-10 10:06 UTC (permalink / raw)
  To: caml-list

On 10 Nov 2010, at 4:45, Yaron Minsky wrote:
> let ( |! ) x f = f x
This is the same as F#'s pipe operator (|>) which I find extremely useful because with it code reads the way it works, not backwards:
2 |> tenfold |> add 1 |> double  => 42
(ok, this is a ridiculous example, but you get the point).
Regards.
-DS

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Infix function composition operator
  2010-11-10  3:19 Infix function composition operator Arlen Christian Mart Cuss
  2010-11-10  3:45 ` [Caml-list] " Yaron Minsky
@ 2010-11-10 13:23 ` Michael Ekstrand
  1 sibling, 0 replies; 5+ messages in thread
From: Michael Ekstrand @ 2010-11-10 13:23 UTC (permalink / raw)
  To: caml-list

On 11/09/2010 09:19 PM, Arlen Christian Mart Cuss wrote:
> Hi all,
> 
> I know this was asked at least 12 years ago[1], but is there any
> consensus or reason for there not being a "compose" function in standard
> OCaml, nor an infix operator?
> 
> At the moment I tend to "let compose" or "let (<<-) f g x = f (g x)",
> but I wish I didn't have to!

Batteries[1] includes such an operator in its Std module and its
extension of Pervasives.  The compose operator is is '-|', and the
reverse compose operator is '|-' ((f |- g) x = g (f x)).  It also
provides the pipeline operator '|>' and its converse application
operator '**>'.

- Michael

1. http://batteries.forge.ocamlcore.org


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-11-10 13:23 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-11-10  3:19 Infix function composition operator Arlen Christian Mart Cuss
2010-11-10  3:45 ` [Caml-list] " Yaron Minsky
2010-11-10  4:37   ` Arlen Christian Mart Cuss
2010-11-10 10:06   ` DS
2010-11-10 13:23 ` Michael Ekstrand

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).