Mohamed Bana | 9 Feb 22:05
Picon

sort on two keys

Hi All,

What would be the equivalent of the C# program below?

var counts = new Dictionary<string, int>();

var sortedDict =
  from entry in counts
  orderby entry.Value descending, entry.Key ascending
  select entry;

that is, sort by max no. then alphabetically.

—Mohamed

Landei | 10 Feb 00:28
Picon
Picon

Re: sort on two keys


Mohamed Bana-6 wrote:

> > Hi All, > > What would be the equivalent of the C# program below? > > var counts = new Dictionary<string, int>(); > > var sortedDict = > from entry in counts > orderby entry.Value descending, entry.Key ascending > select entry; > > that is, sort by max no. then alphabetically. > > —Mohamed > >
I hope I got it right... val m = Map("one"->1,"once"->1,"two"->2,"twice"->2,"double"->2,"three"->3) m.toList.sort((x,y) => if (x._2 == y._2) x._1 < y._1 else x._2 > y._2) //--> res1: List[(java.lang.String, Int)] = List((three,3), (double,2), (twice,2), (two,2), (once,1), (one,1)) Cheers, Daniel -- -- View this message in context: http://old.nabble.com/-scala--sort-on-two-keys-tp27522069p27523939.html Sent from the Scala mailing list archive at Nabble.com.
David Pollak | 9 Feb 01:44
Picon
Gravatar

Very weird error

Folks,

I'm working on some Goat Rodeo code and I'm running into a huge problem with compound types.

I'm got a case class that holds a message and a function to execute on response from the message:

MessageWithAction[ActionType <: QBase,
                                   MsgType <: QBase with
                   MsgWithResponse[ActionType]](msg: MsgType,
 timeout: Long,
 action: Option[ActionType] => Unit)

I've got a worker that takes a MessageWithAction as a parameter in one its methods:

trait Worker[IdType <: WorkerId, MsgType] {
  /**
   * The unique ID of the Work
   */
  def id: IdType

  /**
   * Send a message to the worker
   */
  def !(msg: MsgType): Unit

  /**
   * Send a message to along with a callback
   */
  def !![T <: QBase](msgWithAction: MessageWithAction[T, MsgType with MsgWithResponse[T]]): Unit
}

And I've got a message:

case class MooFu(str: String) extends SimpleMsg with MsgWithResponse[QLong]

Now, when I try to send that message:

 val q = MessageWithAction(MooFu("Hello"), 400L,  (a: Option[QLong]) => println(a))

  n !! q

The compiler complains:
InferThis.scala:73: error: type mismatch;
 found   : MessageWithAction[QLong,MooFu]
 required: MessageWithAction[?,SimpleMsg with MsgWithResponse[?]]
  n !! q
       ^

I try to be nice and explicitly tell the compiler that the type parameter for !! is QLong, but I still get:

InferThis.scala:76: error: type mismatch;
 found   : MessageWithAction[QLong,MooFu]
 required: MessageWithAction[QLong,SimpleMsg with MsgWithResponse[QLong]]
  n.!![QLong](q)

I'm enclosing a complete example.

So, why doesn't the compiler recognize MooFu as a SimpleMsg with MsgWithResponse[QLong]?  How can I help things along?

Thanks,

David



--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics
Attachment (InferThis.scala): text/x-scala, 1550 bytes
Windemuth Andreas | 9 Feb 05:41
Picon

Re: Very weird error


Could it be because MessageWithAction is not covariant in MsgType?

Andreas


From: David Pollak <feeder.of.the.bears <at> gmail.com>
To: Scala list <scala <at> listes.epfl.ch>
Sent: Mon, February 8, 2010 7:44:47 PM
Subject: [sc ala] Very weird error

Folks,

I'm working on some Goat Rodeo code and I'm running into a huge problem with compound types.

I'm got a case class that holds a message and a function to execute on response from the message:

MessageWithAction[ActionType <: QBase,
                                   MsgType <: QBase with
                   MsgWithResponse[ActionType]](msg: MsgType,
 timeout: Long,
 action: Option[ActionType] => Unit)

I've got a worker that takes a MessageWithAction as a parameter in one its methods:

trait Worker[IdType <: WorkerId, MsgType] {
  /**
   * The unique ID of the Work
   */
  def id: IdType

  /**
   * Send a message to the worker
   */
  def !(msg: MsgType): Unit

  /**
   * Send a message to along with a callback
   */
  def !![T <: QBase](msgWithAction: MessageWithAction[T, MsgType with MsgWithResponse[T]]): Unit
}

