0

With reference to the link below Scala: curried constructors

 class Person(var name : String, var age : Int, var email : String)
  def mkPerson = (n : String) => (a : Int) => (e : String) => new Person(n,a,e)
  def makeName(s:String):String="Hello"+s
  def makeAge(age:Int):Int=age
  def makeEmail(email:String):String=email

  val s=mkPerson(makeName("abcd"))
  val t1=s(makeAge(2))
  val t2=t1(makeEmail("abc@gmail.com"))

I have created methods makeName,makeAge,makeEmail to enrich the values. The actual use case is different

Question

  • Is there any possible ways to achieve the above through case classes
  • I don't want to use variables s,t1,t2
  • Can we achieve the above by Partially applied functions
coder25
  • 2,363
  • 12
  • 57
  • 104
  • So, once a `val p: Person` is created, do you want `p.name` to include the `Hello` prefix (i.e. do you want to "forget" the original values and use the "enriched" values only)? – Tzach Zohar Aug 31 '17 at 14:48
  • yes i want the enrichments and I want a solution for case classes .Actually i have enrichers which will return values i want to construct the way I have shown in code.Any other possible ways using partially applied functions – coder25 Aug 31 '17 at 14:52
  • what you want to achieve here is still unclear to me: Do you want calls to `Person("a", 1, "b")` to be "automatically" enriched (i.e. necessarily resulting with `Person(Helloa, 1, b)`? If not, do you want a separate `mkPerson(String, Int, String): Person` that creates "enriched" persons? How would the optimal usage look like? – Tzach Zohar Aug 31 '17 at 15:04
  • mkPerson(String, Int, String): Person approach – coder25 Aug 31 '17 at 15:06

1 Answers1

0

Not sure curried functions are necessary / helpful in this case, if you're OK with a mkPerson that takes 3 arguments:

case class Person private(name: String, age: Int, email: String)

def makeName(s: String): String = "Hello" + s
def makeAge(age: Int): Int = age
def makeEmail(email: String): String = email

def mkPerson(name: String, age: Int, email: String): Person =
  Person(makeName(name), makeAge(age), makeEmail(email))

println(Person("Jane", 29, "jane@mail.com"))   // no enrichment: Person(Jane,29,jane@mail.com)
println(mkPerson("Jane", 29, "jane@mail.com")) // enriched: Person(HelloJane,29,jane@mail.com)

Then, if you really need it, you can curry mkPerson:

val curried = (mkPerson _).curried
curried("Jane")(29)("jane@mail.com")  // same enriched result
Tzach Zohar
  • 37,442
  • 3
  • 79
  • 85
  • thks makePerson can have many arguments any way to we use partially applied functions here – coder25 Aug 31 '17 at 15:13
  • First - yes, that's shown at the end. More importantly - _why_? Partially applied functions are useful, for example, when you want to pass some arguments in one call, and some in a separate call; or, when you want to re-use the result of applying some of the arguments, multiple times while supplying different values for the rest of the arguments; If that is your usecase, show it in the question. – Tzach Zohar Aug 31 '17 at 15:19
  • ok .not sure but i want to map values from one case class to another .ie. field x in model A maps to field y in model B.So i am writing a transformers for each mapping and want to construct the object of model B gradually as fields get mapped – coder25 Aug 31 '17 at 15:24
  • I see. Consider posting a new question that shows all of this, you might get better answers that way... – Tzach Zohar Aug 31 '17 at 15:28
  • posted a new question https://stackoverflow.com/questions/45986193/possible-way-to-use-partially-applied-function-for-case-class – coder25 Aug 31 '17 at 16:29