Skip to content

Instantly share code, notes, and snippets.

View ngsw-taro's full-sized avatar

Taro Nagasawa ngsw-taro

View GitHub Profile
@ngsw-taro
ngsw-taro / gist:1274669
Created October 10, 2011 05:09
FizzBuzz
for(i in 1..100)println(((a=i%3)?"":"Fizz")+(i%5?(a?i:""):"Buzz"))
@ngsw-taro
ngsw-taro / gist:2606595
Created May 6, 2012 00:38
Kotlin 関数定義の方法に悩む…
// 拡張関数で定義。
// たぶん一番Kotlinらしい書き方
// kotlin friendly
fun String.isFourCharacters() = this.size == 4
// static関数として定義。
// Javaっぽい書き方?
// like java?
//fun isFourCharacters(str : String) = str.isFourCharacters()
@ngsw-taro
ngsw-taro / gist:2730038
Created May 19, 2012 08:10
Kotlin's Bug?
fun main(args : Array<String>) {
val a = arrayList(1)
val b = a + a // OK
val c = a + arrayList(1) // Compilation error
}
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
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 {
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.*
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)
}
@ngsw-taro
ngsw-taro / Prime.hs
Created August 26, 2012 01:26
はすけるで素数判定
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..])
@ngsw-taro
ngsw-taro / gist:3930225
Created October 22, 2012 08:00
ImmutableCollection in Kotlin
// 不変コレクションを作る場合は ImmutableArrayListBuilderクラス を使おう
// このテストは失敗する
package com.taroid.kt.sample
import kotlin.test.assertEquals
import org.junit.Test as test
class ImmutableCollectionTest() {
@ngsw-taro
ngsw-taro / MainActivity.java
Created December 6, 2012 11:09
AndroidAdventCalendar2012
// Android(サーバ側)
package com.taroid.advent;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity {