" Curring transforms a function that takes multiple parameters into a chain of functions, each taking a single parameter."
(source: Dean Wampler " Programing in Scala "(O’Reilly Media ' 2008) ).It sounds awesome and quite simple:
object Curring {
def main(args: Array[String]): Unit = {
println(curryExtreme("cheese")("cake")
}
def curryNormal(s1: String)(s2: String) = s1 + s2
}
You will be see :
cheesecake
in output.It sounds simple and useful.
However ,then i wrote this code to glue all values together...
package lab.blog
object CurryCase {
def main(args: Array[String]): Unit = {
println(curryExtreme("ufo")("alien")("0000")(4))
}
def curryExtreme(s1: String)(s2: String)(s3: String)(i: Int) = i + i + s1 + i + i + s2 + i + i + i + i +s3
}
and guess output ?
formula: i + i + s1 + i + i + s2 + i + i + i + i +s3
data: strings s1 =
"ufo", s2 = "alien" s3= "0000" and integer i = 4
Iexpected...
44ufo 44alien44440000
BUT, i get ...
8ufo44alien44440000
Why? Why is 8 not 44?
This is due way how + works.
.First + works as add for 2 integers
Second + works as gluing because when you ise + with string ithen it starts gluing integer with strings so rest is a gluing 8! which seems logical as 4+4 ,but
conclusion:
Remember about basics and order ,when you programming to avoid silly suprises :)
No comments:
Post a Comment