Scala – Pattern Matching & Collections
Pattern matching is not just limited to simple types it can also be applied to collections as well. Let’s see how it can be applied to Lists. See Below In the above example, we want to check if the list of 3 elements starts with 10 or not. This logic works fine when the list has just 3 elements. But fails if the list has more than 3 elements. The last print shows the failure condition. Extending the logic so that lists of any size can be checked. See Below. In the above code, the only change we made was in the second line of the function and introduced _* – is a special instance of type ascription which tells the compiler to treat a single argument of a sequence type as a variable argument sequence. It is not as complicated as it sounds essentially it means you can pass multiple … Read more