1

I'm writing code for a game as an exercise to learn Scala after getting acquainted with Haskell. I started with an ADT as follows:

sealed class Circle(x: Double, y: Double, r: Double)

case class PlayerCircle (x: Double, y: Double, r: Double) extends Circle(x, y, r)

case class AICircle (x: Double, y: Double, r: Double) extends Circle(x, y, r)

I'm trying to write a lazy, curried val that does as follows (in Haskell pseudocode):

addToPlayer :: PlayerCircle -> Circle -> PlayerCircle
addToPlayer (PlayerCircle px py pr) (AICircle _ _ cr) = PlayerCircle px py (pr + cr)
addToPlayer player _ = player

I have the following:

def addToPlayer (wcircle : Circle) : PlayerCircle = wcircle match {
    case AICircle (_, _, wr) => copy(this.x, this.y, this.r + wr)
    case _ => this
}

What is necessary to make this function curried and lazy?

Edit: I've googled for the answer but haven't found any article of use so please help me with this one.

4
  • why do you want to curry it and make it lazy? Also currying is when you take a function that accepts multiple arguments and translate it into a sequence of functions each of which takes on argument. Commented Jun 19, 2015 at 22:55
  • I'm used to the Haskell paradigms, kinda. It's mostly a learning exercise. Commented Jun 19, 2015 at 23:01
  • but your function only takes one argument...how do you expect to curry it? Commented Jun 19, 2015 at 23:09
  • @I.K. Look at the Haskell-like code. It shows that instead of using this we pass the object as a parameter Commented Jun 19, 2015 at 23:45

2 Answers 2

2

Here's a curried function example:

def addToPlayer(c: Circle, p: Player) = ... actual code...
def addToPlayer(c: Circle) = p: Player => addToPlayer(c, p)

Then you can do this:

val partial = addToPlayer(c)
val complete = partial(p)

This is lazy because addToPlayer(c, p) isn't run until both parameters are given.

HTH.

Sign up to request clarification or add additional context in comments.

Comments

0

maybe this way

def addToPlayer(p: PlayerCircle)(c: Circle): PlayerCircle = c match {
  case AICircle(_, _, wr) => p.copy(p.x, p.y, p.r + wr)
  case _ => p
}

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.