Created
May 31, 2013 12:37
-
-
Save f81/5684714 to your computer and use it in GitHub Desktop.
What is 関数型言語? ref: http://qiita.com/items/9f0b9cc5c0f28deb48e2
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
y = sin(x) |
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
$ scala NoneSideEffect.scala | |
add=6 | |
add=6 | |
add=6 |
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
$ scala SideEffect.scala | |
add=6 | |
add=7 | |
add=6 |
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
def add(x:Int, y: Int) = x + y |
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
var add = (x:Int, y:Int) => x + y |
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
$ scala FunctionExample.scala | |
y=6 | |
y=7 |
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
$ scala Mutable.scala | |
Mutable | |
Change mutable |
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
$ scala Immutable.scala | |
Immutable.scala:6: error: reassignment to val | |
immutable = "Change immutable" | |
^ | |
one error found |
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
$ scala SideEffect.scala | |
total=1 | |
total=2 | |
total=3 |
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
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)) | |
} | |
} |
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
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)) | |
} | |
} |
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
object Immutable{ | |
def main(args: Array[String]){ | |
val immutable = "Immutable" | |
printf("%s ", immutable) | |
immutable = "Change immutable" | |
printf("%s ", immutable) | |
} | |
} |
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
object Mutable{ | |
def main(args: Array[String]){ | |
var mutable = "Mutable" | |
printf("%s ", mutable) | |
mutable = "Change mutable" | |
printf("%s ", mutable) | |
} | |
} |
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
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)) | |
} | |
} |
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
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