Last active
September 27, 2018 13:52
-
-
Save ShahOdin/18b31121c6e8bd4b72de7e78f0be1fcf to your computer and use it in GitHub Desktop.
dependency injection by passing parameterised functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Pen | |
type Drawing = Unit | |
trait Side | |
case object Right extends Side | |
case object Left extends Side | |
object Drawing { | |
//pens everywhere, distracting us from the main drawing logic | |
def drawHead(pen: Pen): Drawing = ??? | |
def drawFoot(pen: Pen, side: Side): Drawing = ??? | |
def drawHand(pen: Pen, side: Side): Drawing = ??? | |
def drawHuman(pen: Pen): List[Drawing] = { | |
val head: Drawing = drawHead(pen) | |
val rightHand: Drawing = drawHand(pen, Right) | |
val leftHand: Drawing = drawHand(pen, Left) | |
val rightFoot: Drawing = drawFoot(pen, Right) | |
val leftFoot: Drawing = drawFoot(pen, Left) | |
List(head, rightHand, leftHand, rightFoot, leftFoot) | |
} | |
} | |
object DrawingSemiFP { | |
val drawHeadInstructions: Pen => Unit = ??? | |
def drawFootInstructions(side: Side): Pen => Drawing = ??? | |
def drawHandInstructions(side: Side): Pen => Drawing = ??? | |
val drawHumanInstructionsTheOldWay: Pen => List[Drawing] = { | |
pen => | |
val head: Drawing = drawHeadInstructions(pen) | |
val rightHand: Drawing = drawHandInstructions(Right)(pen) | |
val leftHand: Drawing = drawHandInstructions(Left)(pen) | |
val rightFoot: Drawing = drawFootInstructions(Right)(pen) | |
val leftFoot: Drawing = drawFootInstructions(Left)(pen) | |
List(head, rightHand, leftHand, rightFoot, leftFoot) | |
} | |
val drawHumanInstructions: Pen => List[Drawing] = { | |
pen => | |
val head: Pen => Drawing = drawHeadInstructions | |
val rightHand: Pen => Drawing = drawHandInstructions(Right) | |
val leftHand: Pen => Drawing = drawHandInstructions(Left) | |
val rightFoot: Pen => Drawing = drawFootInstructions(Right) | |
val leftFoot: Pen => Drawing = drawFootInstructions(Left) | |
List(head, rightHand, leftHand, rightFoot, leftFoot).map(_(pen)) | |
} | |
} | |
object DrawingFP { | |
val drawHeadInstructions: Pen => Drawing = ??? | |
val drawFootInstructions: Side => Pen => Drawing = ??? | |
val drawHandInstructions: Side => Pen => Drawing = ??? | |
val drawHumanInstructions: Pen => List[Drawing] = { | |
pen => | |
val head: Pen => Drawing = drawHeadInstructions | |
val rightHand: Pen => Drawing = drawHandInstructions(Right) | |
val leftHand: Pen => Drawing = drawHandInstructions(Left) | |
val rightFoot: Pen => Drawing = drawFootInstructions(Right) | |
val leftFoot: Pen => Drawing = drawFootInstructions(Left) | |
List(head, rightHand, leftHand, rightFoot, leftFoot).map(_(pen)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment