Sunday, November 4, 2012

Scala: when you wish that Seq was a List

As a long-time Java dev, it is tempting to declare function parameters as Lists. This is appropriate if we require an ordered sequence, but usually we only care that we can iterate over it. Our functions are more general if they accept any Seq. [1]

However, now and then I really miss the cons operator and deconstructor pattern (::) that is defined on List. An excerpt:

   case Nil => Nil
   case head :: tail => something(head) :: recurse(tail)

If what I'm matching is a sequence instead of list, I can't use :: in either context. Here is the equivalent for a sequence:

    case Seq() => Seq()
    case Seq(head, tail @ _*) => something(head) +: recurse(tail)
  • cons :: is replaced by prepend +: 
  • The sequence deconstruction pattern _* matches 0 or more sequence elements after the first one[2]
  • Meanwhile, the @ operator links an identifier to a portion of the pattern, capturing the rest of the sequence in tail.
  • The empty sequence is equivalent to Nil in List.
Lists are prettier, but sequences are more general. Consider both when defining your methods.

-----------------
[1] Technically we could be declaring the argument as GenIterable, but we're not that hard-core. 
[2] google unapplySeq to learn more about _*

15 comments:

  1. It is our intention to drive the first Electric Van for approximately 90 days and discover all of the nuances that might come with an EV or if there are any design changes we would like to have included with future vans,” said Robichaud. here are the findings

    ReplyDelete
  2. Adapted from Chaim Potok’s 1967 novel by the writer and Aaron Posner, who also instructed this manufacturing. bounce house rentals pensacola fl

    ReplyDelete
  3. Be "low" or "lowliness" but "lowness" certainly drives it home. I might have used "lowsomeness" because it's more of a mouthful, more fun to pronounce. But I'll probably find myself using lowness from now on. the venus factor does it work

    ReplyDelete
  4. It is our objective to generate the first Power Van for 90 times and find out all of the technicalities that might come with an EV or if there are any style changes we would like to have involved with upcoming vehicles,” said Robichaud. (via Facebook)

    ReplyDelete
  5. It has no way of it. But there is another aspect of interest that not only can understand it, but already knows about it. There is a saying in the Zen customized, “Birth and lack of way of life are the outstanding problem.” That is where actual Buddhist work out needs primary. Igor Cornelsen

    ReplyDelete
  6. Moreover to monitoring moment-to-moment threats such as an beginning car or a decrease banister, our threat verifying starts to intuit a distant but progressively approaching dark thinking — the approaching end, the biggest boundary. Mike Livak

    ReplyDelete
  7. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. Thanks for such post and please keep it up. Susan McGall

    ReplyDelete
  8. Many company activities, like contractors’ businesses, knowledgeable considerably during the excellent economic economic downturn of the last few years. Jason Halpern

    ReplyDelete
  9. Thanks to an wide range of mobile cellphone programs and thinking handling, companies can often find out out most of the facts they need, and handle their day, without ever speaking with the house company office. Sultan Alhokai

    ReplyDelete
  10. Why not let the bubble burst, the recession linger for a year or two and then let the market and entrepreneurs get us out of this hole. Sam Tabar

    ReplyDelete
  11. I am a big fan of Knut Wicksell's theory of interest, prices and the cumulative process and have often felt his understanding of the rise and fall of prices has been over looked. http://ireport.cnn.com/docs/DOC-1149856

    ReplyDelete
  12. So the primary part here is that excellent company activities need to be strengthened by the whole market. We all need to do a better job of creating them a more useful experience. http://www.cbsnews.com/news/steve-jobs-family-photo-album/

    ReplyDelete
  13. If the technological innovation performs, and in this situation it seems that it does, it’s a win for everyone. One nice aspect about being the first organization to buy the vehicles is that Robichaud is fairly much engaged in any style changes that may be created to your vehicle. findthebest

    ReplyDelete
  14. "It was a constant procedure. When it very first started, it was just creating files in my residence. I did not have a team name, was not enjoying stay, did not have a group; it could be anything, and it was fairly different things. Marc Sparks

    ReplyDelete
  15. I am a big fan of Knut Wicksell's concept of attention, costs and the collective procedure and have often experienced his knowing of the go up and down of costs has been over seemed. Google

    ReplyDelete