1

I'm trying to transform a sequence like the one bellow

val raw: Seq[String] = Seq("timmy barns", "jimmy smith", "mark middle")

into a sequence that would look like this.

val parsed: Seq[(String, String)] = Seq(("timmy", "barns"), ("jimmy", "smith"), ("mark", "middle"))

The best I can come up with is something like this.

val parsed: Seq[(String, String)] = Seq(raw.map(i => i.split(" ")))

Obviously this won't work, can anyone give me suggestions?

4 Answers 4

1

This will gracefully, but silently, drop all input that doesn't split() into 2 elements.

raw.map(_.split("\\s+")).collect{case Array(a,b) => (a,b)}
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to combine it with a more general regex matching anyway, this variant might be preferable:

val P = "(\\w+) +(\\w+)".r 
val result = for (P(x, y) <- raw) yield (x, y)

gives:

result: Seq[(String, String)] = List((timmy,barns), (jimmy,smith), (mark,middle))

2 Comments

\\w+ probably won't work for names like "tim berners-lee". Maybe \\S+ instead?
@LeoC Well, yes... but with "Saunders Mac Lane", even \\S+ will not work correctly. The advantage of the regex solution (as compared to split) is that one has a little bit more flexibility, so that one could admit more symbols in the surname, for example.
0

May be something like this:-

val raw: Seq[String] = Seq("timmy barns", "jimmy smith", "mark middle")

val splitRaw = raw.map { x =>
  (x.split(" ").head, x.split(" ").last)
}

Hope this helps!

Comments

0

My version )

raw.map(_.split(" ") match {
    case Array(a, b) => (a, b)
})

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.