This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Really? Why? What Is It??

8 replies
Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.

Hi,

Some mysterious stranger scrawled this on the walls of #scala:

[20:37:43] @type on -- Scala needs this function
[20:37:44] forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
[20:41:10] scala> implicit def FunctionOn[T, R](f: T => R) = new { def on[X](g: (R, R) => X): (T, T) => X = (t1: T, t2: T) => g(f(t1), f(t2)) }
[20:41:11] FunctionOn: [T,R]((T) => R)java.lang.Object{def on[X]((R, R) => X): (T, T) => X}
[20:41:11] scala> val k = ((_:String).toInt) on (_ * _)
[20:41:11] k: (String, String) => Int =
[20:41:11] scala> k("88", "99")
[20:41:12] res5: Int = 8712

The stranger's accomplice, one "lambdabot," seems to speak a
different language than the one true Scala.

What does it mean? Why does Scala need this "on" thingamajig?
Will it make my dishes sparkle? My teeth whiter? Will it clean my
cat's litterbox? (I sure hope it doesn't raise an army of zombies
from nearby cemeteries—I live not too far from America's only
necropolis!)

Why do I want this? Do I want it? What's it going to cost me?

RRS

Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.
Re: Really? Why? What Is It??

Randall R Schulz wrote:
> Hi,
>
> Some mysterious stranger scrawled this on the walls of #scala:
>
> [20:37:43] @type on -- Scala needs this function
> [20:37:44] forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
> [20:41:10] scala> implicit def FunctionOn[T, R](f: T => R) = new { def on[X](g: (R, R) => X): (T, T) => X = (t1: T, t2: T) => g(f(t1), f(t2)) }
> [20:41:11] FunctionOn: [T,R]((T) => R)java.lang.Object{def on[X]((R, R) => X): (T, T) => X}
> [20:41:11] scala> val k = ((_:String).toInt) on (_ * _)
> [20:41:11] k: (String, String) => Int =
> [20:41:11] scala> k("88", "99")
> [20:41:12] res5: Int = 8712
>
> The stranger's accomplice, one "lambdabot," seems to speak a
> different language than the one true Scala.
>
> What does it mean? Why does Scala need this "on" thingamajig?
> Will it make my dishes sparkle? My teeth whiter? Will it clean my
> cat's litterbox? (I sure hope it doesn't raise an army of zombies
> from nearby cemeteries—I live not too far from America's only
> necropolis!)
>
> Why do I want this? Do I want it?
Hello Randall,
There is an example given of why you might want it. I can think of many
other examples. Here's one:

scala> val t = (_: String).toInt
t: (String) => Int =

scala> val p = List("56", "78", "97", "45", "34564", "3", "54", "77")
sort (t on (_ < _))
p: List[java.lang.String] = List(3, 45, 54, 56, 77, 78, 97, 34564)

> What's it going to cost me?
>

A patch I suppose.

>
> RRS
>
>

Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.
Re: Really? Why? What Is It??

On Monday March 30 2009, Tony Morris wrote:
> Randall R Schulz wrote:
> > Hi,
> >
> > Some mysterious stranger scrawled this on the walls of #scala:
> >
> > [20:37:43] @type on -- Scala needs this function
> > [20:37:44] forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
> > [20:41:10] scala> implicit def FunctionOn[T, R](f: T => R) = new { def on[X](g: (R, R) => X): (T, T) => X = (t1: T, t2: T) => g(f(t1), f(t2)) }
> > [20:41:11] FunctionOn: [T,R]((T) => R)java.lang.Object{def on[X]((R, R) => X): (T, T) => X}
> > [20:41:11] scala> val k = ((_:String).toInt) on (_ * _)
> > [20:41:11] k: (String, String) => Int =
> > [20:41:11] scala> k("88", "99")
> > [20:41:12] res5: Int = 8712
> >
> > ...
>
> Hello Randall,
> There is an example given of why you might want it. I can think of
> many other examples. Here's one:

How would you paraphrase it's meaning in English? Apart from examples
of converting strings to integers, what general class of problems does
it solve?

> ...

Randall Schulz

Martin S. Weber
Joined: 2008-12-23,
User offline. Last seen 42 years 45 weeks ago.
Re: Really? Why? What Is It??

Quoting Randall R Schulz :

> On Monday March 30 2009, Tony Morris wrote:
>> Randall R Schulz wrote:
>> > Hi,
>> >
>> > Some mysterious stranger scrawled this on the walls of #scala:
>> >
>> > [20:37:43] @type on -- Scala needs this function
>> > [20:37:44] forall b c a. (b -> b -> c) -> (a -> b) ->
>> a -> a -> c
(...)
> (Randall again:)
> How would you paraphrase it's meaning in English?

forall: A 2nd higher order function of three type parameters. It will
take a function of a 2-tuple of parameters of type "b" which returns a
value of type "c". It will then return a function, which, when given a
conversion from type "a" to type "b" will be applicable to a 2-tuple
of arguments of type "a" and return a value of type "c".

Or in other words, given the existance of a conversion from "a" to
"b", and given a function that takes two "b" parameters and returns a
"c", this function returns a function that takes two "a" parameters
instead, returning a "c".

> (still Randall:) Apart from examples
> of converting strings to integers, what general class of problems does
> it solve?

Look closely. It's not converting strings to integers, the example is
applying an integer addition on string arguments. The real answer to
your question is left as an exercise to the reader..

-Martin

geoff
Joined: 2008-08-20,
User offline. Last seen 1 year 25 weeks ago.
Re: Really? Why? What Is It??