And I've got a message:

case class MooFu(str: String) extends SimpleMsg with MsgWithResponse[QLong]

Now, when I try to send that message:

 val q = MessageWithAction(MooFu("Hello"), 400L,  (a: Option[QLong]) => println(a))

  n !! q

The compiler complains:
InferThis.scala:73: error: type mismatch;
 found   : MessageWithAction[QLong,MooFu]
 required: MessageWithAction[?,SimpleMsg with MsgWithResponse[?]]
  n !! q
       ^

I try to be nice and explicitly tell the compiler that the type parameter for !! is QLong, but I still get:

InferThis.scala:76: error: type mismatch;
 found   : MessageWithAction[QLong,MooFu]
 required: MessageWithAction[QLong,SimpleMsg with MsgWithResponse[QLong]]
  n.!![QLong](q)

I'm enclosing a complete example.

So, why doesn't the compiler recognize MooFu as a SimpleMsg with MsgWithResponse[QLong]?  How can I help things along?

Thanks,

David



--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics

David Pollak | 9 Feb 17:28
Picon
Gravatar

Re: Very weird error



On Mon, Feb 8, 2010 at 8:41 PM, Windemuth Andreas <windemut <at> yahoo.com> wrote:

Could it be because MessageWithAction is not covariant in MsgType?

Yep... PaulP showed me the errors of my ways at the Scala BASE meeting last night.  Thanks all!
 

Andreas


From: David Pollak <feeder.of.the.bears <at> gmail.com>
To: Scala list <scala <at> listes.epfl.ch>
Sent: Mon, February 8, 2010 7:44:47 PM
Subject: [scala] Very weird error

Folks,

I'm working on some Goat Rodeo code and I'm running into a huge problem with compound types.

I'm got a case class that holds a message and a function to execute on response from the message:

MessageWithAction[ActionType <: QBase,
                                   MsgType <: QBase with
                   MsgWithResponse[ActionType]](msg: MsgType,
 timeout: Long,
 action: Option[ActionType] => Unit)

I've got a worker that takes a MessageWithAction as a parameter in one its methods:

trait Worker[IdType <: WorkerId, MsgType] {
  /**
   * The unique ID of the Work
   */
  def id: IdType

  /**
   * Send a message to the worker
   */
  def !(msg: MsgType): Unit

  /**
   * Send a message to along with a callback
   */
  def !![T <: QBase](msgWithAction: MessageWithAction[T, MsgType with MsgWithResponse[T]]): Unit
}

And I've got a message:

case class MooFu(str: String) extends SimpleMsg with MsgWithResponse[QLong]

Now, when I try to send that message:

 val q = MessageWithAction(MooFu("Hello"), 400L,  (a: Option[QLong]) => println(a))

  n !! q

The compiler complains:
InferThis.scala:73: error: type mismatch;
 found   : MessageWithAction[QLong,MooFu]
 required: MessageWithAction[?,SimpleMsg with MsgWithResponse[?]]
  n !! q
       ^

I try to be nice and explicitly tell the compiler that the type parameter for !! is QLong, but I still get:

InferThis.scala:76: error: type mismatch;
 found   : MessageWithAction[QLong,MooFu]
 required: MessageWithAction[QLong,SimpleMsg with MsgWithResponse[QLong]]
  n.!![QLong](q)

I'm enclosing a complete example.

So, why doesn't the compiler recognize MooFu as a SimpleMsg with MsgWithResponse[QLong]?  How can I help things along?

Thanks,

David



--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics




--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics
Blair Zajac | 8 Feb 17:06

Adding ARM to Scala's standard library

Probably anybody that has written something that needs to be closed after use 
has written something like

object With
{
   def closeable[C <: java.io.Closeable,R](c: C)
                                          (f: C => R): R =
   {
     try {
       f(c)
     }
     finally {
       try {
        c.close()
       }
       catch {
         // #### Log this, but how?
         case e =>
       }
     }
   }
}

Then it would be used as

With.closeable(new ByteArrayOutputStream(1024)) { os =>
   ...
}

This looks like something that would do well in Scala's standard library.

One question is how to deal with the logger for the standard library.  I use 
slf4j in my projects but not everybody does.  I think if this code is in the 
standard library then it must provide a way to have the exception from #close() 
be logged with the logger of choice.  The With object could become an abstract 
class with an abstract logging method than projects would instantiate their own 
concrete class with their logger of choice.

abstract class With
{
   protected def logCloseException(t: Throwable): Unit
}

