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
for(i in 1..100)println(((a=i%3)?"":"Fizz")+(i%5?(a?i:""):"Buzz")) |
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
// 拡張関数で定義。 | |
// たぶん一番Kotlinらしい書き方 | |
// kotlin friendly | |
fun String.isFourCharacters() = this.size == 4 | |
// static関数として定義。 | |
// Javaっぽい書き方? | |
// like java? | |
//fun isFourCharacters(str : String) = str.isFourCharacters() |
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 main(args : Array<String>) { | |
val a = arrayList(1) | |
val b = a + a // OK | |
val c = a + arrayList(1) // Compilation error | |
} |
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
class Foo<out T>() { | |
var value : T? = null | |
} | |
fun main(args : Array<String>) { | |
val a : Foo<Int> = Foo<Int>() | |
a.value = 5 | |
val b : Foo<Number> = a | |
b.value = 2.0 |
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
import java.util.LinkedList | |
import java.util.List | |
fun rpn(expression : String) : Double { | |
val expressionElements = expression.split(' ').toLinkedList() | |
val result = foldl(rpn, Stack<Double>(), expressionElements) | |
return result.pop() | |
} | |
private fun foldl<T, U>(f : (T, U) -> T, a : T, l : List<U>) : T { |
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
import javafx.application.Application | |
import javafx.event.ActionEvent | |
import javafx.geometry.Pos | |
import javafx.scene.Scene | |
import javafx.scene.control.Button | |
import javafx.scene.control.Label | |
import javafx.scene.layout.VBox | |
import javafx.stage.Stage | |
import kotlinfx.* |
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 main(args : Array<String>) { | |
for(i in 1..100) { | |
i.jojo() | |
} | |
} | |
private fun Int.jojo() { | |
val s = if(this.isPrime()) "JOJO!" else "$this" | |
println(s) | |
} |
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
module Prime( | |
prime | |
) where | |
prime :: Int -> Bool | |
prime x | x < 0 = error "the argument should be larger than zero." | |
| x == 1 = False | |
| x == 2 = True | |
| mod x 2 == 0 = False | |
| otherwise = all (\y -> mod x y /= 0 || x == y) (take (x - 3) [3, 5..]) |
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
// 不変コレクションを作る場合は ImmutableArrayListBuilderクラス を使おう | |
// このテストは失敗する | |
package com.taroid.kt.sample | |
import kotlin.test.assertEquals | |
import org.junit.Test as test | |
class ImmutableCollectionTest() { |
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
// Android(サーバ側) | |
package com.taroid.advent; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.widget.Toast; | |
public class MainActivity extends Activity { |
OlderNewer