Skip to content

Instantly share code, notes, and snippets.

@f81
Created May 31, 2013 12:37
Show Gist options
  • Save f81/5684714 to your computer and use it in GitHub Desktop.
Save f81/5684714 to your computer and use it in GitHub Desktop.
What is 関数型言語? ref: http://qiita.com/items/9f0b9cc5c0f28deb48e2
$ scala NoneSideEffect.scala
add=6
add=6
add=6
$ scala SideEffect.scala
add=6
add=7
add=6
def add(x:Int, y: Int) = x + y
var add = (x:Int, y:Int) => x + y
$ scala FunctionExample.scala
y=6
y=7
$ scala Mutable.scala
Mutable
Change mutable
$ scala Immutable.scala
Immutable.scala:6: error: reassignment to val
immutable = "Change immutable"
^
one error found
$ scala SideEffect.scala
total=1
total=2
total=3
object FirstClassFunction{
def main(args: Array[String]){
val add = (x: Int) => {
val _add = (y: Int) => y + 5
_add(x)
}
printf("add=%s \n", add(1))
printf("add=%s \n", add(2))
printf("add=%s \n", add(1))
}
}
object FunctionExample{
def main(args: Array[String]){
var calculate = (x: Int) => x + 5 : Int
printf("y=%s \n", calculate(1))
printf("y=%s \n", calculate(2))
}
}
object Immutable{
def main(args: Array[String]){
val immutable = "Immutable"
printf("%s ", immutable)
immutable = "Change immutable"
printf("%s ", immutable)
}
}
object Mutable{
def main(args: Array[String]){
var mutable = "Mutable"
printf("%s ", mutable)
mutable = "Change mutable"
printf("%s ", mutable)
}
}
object NoneSideEffect{
def main(args: Array[String]){
var add = (x: Int) => x + 5
printf("add=%s \n", add(1))
printf("add=%s \n", add(1))
printf("add=%s \n", add(1))
}
}
object SideEffect{
var total = 0
def main(args: Array[String]){
var add = (x:Int) => {
total+=x
total
}
printf("total=%s \n", add(1))
printf("total=%s \n", add(1))
printf("total=%s \n", add(1))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment