"on" combinator #516

Open
c-cube opened this Issue Jan 20, 2014 · 8 comments

Comments

Projects
None yet
4 participants
Member

c-cube commented Jan 20, 2014

In haskell's standard library there is that nice combinator, on, whose signature would be:

val on : ('b -> 'b -> 'c) -> ('a -> 'b) -> 'a -> 'a -> 'c

and it would be used like this:

# List.sort (compare `on` snd) [(1,2); (4,0)]
int list :- [4,0; 1,2]

Would there be a place in Batteries for this (or a proper infix synonym)? It looks like all combinators are directly in BatPervasives, but that may be too much. Would a BatFun collection of higher-order combinators be useful?

Owner

gasche commented Jan 20, 2014

I think having something like on somewhere would be useful, yes.

On Mon, Jan 20, 2014 at 10:57 PM, Simon Cruanes notifications@github.comwrote:

In haskell's standard library there is that nice combinator, on, whose
signature would be:

val on : ('b -> 'b -> 'c) -> ('a -> 'b) -> 'a -> 'a -> 'c

and it would be used like this:

List.sort (compare on snd) [(1,2); (4,0)]int list :- [4,0; 1,2]

Would there be a place in Batteries for this (or a proper infix synonym)?
It looks like all combinators are directly in BatPervasives, but that may
be too much. Would a BatFun collection of higher-order combinators be
useful?


Reply to this email directly or view it on GitHubhttps://github.com/ocaml-batteries-team/batteries-included/issues/516
.

Member

UnixJunkie commented Jan 21, 2014

Looks quite useful indeed.

Owner

thelema commented Jan 21, 2014

There's a couple (not quite as general) groups of functions in BatOrd:


val map_eq : ('a -> 'b) -> 'b eq -> 'a eq
val map_comp : ('a -> 'b) -> 'b comp -> 'a comp
val map_ord : ('a -> 'b) -> 'b ord -> 'a ord
val eq_by : ('a -> 'b) -> 'a eq
val comp_by : ('a -> 'b) -> 'a comp
val ord_by : ('a -> 'b) -> 'a ord

on works in haskell because functions can be made infix, so it reads
nicely. I want to see what you come up with in OCaml that's anywhere near
as nice.

On Mon, Jan 20, 2014 at 8:34 PM, Francois Berenger <notifications@github.com

wrote:

Looks quite useful indeed.


Reply to this email directly or view it on GitHubhttps://github.com/ocaml-batteries-team/batteries-included/issues/516#issuecomment-32813763
.

Member

c-cube commented Jan 21, 2014

let's try:

# let (%<) f g x y = f (g x)(g y);;
val (%<) : ('b -> 'b -> 'c) -> ('a -> 'b) -> 'a -> 'a -> 'c
# List.sort (compare %< snd) [(1,2); (4,0)];;
int list :- [4,0; 1,2]

it's not especially pretty, I have to admit, I tried to keep the "compose+fork" idea. Anyway something with % sounds good, because it's a kind of composition. I'm not sure it should be in Pervasives though.

Owner

gasche commented Jan 21, 2014

I'm not fond of arbitrary infix operators. They look good one night of despair, and you regret them the morning after. Why don't we pick a sane name and drop the idea of using it in infix position? We could simply add on in BatOrd if it generalizes the other combinators.

In your example, I think it would be just as readable to use List.sort (Ord.comp_by snd) [(1,2); (4,0)].

Member

c-cube commented Jan 21, 2014

@gasche agreed, we don't need it to be infix. However, it's much more general than a Ord combinator, so I don't think it belongs to Ord. What about compose_on or something like this?

Owner

gasche commented Jan 21, 2014

You could simply call it on. If it's judged too specialized to be in an opened-by-default module, I think Ord makes sense. That said, it could also go in your hypotehetical Fun module and then be part of a zoology of pre-compose and post-compose functions of multiple arities: pre1, pre2, post1, post2, etc., with on being an alias for pre2.

Member

c-cube commented Jan 21, 2014

Why not just on, indeed. I'll think about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment