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 thoughts on “Scala: when you wish that Seq was a List

  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

  2. 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)

  3. 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

  4. 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

  5. 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

  6. 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

  7. \”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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

Comments are closed.

Discover more from Jessitron

Subscribe now to keep reading and get access to the full archive.

Continue reading