On Tue, Mar 31, 2009 at 06:10:36AM -0700, Randall R Schulz said
>
> How would you paraphrase it's meaning in English? Apart from examples
> of converting strings to integers, what general class of problems does
> it solve?

To me it reads better when defined the other way around:

implicit def FunctionOn[T,R](f: (T,T) => R) = new {
def on[X](g: X => T): (X,X) => R = (x1: X, x2: X) => f(g(x1), g(x2))
}

I think that this fits in with the idiomatic use in haskell (the
language lambdabot is speaking) as well.

The operator combines a two argument function (f) and a transform
function (g) into a new two argument function that applies g to the
incoming values before passing them to f.

Here's an example that might be a little more interesting.

Consider you've got a person class

case class Person(name: String, department: String)

and some people

val people = List(Person("Sue", "Sales"),
Person("Bob", "Marketing"),
Person("Ralph", "Sales"),
Person("James","Human Resources"),
Person("Judy","Sales"))

using this helper

def cmp[T <: Comparable[T]] = (x: T, y: T) => (x compareTo y) < 0

you can now do

people.sort(cmp[String] on (_.name))

and

people.sort(cmp[String] on (_.department))

to sort the people by their name and department, respectively.

The type annotation on cmp is there because scala unfortunately can't
infer all the types.

For the sorting idiom it is rougly equivalent to doing

people.map(_.name).sort(cmp[String])

but without creating an intermediate list and (IMO) a clearer intention.

Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.
Re: Really? Why? What Is It??

On Monday March 30 2009, Randall R Schulz wrote:
> Hi,
>
> Some mysterious stranger scrawled this on the walls of #scala:
>
> ...

Thanks Tony, Martin and Geoff for the explanations and elaborations. I
figured it was useful, I just couldn't decipher it.

I'd been told that people involved in FP have better-than-average senses
of humor, so I figured I'd go for the frivolous presentation. For the
record, I knew that lambdabot speaks Haskell.

Randall Schulz

Bryan
Joined: 2008-12-19,
User offline. Last seen 42 years 45 weeks ago.
Re: Really? Why? What Is It??
'on' certainly seems neat.  You can also achieve this sorting without an intermediate list using something like

people.sort{case (Person(n1, _), Person(n2, _)) => cmp(n1, n2)}

--Bryan

On Tue, Mar 31, 2009 at 1:27 PM, Randall R Schulz <rschulz@sonic.net> wrote:
On Monday March 30 2009, Randall R Schulz wrote:
> Hi,
>
> Some mysterious stranger scrawled this on the walls of #scala:
>
> ...

Thanks Tony, Martin and Geoff for the explanations and elaborations. I
figured it was useful, I just couldn't decipher it.

I'd been told that people involved in FP have better-than-average senses
of humor, so I figured I'd go for the frivolous presentation. For the
record, I knew that lambdabot speaks Haskell.


Randall Schulz

Bryan
Joined: 2008-12-19,
User offline. Last seen 42 years 45 weeks ago.
Re: Really? Why? What Is It??
D'oh.  This would give you the same sorting, but a List[Person] instead of List of names.  I jumped the gun.  Sorry.

--Bryan

On Tue, Mar 31, 2009 at 2:09 PM, Bryan <germish@gmail.com> wrote:
'on' certainly seems neat.  You can also achieve this sorting without an intermediate list using something like

people.sort{case (Person(n1, _), Person(n2, _)) => cmp(n1, n2)}

--Bryan

On Tue, Mar 31, 2009 at 1:27 PM, Randall R Schulz <rschulz@sonic.net> wrote:
On Monday March 30 2009, Randall R Schulz wrote:
> Hi,
>
> Some mysterious stranger scrawled this on the walls of #scala:
>
> ...

Thanks Tony, Martin and Geoff for the explanations and elaborations. I
figured it was useful, I just couldn't decipher it.

I'd been told that people involved in FP have better-than-average senses
of humor, so I figured I'd go for the frivolous presentation. For the
record, I knew that lambdabot speaks Haskell.


Randall Schulz


Bryan
Joined: 2008-12-19,
User offline. Last seen 42 years 45 weeks ago.
Re: Really? Why? What Is It??
Ok, I was correct.  I don't know what I was thinking.  For the sake of the people on this mailing list I will no longer click "send" without reading my message (and actually thinking) first.

Sorry for the troubles,
Bryan

On Tue, Mar 31, 2009 at 2:10 PM, Bryan <germish@gmail.com> wrote:
D'oh.  This would give you the same sorting, but a List[Person] instead of List of names.  I jumped the gun.  Sorry.

--Bryan

On Tue, Mar 31, 2009 at 2:09 PM, Bryan <germish@gmail.com> wrote:
'on' certainly seems neat.  You can also achieve this sorting without an intermediate list using something like

people.sort{case (Person(n1, _), Person(n2, _)) => cmp(n1, n2)}

--Bryan

On Tue, Mar 31, 2009 at 1:27 PM, Randall R Schulz <rschulz@sonic.net> wrote:
On Monday March 30 2009, Randall R Schulz wrote:
> Hi,
>
> Some mysterious stranger scrawled this on the walls of #scala:
>
> ...

Thanks Tony, Martin and Geoff for the explanations and elaborations. I
figured it was useful, I just couldn't decipher it.

I'd been told that people involved in FP have better-than-average senses
of humor, so I figured I'd go for the frivolous presentation. For the
record, I knew that lambdabot speaks Haskell.


Randall Schulz



Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland