/1.kt Secret
Last active
December 20, 2015 01:19
blog memo
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
fun sayHello(target : String) { | |
println("Hello, $target!") | |
} | |
val target : Any = "world" | |
if(target is String) { | |
sayHello(target) // Hello, world! | |
} |
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 sayHello(target : String) = println(s"Hello, $target!") | |
val target : Any = "world" | |
target match { | |
case x:String => sayHello(x) | |
case _ => | |
} |
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
fun isUpperCase(str : String) : Boolean { | |
fun String.all(f : (Char) -> Boolean) : Boolean { | |
for(val c in this) { | |
if(!f(c)) { | |
return false | |
} | |
} | |
return true | |
} | |
return str.all { | |
Character.isUpperCase(it) | |
} | |
} |
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 isUpperCase(str : String) : Boolean = { | |
implicit class MyRichString(val self : String) { | |
def all(f : Char => Boolean) : Boolean = self.forall(f) | |
} | |
str.all(Character.isUpperCase) | |
} |
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
fun run(f : () -> Unit) = f() | |
// リテラルじゃないから引数として渡せないらしい | |
//fun sayHello() { | |
// println("Hello") | |
//} | |
val sayHello = { | |
println("Hello") | |
} | |
run(sayHello) |
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 run(f: () => Unit ) = f() | |
val runLiteral = (f:() => Unit) => f() | |
def sayHelloFunc() = println("Func!!") | |
val sayHelloLiteral = () => println("Literal!!") | |
run( sayHelloFunc ) | |
run( sayHelloLiteral ) | |
runLiteral( sayHelloFunc ) | |
runLiteral( sayHelloLiteral ) |
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
when(pair) { | |
is #(0, 0) -> "(0, 0)" | |
is #(0, Int) -> "(0, Int)" | |
is #(Int, 0) -> "(Int, 0)" | |
is #(Int, Int) -> "(Int, Int)" | |
is #(String, String) -> "(String, String)" | |
else -> "unknown" | |
} |
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
val pair:(Any, Any) = ... | |
pair match { | |
case (0, 0) => "(0, 0)" | |
case (0, _:Int) => "(0, Int)" | |
case (_:Int, 0) => "(Int, 0)" | |
case (_:Int, _:Int) => "(Int, Int)" | |
case (_:String, _:String) => "(String, String)" | |
case _ => "unknown" | |
} |
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
val pair : #(String, Int) = #("taro", 23) // ← 先日25歳になっていたような…w | |
if(pair is #(val a, val b)) { | |
println("$a ($b)") // taro (23) | |
} |
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
val pair = ("taro", 23) | |
// スマートキャストとおんなじような事やるならパターンマッチも使えばいいかな | |
val (a, b) = pair |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment