|
View:
New views
7 Messages
—
Rating Filter:
Alert me
|
|
|
Explicitly access an orderingI am implementing an Ordering on a class that contains an Int
attribute which is what I want to order by. So I have code like: new Ordering[Foo] { def compare(foo1:Foo, foo2: Foo) = whatHere(foo1.weight, foo2.weight) but what can I put for "whatHere"? Obviously I can explicitly write out the comparison logic, but I was hoping to do something like Int.compare(....) or similar. Thanks, Andrew |
|
|
Re: Explicitly access an orderingOn Monday November 2 2009, andrew cooke wrote:
> I am implementing an Ordering on a class that contains an Int > attribute which is what I want to order by. So I have code like: > > new Ordering[Foo] { > def compare(foo1:Foo, foo2: Foo) = > whatHere(foo1.weight, foo2.weight) > > but what can I put for "whatHere"? Obviously I can explicitly write > out the comparison logic, but I was hoping to do something like > Int.compare(....) or similar. I had to look up Ordering (under the assumption that you were not making up Ordering, I looked in the 2.8 docs). There I find 13 "Direct Known Subclasses" including "Ordering.IntOrdering" which in turn is reified as object IntIsIntegral. Thus I believe you could just put "IntIsIntegral.compare" in place of your "whatHere". > Thanks, > Andrew Randall Schulz |
|
|
Re: Explicitly access an orderingOrdering is a contravariant functor. Therefore, there could
theoretically exist a comap function on Ordering[A]: (B => A) => Ordering[B]. Then your function could be as simple as comap (_.weight). While no library function exists, there are ways of declaring it in your own library (i.e. a ContraFunctor type-class, see Scalaz). Contravariant functor comprehensions anyone? Just kidding. andrew cooke wrote: > I am implementing an Ordering on a class that contains an Int > attribute which is what I want to order by. So I have code like: > > new Ordering[Foo] { > def compare(foo1:Foo, foo2: Foo) = > whatHere(foo1.weight, foo2.weight) > > but what can I put for "whatHere"? Obviously I can explicitly write > out the comparison logic, but I was hoping to do something like > Int.compare(....) or similar. > > Thanks, > Andrew > > -- Tony Morris http://tmorris.net/ |
|
|
Re: Explicitly access an orderingandrew cooke wrote:
> I am implementing an Ordering on a class that contains an Int > attribute which is what I want to order by. So I have code like: > > new Ordering[Foo] { > def compare(foo1:Foo, foo2: Foo) = > whatHere(foo1.weight, foo2.weight) > > but what can I put for "whatHere"? Obviously I can explicitly write > out the comparison logic, but I was hoping to do something like > Int.compare(....) or similar. def attributeOrdering[A, B](f: A => B) (implicit ordering: Ordering[B]): Ordering[A] = new Ordering[A] { def compare(first:A, second: A) = ordering.compare(f(first), f(second)) } implicit val fooOrdering = attributeOrdering[Foo, Int](_.weight) or new Ordering[Foo] { def compare(foo1:Foo, foo2: Foo) = implicitly[Ordering[Int]].compare(foo1.weight, foo2.weight) } using Predef's def implicitly[T](implicit e: T) = e |
|
|
Re: Explicitly access an orderingFirst, sorry for not mentioning 2.8.
Second, I looked at the docs and found Ordering.IntOrdering, but didn't get past that point. Unfortunately the docs seem to be missing right now so I'll check again later (or someone will correct my other email in a minute I guess). But how do you find out what the reification of a trait is? Is there an "implemented by" link that I just missed? I guess I will see... Thanks, Andrew 2009/11/2 Randall R Schulz <rschulz@...>: > On Monday November 2 2009, andrew cooke wrote: >> I am implementing an Ordering on a class that contains an Int >> attribute which is what I want to order by. So I have code like: >> >> new Ordering[Foo] { >> def compare(foo1:Foo, foo2: Foo) = >> whatHere(foo1.weight, foo2.weight) >> >> but what can I put for "whatHere"? Obviously I can explicitly write >> out the comparison logic, but I was hoping to do something like >> Int.compare(....) or similar. > > I had to look up Ordering (under the assumption that you were not making > up Ordering, I looked in the 2.8 docs). There I find 13 "Direct Known > Subclasses" including "Ordering.IntOrdering" which in turn is reified > as object IntIsIntegral. Thus I believe you could just > put "IntIsIntegral.compare" in place of your "whatHere". > > >> Thanks, >> Andrew > > > Randall Schulz > |
|
|
Re: Re: Explicitly access an orderingThanks. I think I need to read through Predef.
Andrew 2009/11/2 Eric Willigers <ewilligers@...>: > using Predef's > def implicitly[T](implicit e: T) = e > > |
|
|
Re: Explicitly access an orderingOn Tuesday November 3 2009, andrew cooke wrote:
> First, sorry for not mentioning 2.8. > > Second, I looked at the docs and found Ordering.IntOrdering, but > didn't get past that point. Unfortunately the docs seem to be > missing right now so I'll check again later (or someone will correct > my other email in a minute I guess). But how do you find out what > the reification of a trait is? Is there an "implemented by" link > that I just missed? I guess I will see... I just looked at its "Direct Known Subclasses" for an object and found IntIsIntegral. That section is present on every Scaladoc trait or class detail page that has subclasses (or traits). > Thanks, > Andrew Randall Schulz |
| Free embeddable forum powered by Nabble | Forum Help |