1

I have following list of splitters:

    val splitd = list(" or ", " and ", " up to ")

and the following string:

    val st = "You should eat 2 kg apples a week or 2 bananas everyday; up to a month you should eat 5g of ginger everyday"

I want following output:

    val entry = List("You should eat 2 kg apples a week", "2 bananas everyday;", "a month you should eat 5g of ginger everyday")

If there is no entry in "splitd" matching the content in "st" then full string "st" should be returned. Thanks in advance for your help.

Dear @Shadowlands and @marstran, need your help again.

1
  • @jwvh No, leaving "or" was a typo. Any help now.... Commented Oct 24, 2015 at 7:38

1 Answer 1

1

Check this out:

splitd.foldLeft(List(st)) {
  case (acc, spl) => acc.flatMap(item => item.split(spl).toList)
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, it worked perfectly fine. Can you please suggest any online reference where I can get a deeper understanding of Map, folding etc. etc. SCALA cookbook, I am referring to, has very simple examples.
I would recommend 'Functional Programming in Scala' by Paul Chiusano and Rúnar Bjarnason.

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.