object MyWith
   extends With
{
   override protected def logCloseException(t: Throwable): Unit = ...
}

BTW, the reason not to use structural subtyping is performance and it doesn't 
work with Oracle's java.sql.ResultSet subclass:

   def with_closeable[T <: { def close(): Unit }]

   java.lang.IllegalAccessException: Class DatabaseCopy$ can not
   access a member of class oracle.jdbc.driver.OracleResultSetImpl
   with modifiers "public synchronized"
   at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
   at java.lang.reflect.Method.invoke(Method.java:588)

Blair

Paul Phillips | 8 Feb 17:29

Re: Adding ARM to Scala's standard library

On Mon, Feb 08, 2010 at 08:06:41AM -0800, Blair Zajac wrote:
> This looks like something that would do well in Scala's standard 
> library.

I thought you were here the last few months.

> BTW, the reason not to use structural subtyping is performance and it 
> doesn't work with Oracle's java.sql.ResultSet subclass:

http://lampsvn.epfl.ch/trac/scala/ticket/2318
"Structural method is dispatched on non-public member when overriding public member"

--

-- 
Paul Phillips      | Atheists dig the best foxholes.
In Theory          | 
Empiricist         | 
slap pi uphill!    |----------* http://www.improving.org/paulp/ *----------

Blair Zajac | 8 Feb 17:41

Re: Adding ARM to Scala's standard library

On Feb 8, 2010, at 8:29 AM, Paul Phillips wrote:

> On Mon, Feb 08, 2010 at 08:06:41AM -0800, Blair Zajac wrote:
>> This looks like something that would do well in Scala's standard 
>> library.
> 
> I thought you were here the last few months.

I've got 19,000 unread messages in my Scala mailbox and not enough time to keep up on everything :(

I found Josh's thread mentioning scalax.io, but the source repository at hg.scalaforge.org is down.

Is the most current discussion at
http://groups.google.com/group/scala-incubator/web/arm-questions ?

> 
>> BTW, the reason not to use structural subtyping is performance and it 
>> doesn't work with Oracle's java.sql.ResultSet subclass:
> 
> http://lampsvn.epfl.ch/trac/scala/ticket/2318
> "Structural method is dispatched on non-public member when overriding public member"

Thanks.

Blair

Josh Suereth | 9 Feb 16:51
Picon
Gravatar

Re: Adding ARM to Scala's standard library

Blair, there's actually a project on github here:

http://github.com/jsuereth/scala-arm

I need to get back into it and tweak the thing.  I think my experiment with "leaky monads" is failed, but let me know what you think or if you have any questions.


- josh

On Mon, Feb 8, 2010 at 11:41 AM, Blair Zajac <blair <at> orcaware.com> wrote:
On Feb 8, 2010, at 8:29 AM, Paul Phillips wrote:

> On Mon, Feb 08, 2010 at 08:06:41AM -0800, Blair Zajac wrote:
>> This looks like something that would do well in Scala's standard
>> library.
>
> I thought you were here the last few months.

I've got 19,000 unread messages in my Scala mailbox and not enough time to keep up on everything :(

I found Josh's thread mentioning scalax.io, but the source repository at hg.scalaforge.org is down.

Is the most current discussion at http://groups.google.com/group/scala-incubator/web/arm-questions ?

>
>> BTW, the reason not to use structural subtyping is performance and it
>> doesn't work with Oracle's java.sql.ResultSet subclass:
>
> http://lampsvn.epfl.ch/trac/scala/ticket/2318
> "Structural method is dispatched on non-public member when overriding public member"

Thanks.

Blair


Blair Zajac | 9 Feb 17:21

Re: Adding ARM to Scala's standard library

Josh,

Thanks, I'll take a look.

BTW, with my sample "With" object, I later realized that different projects will 
want different behavior when close() throws an exception, some will want to 
silence it and others will want to propagate it if the try block didn't throw an 
exception.  So I would need a With.closeable() and With.closeableQuietly().

Blair

Josh Suereth wrote:

