0

I want to get Student and employee who works in country India and china

object Countries {
  sealed trait Country {def name: String}
  case object FRANCE extends Country {val name = "FRANCE"}
  case object INDIA extends Country {val name = "INDIA"}
  case object CHINA extends Country {val name = "CHINA"}
  val list = Seq(FRANCE, INDIA, CHINA)
}

def getCountry: Option[Countries.Country] ={
  //some DB call to return country based on some parameters .I assume INDIA here
  Option(Countries.INDIA)
}

case class Student(name: String, id: Symbol = Symbol("Id"))

def getStudentName(name: Option[String]): Option[Student]={
  val std = name
    .filterNot(_.isEmpty)
    .map(Student(_))

  getCountry.collect {
    case Countries.INDIA => std
    case Countries.CHINA => std
  }.flatten              
}

case class Emp(id: Int)

def getEmp(id: Option[String]): Option[Emp] = {
  val emp = id.flatMap(_ => Option(Emp(id.get.toInt)))
  getCountry.collect {
    case Countries.INDIA => emp
    case Countries.CHINA => emp
  }.flatten
}

Is there any efficient way to avoid the repeated code using collect and case matching I have done .

6
  • I downvoted because the code is nowhere near compiling, making the question not understandable for me. Commented Sep 18, 2017 at 15:15
  • what do you mean by "efficient"? Do you mean readability (e.g. not repeating itself) or anything else (e.f. better performing)? Commented Sep 18, 2017 at 15:16
  • @C4stor please check Commented Sep 18, 2017 at 15:23
  • @TzachZohar It means readability Commented Sep 18, 2017 at 15:24
  • @C4stor "nowhere near compiling" is an exaggeration, had one mistake (Country instead of Countries.Country) ... Commented Sep 18, 2017 at 15:26

1 Answer 1

2
def ifCountry[T](result: => Option[T], countries: Country*) = getCountry
  .filter(countries.toSet)
  .flatMap(_ => result)


def getStudentName(name: Option[String]) = ifCountry(
   name.filterNot(_.isEmpty).map(Student(_)),
   INDIA, CHINA
)

def getEmp(id: Option[String]) = ifCountry(
    id.map(Emp(_)), 
    INDIA, CHINA
}
Sign up to request clarification or add additional context in comments.

5 Comments

can anyone explain result: => Option[T] and :=> symbol in scala
it is the syntax for by name parameters. That means that the parameter will be evaluated only when needed, not before.
An argument whose type is => A is an argument whose type is A but which will be passed by name rather than by value. That is, it will be evaluated every time that it occurs in the code, rather than exactly once when the function is called.
Another way to see it is you're actually passing a function give an A instead of an instance of A, and this function will be called every time you need it.
Yes, it is actually just a syntactic sugar for Function0

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.