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 _*

36 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
  16. I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post. Free PDF Ebooks

    ReplyDelete
  17. As almost everyone’s expenses knowledgeable, many organizations had to cut back on their display participation and the wide variety of individuals they sent to be existing at activities was considerably reduced. Consequently, some reveals stopped to are available or became much smaller versions of their halcyon periods. truth about cellulite by joey atlas ebook

    ReplyDelete
  18. The A.D. White-colored selection of traditional medallions is a selection of two million duplicates of vintage gemstones from the Elegant Art gallery of Germany. this site here

    ReplyDelete
  19. Robichaud also hired Bella Energy of Louisville, Colo., to install solar panels on Precision headquarters to help recharge the batteries for eight hours at night after technicians drive them 120 miles during the day. steve holman

    ReplyDelete
  20. Energy Van for 90 periods and discover out all of the details that might come with an EV or if there are any design changes we would like. official sites

    ReplyDelete
  21. What other contractor can say they were involved in the design process of an all-electric service vehicle? Not many! Robichaud is a great example of a contractor on the cutting-edge of technology. look here

    ReplyDelete
  22. Because I'm a beginner I first thought "I'll just buy the $[...] one with 10 stitch options because I don't know what all those stitches do anyway, and then if I really get into sewing that's cheap enough that I'll upgrade in a year or two" I'm so glad I didn't do that, I'm still just beginning but I can see how valuable having more stitch options is going to be and I've already started using some of them. There wasnt a major growth but my boobs were a little more plump and full. Disappointed that it did not include a miter guide. old school new body does it work

    ReplyDelete
  23. Nixon reconciled for a crime scandal. Clinton was impeached for a scandal as well. browse around this site

    ReplyDelete
  24. Remaining beneficial does not fix the issue. It does, however, strengthen that a damaging mind-set does nothing to fix anything. Plus, acting it until I create it has proved helpful for me in previous times. Sometimes you have to think it before you stay it. Free Itunes Codes

    ReplyDelete
  25. Unchecked) exceptions make it nice because you don't have to clutter up the code with errors that will be propagated up. However, I think the gain from having the errors explicitly in your return type outweighs the inconvenience. address

    ReplyDelete
  26. By getting your material launched online, you have began out the entry for more visitors to your website, new visitors (possibly investing promoters and/or suppliers looking for excellent authors in a particular field), and name recognition across the web. important link

    ReplyDelete
  27. Boulder Electric Vehicle and a Precision customer, invented the electric service truck, and convinced Robichaud to test-drive the truck that he felt would be a perfect fit for the service industry because of the short routes service technicians drive daily. venus factor review

    ReplyDelete
  28. It is then our intention to start the transition at two per month. Logistically it takes time to swap out a technician to a new van and we do not want to overwhelm ourselves, although the sooner we switch the sooner we start saving. http://www.amazon.com/BIOGOLD-100%25-GARCINIA-CAMBOGIA-EXTRACT/dp/B00MQ00DUY

    ReplyDelete
  29. One neat thing about being the first company to purchase the trucks is that Robichaud is pretty much involved in any design changes that may be made to the vehicle. Saran Wrap Weight Loss

    ReplyDelete
  30. Search for community organizations that support FSBO or online are valuable resources for helping you succeed at your venture. Sell my house quick

    ReplyDelete
  31. Consider for representation the deficiency of an starting marketing in the distribute dealing. Agen IBCBET

    ReplyDelete
  32. One neat thing about being the first company to purchase the trucks is that Robichaud is pretty much involved in any design changes that may be made to the vehicle. Buy My Property

    ReplyDelete
  33. However, excellent quotations don't always start in a guide. Sometimes, they're provided in when by individuals who know exactly what they're referring to due a prosperity of encounter in a certain area. This is often the case with excellent company quotations. wise sayings

    ReplyDelete
  34. There are many a write-ups on such maintenance on the internet but understanding is crucial for them. As they say, a little information is a risky factor. So people with no professional information must not try it on their own and depend on the professionals only. straight from the source

    ReplyDelete
  35. To talk about particular item or set up difficulties with manufacturers’ technical employees.To talk about particular item or set up difficulties with manufacturers’ technical employees.To talk about particular item or set up difficulties with manufacturers’ technical employees. click to investigate

    ReplyDelete
  36. Keeping an organized playground is another excellent way to get rid of risks and damage. visit this page

    ReplyDelete