Skip to content

Instantly share code, notes, and snippets.

@SatoTakeshiX SatoTakeshiX/function.kt
Last active Jul 19, 2017

Embed
What would you like to do?
Kotlinスタートブック第五章 関数で話されていたコード
/**
* Created by satoutakeshi on 2017/07/17.
*/
fun main(args : Array<String>) {
val result = succ(31)
println(result)
println(square(5))
println(hello("sato"))
println(max(2, 4))
println(sum(arrayOf(1,2,3)))
//引数名 = 値
//定義順を逆にしても大丈夫
val subValue = sub(subtrahend = 8, minuend = 50)
println(subValue)
println(sub(minuend = 10, subtrahend = 9))
println(helloWithDefault())
println(sum2(1,2,3,4,5))
//*は配列が展開される。
println(sum2(ints = *intArrayOf(1,2,3)))
println(sum42222(*listOf<Int>(1,2,3,4,5,6).toIntArray()))
//println(*intArrayOf(1,2,3).toString())
//--再帰の関数。スタック量が多いのでStackOverflowErrorを起こす
//sumRecursion((1L..123456).toList())
//tailrecをつけて、StackOverflowErrorを防ぐ
//sumRecursion2((1L..123456).toList(),accumulator = 100)
println(sum4(listOf(1,2,3)))
var c = Counter()
c.countUp()
println(c.getCount())
c.countUp()
println(c.getCount())
println("------------")
println(listOf(1,2,3).fold(0, {a, b -> a + b}))
//swift
//[1, 2, 3].reduce(0, {$0 + $1})
}
/**
* fun <関数名>(<引数名>:<引数の型>): <返り値の型> = 処理(式)
*/
fun succ(i:Int): Int = i + 1
//引数の二乗を返す関数
fun square(i: Int): Int = i * i
//
fun hello(name:String): String {
val ss = "Hello, $name!"
return ss
}
//引数を2つ渡して、大きい方を返す関数
fun max(a: Int, b: Int): Int = if(b<=a) a else b
//文 2行以上の関数は{}をつける
//return が必要
fun sum(ints: Array<Int>): Int {
var sum = 0
for (i in ints) {
sum += i
}
return sum
}
//名前つき引数の関数
//第一引数から第二引数を引いた数を返す
fun sub(minuend: Int, subtrahend: Int): Int = minuend - subtrahend
//デフォルト値
// = 値をしている。
fun helloWithDefault(name: String = "World"): String = "Hello!, $name!"
//可変長引数。引数の個数を可変にできる
fun sum2(vararg ints: Int): Int {
var sum = 0
for (i in ints){
sum += i
}
return sum
}
fun sum42222(vararg ints: Int): Int {
//コンパイル時に引数を全部配列にして
var sum = 0
for (i in ints){
sum += i
}
return sum
}
fun sum3(numbers: List<Long>): Long {
var sum = 0L
for (n in numbers) {
sum += n
}
return sum
}
//再帰
fun sumRecursion(numbers: List<Long>): Long {
if (numbers.isEmpty()) {
return 0
}
else {
//numbersの1件目をスタック.
//二軒目を
return numbers.first() + sumRecursion(numbers.drop(1))
}
}
//sumRecursionを呼ぶ時にsumRecursionを呼ぶときにsumRecursionを呼ぶ時にsumRecursionを呼ぶ。
tailrec fun sumRecursion2(numbers: List<Long>, accumulator: Long = 0): Long {
if (numbers.isEmpty()){
return accumulator
}
else{
return sumRecursion2(numbers.drop(1), accumulator + numbers.first())
}
}
//ローカル関数
fun sum4(numbers: List<Long>): Long {
tailrec fun go(numbers: List<Long>, accumulator: Long): Long = if (numbers.isEmpty()) accumulator else sumRecursion2(numbers.drop(1), accumulator + numbers.first())
return go(numbers, 0)
}
//結果を返さない関数
class Counter{
private var cnt = 0
fun countUp(): Unit{
cnt++
}
fun void(): Void? {
return null
}
fun getCount(): Int {
val countup = countUp()
return cnt
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.