spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Scala List match last element


You need to switch the order. The xs symbol will eagerly match anything in that position. Trying to match Nil first will make that statement no longer unreachable. Also, you'll still need to match Nil by itself to account for empty lists.

def stringify(list: List[String]):
String = list match {
   case x :: Nil => x
   case x :: xs => x + ":" + stringify(xs)  
   case Nil => ""
}

Though mkString already does what you're looking to make.


Categories : Scala

Related to : Scala List match last element
Can I tell scala.xml to match any of two tags?
If you take a look at the source for \ in NodeSeq.scala you can see that it's really just a bit of sugar for a filter operation over descendant_or_self, which is a List[Node], using the node's label. So you could do the same thing yourself, matching against a set of labels, like this: val searchedLabels = Set("p", "div") val results = body.descendant_or_self.filter(node => searchedLabels.con

Categories : Scala
Pattern Match on Scala `class`
The argument of unapply should be an instance of the type you want to match: class Foo(val x: Int) object { def unapply(f: Foo): Option[Int] = Some(f.x) }

Categories : Scala
How to match all words in a sentence with scala combinators?
You should use something like that to match sequence of tokens instead of one token: trait TagParser extends RegexParsers { val tags: Parser[List[String]] = rep("""[a-zA-Z]+""".r) } rep is: A parser generator for repetitions. rep(p) repeatedly uses p to parse the input until p fails (the result is a List of the consecutive results of p). http://www.scala-lang.org/files/archive/ni

Categories : Scala
Scala - Add element:MyType to Array = option[MyType] expected
As an alternative to prevent you from going down the Option[Array[A]], Array[Option[A]] path, you can invert your initial put into like this: var allMyType = new HashMap[String, MyType]() var secondHashMap = new HashMap[Any, Array[MyType]]() allMyType.get(key).fold(MISSING KEY CASE)(x => secondHashMap.put(i, x)) Fold in this case looks at the identity case (NONE) where the requested key is

Categories : Scala
Remove prefixed attribute in scala (scala-xml)
You can't remove a prefixed attribute with remove(String) because of its implementation: From Attribute.scala: def remove(key: String) = if (!isPrefixed && this.key == key) next else copy(next remove key) As you can see, if the attribute is prefixed, the first branch condition is false. However, there is a different function in the same class: def remove(namespace: String, scop

Categories : Xml
Recently Add
head :: tail pattern matching for strings
What is the easiest and most efficient way to make a min heap in Scala?
Removing mutable collections and making it look more scala style
Reverse list of n elements
scalac: Error: object CharRef in intelliJ 14
Scala - trouble with type inference in lambda expression
Exception on spark test
How can I emulate Haskell's typeclasses in Scala?
Slick: CRUD extension: How to encapsulate implicit mapping:BaseColumnType[T]
Can't find Traverse for sequencing Seq[ValidationNel[String, MyCaseClass]] => ValidationNel[String, Seq[MyCaseClass]]
Tail recursion: internal "loop" function or default values for accumulators
Scala - Add element:MyType to Array = option[MyType] expected
Scala, Composing Function with two values
Using dependent type to generate compile error
How to match all words in a sentence with scala combinators?
Parser Alternative Operator | Fails
ScalaTest assertion mismatch due to Physical Address
Scala implicit parameter and japanese smiley 'foldLeft'
Is it possible to user reduceByKey((x, y, z) => ...)?
How to implement security Authorization using scala and play?
SSO login using scala script
Sum elements based on a predicate
Keep track of completed Futures
API Observable with dynamic caching
java.io.IOException: Remotely closed in gatling
Scala permutations using two lists
Is Scala Either really a Monad
Spark: Use of distinct
Identifying two type wildcards as identical
how to package spark scala application
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.