0

I want to subset on people's names whose surnames are in names.

The following isn't working

val names = List("Smith","Adams","Crawford")
val people =List("Billy Jean","Will Morten","Lenhardt Peterson","Lauryn Crawford","George Smith","Priscilla Adams")

people.filter(p=> names.exists(p.name.contains))
1

2 Answers 2

1

Surnames come in all sorts of confusing configurations.

val names = List("Smith","von Braun","Jones","de Kooning")
val people =List("Billy Jean","Will Morten","Lenhardt Peterson",
  "Lauryn Crawford","George Smith","Priscilla Adams",
  "Wernher von Braun", "Diederik Arnoldus De Beer", "Willem de Kooning")

names.flatMap(n => people.find(_.matches(s".* $n")))
// res0: List[String] = List(George Smith, Wernher von Braun, Willem de Kooning)
Sign up to request clarification or add additional context in comments.

Comments

1

The naive and not most effecient solution:

val names = Set("Smith","Adams","Crawford")
val people = List("Billy Jean","Will Morten",
                  "Lenhardt Peterson","Lauryn Crawford", 
                  "George Smith","Priscilla Adams")

people.filter(x => names.contains(x.split(" ").lastOption.getOrElse("")))
      .foreach(println)

Results in:

Lauryn Crawford
George Smith
Priscilla Adams

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.