> Blair, there's actually a project on github here: > > http://github.com/jsuereth/scala-arm > > I need to get back into it and tweak the thing. I think my experiment > with "leaky monads" is failed, but let me know what you think or if you > have any questions. > > > - josh > > On Mon, Feb 8, 2010 at 11:41 AM, Blair Zajac <blair <at> orcaware.com > <mailto:blair <at> orcaware.com>> wrote: > > On Feb 8, 2010, at 8:29 AM, Paul Phillips wrote: > > > On Mon, Feb 08, 2010 at 08:06:41AM -0800, Blair Zajac wrote: > >> This looks like something that would do well in Scala's standard > >> library. > > > > I thought you were here the last few months. > > I've got 19,000 unread messages in my Scala mailbox and not enough > time to keep up on everything :( > > I found Josh's thread mentioning scalax.io <http://scalax.io>, but > the source repository at hg.scalaforge.org > <http://hg.scalaforge.org> is down. > > Is the most current discussion at > http://groups.google.com/group/scala-incubator/web/arm-questions ? > > > > >> BTW, the reason not to use structural subtyping is performance > and it > >> doesn't work with Oracle's java.sql.ResultSet subclass: > > > > http://lampsvn.epfl.ch/trac/scala/ticket/2318 > > "Structural method is dispatched on non-public member when > overriding public member" > > Thanks. > > Blair > >
-- -- Blair Zajac, Ph.D. CTO, OrcaWare Technologies <blair <at> orcaware.com> Subversion training, consulting and support http://www.orcaware.com/svn/
Josh Suereth | 9 Feb 20:38
Picon
Gravatar

Re: Adding ARM to Scala's standard library

Blair,

The ARM library I was working on actually has the ability to return an Either[List[Throwable],T].   This lets you decide what to do with your exceptions.

Also ->

map and flatMap are assumed to be lazily executed (i.e. you can't acquire/release the resource if you're modifying it)
foreach is assumed to be eagerly executed (you almost have to do this... otherwise when will you run it?)


The final fun was if you pass a function f of Resouce => ? <: Traverable[T], we made the assumption that it would be safe to return a "managed" Traversable.   The returned traversable will open the resource, call the mapping function, call foreach on the internal traversable, and then close the resource when done.   This was my first attempt to see if I could use the type system to determine when it is safe to extract from a monad.  This is the piece that's strange...

basically I wanted this to work:

val lines = for { file <- managed(new java.io.File("some.txt"))
       stream <- new java.io.FileInputStream(file)
       line <- makeLineIterator(stream) } yield line

Which... does in fact work as I wanted it to... at times.   Anyway, I'm still fighting with whether or not the auto-leak feature is useful.  If not, the library can be greatly simplified.

Also... one thing I was using this for was RMI/JNDI.   In that case I wanted to create a managed resource in a configurable manner...  i.e. one that would 'retry" the remote method on failure after some specified timeout.  This also required some threading magic and contexts.   I'll try to get the code open-source to see if it's of any use to others.

- Josh


On Tue, Feb 9, 2010 at 11:21 AM, Blair Zajac <blair <at> orcaware.com> wrote:
Josh,

Thanks, I'll take a look.

BTW, with my sample "With" object, I later realized that different projects will want different behavior when close() throws an exception, some will want to silence it and others will want to propagate it if the try block didn't throw an exception.  So I would need a With.closeable() and With.closeableQuietly().

Blair

Josh Suereth wrote:
Blair, there's actually a project on github here:

http://github.com/jsuereth/scala-arm

I need to get back into it and tweak the thing.  I think my experiment with "leaky monads" is failed, but let me know what you think or if you have any questions.


- josh

On Mon, Feb 8, 2010 at 11:41 AM, Blair Zajac <blair <at> orcaware.com <mailto:blair <at> orcaware.com>> wrote:

   On Feb 8, 2010, at 8:29 AM, Paul Phillips wrote:

    > On Mon, Feb 08, 2010 at 08:06:41AM -0800, Blair Zajac wrote:
    >> This looks like something that would do well in Scala's standard
    >> library.
    >
    > I thought you were here the last few months.

   I've got 19,000 unread messages in my Scala mailbox and not enough
   time to keep up on everything :(

   I found Josh's thread mentioning scalax.io <http://scalax.io>, but

   the source repository at hg.scalaforge.org
   <http://hg.scalaforge.org> is down.


   Is the most current discussion at
   http://groups.google.com/group/scala-incubator/web/arm-questions ?

    >
    >> BTW, the reason not to use structural subtyping is performance
   and it
    >> doesn't work with Oracle's java.sql.ResultSet subclass:
    >
    > http://lampsvn.epfl.ch/trac/scala/ticket/2318
    > "Structural method is dispatched on non-public member when
   overriding public member"

   Thanks.

   Blair




--
Blair Zajac, Ph.D.
CTO, OrcaWare Technologies
<blair <at> orcaware.com>
Subversion training, consulting and support
http://www.orcaware.com/svn/


Gmane