0

I have a list of strings as shown below, which lists fruits and the cost associated with each. In case of no value, it is assumed to be 5:

val stringList: List[String] = List("apples 20", "oranges", "pears 10")

Now I want to split the string to get tuples of the fruit and the cost. What is the scala way of doing this?

stringList.map(query => query.split(" ")) 

is not what I want.

I found this which is similar. What is the correct Scala way of doing this?

1
  • Map a custom function that splits and then adds an element if the second is missing. Commented Apr 25, 2016 at 19:57

2 Answers 2

4

You could use a regular expression and pattern matching:

val Pat = """(.+)\s(\d+)""".r  // word followed by whitespace followed by number

def extract(in: String): (String, Int) = in match {
  case Pat(name, price) => (name, price.toInt)
  case _                => (in, 5)
}

val stringList: List[String] = List("apples 20", "oranges", "pears 10")

stringList.map(extract) // List((apples,20), (oranges,5), (pears,10))

You have two capturing groups in the pattern. These will be extracted as strings, so you have to convert explicitly using .toInt.

Sign up to request clarification or add additional context in comments.

3 Comments

Here is a bit more on pattern matching: stackoverflow.com/questions/4636610/…
val r = """(\S+)\s*(\d+)?""".r ; def f(s: String) = s match { case r(n, null) => (n, 42) case r(n, i) => (n, i.toInt) case _ => (0,0) } to show handling empty second group. Regex helps so much with verifying.
@som-snytt Thanks, my regex foo is rather limited
3

You almost have it:

stringList.map(query => query.split(" ")) 

is what you want, just add another map to it to change lists to tuples:

.map { list => list.head -> list.lift(1).getOrElse("5").toInt }

or this instead, if you prefer:

.collect { 
    case Seq(a, b) => a -> b.toInt
    case Seq(a) => a -> 5
 }

(.collect will silently ignore the occurrences, where there are less than one or more than two elements in the list. You can replace it with .map if you would prefer it to through an error in such cases).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.