Skip to content

Instantly share code, notes, and snippets.

@ShahOdin
Last active October 1, 2018 13:37
Show Gist options
  • Save ShahOdin/8930081e68aa0fb254912529a9d931d9 to your computer and use it in GitHub Desktop.
Save ShahOdin/8930081e68aa0fb254912529a9d931d9 to your computer and use it in GitHub Desktop.
currying is not just for partial application
object Model {
//the types don't mean anything, just faking some models.
type Config = Float
type ID = Int
type Age = Int
type DateInString = String
type DateSinceEpoch = Float
}
//example of api changing where the first parameter is changed.
import Model._
case class AgeServiceClient(config: Config) {
val getAgeOnDate_OldAPI: DateInString => ID => Age = ???
val getAgeOnDate_NewAPI: DateSinceEpoch => ID => Age = ???
}
def modelMapping: DateInString => DateSinceEpoch = ???
val getAgeOnDate: Config => DateInString => ID => Age
= AgeServiceClient(_).getAgeOldAPI
val getAgeOnDateRefactored: Config => DateInString => ID => Age
= AgeServiceClient(_).getAgeNewAPI compose modelMapping
}
//example of api changing where the non-first parameter is changed.
object Demo2 {
import Model._
case class AgeServiceClient(config: Config) {
val getAgeOldAPI: ID => DateInString => Age = ???
val getAgeNewAPI: ID => DateSinceEpoch => Age = ???
}
def modelMapping: DateInString => DateSinceEpoch = ???
val getAgeOnDateOld: Config => ID => DateInString => Age
= AgeServiceClient(_).getAgeOldAPI
val getAgeOnDateRefactored: Config => ID => DateInString => Age
= {
config => // in general, all the unchanged parameters up to changed parameter have to be explicitly passed in.
AgeServiceClient(config).getAgeNewAPI(_) compose modelMapping
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment