Created
September 11, 2025 15:12
-
-
Save hikaMaeng/88fa7e1b926d6569b5869743662aae4a to your computer and use it in GitHub Desktop.
This file contains hidden or 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() { | |
// val a = "url" | |
// val b = optional({ a.length > 2 } ){ | |
// println("a > 2") | |
// | |
// } | |
// b() | |
// val iter = iterator(0, { | |
// if(it < 10) it + 2 else null | |
// }){ | |
// println(it) | |
// } | |
// iter() | |
// | |
// val member = Member({"aa"} , 11) | |
val list = listOf('a', 'b', 'c', 'X', 'Z') | |
println(list.takeLast(3) eq "[c, X, Z]") | |
println(list.takeLastWhile { it.isUpperCase() } eq "[X, Z]") | |
println(list.drop(1) eq "[b, c, X, Z]") | |
println(list.dropWhile { it.isLowerCase() } eq "[X, Z]") | |
val member = "hika" age 10 grade 10 | |
val a1 = listOf(1,2,3,0).any{it > 0} | |
val a11 = listOf(1,2,3,0).fold(AnyFold(false){it > 0}){acc, it-> | |
if(acc.result) acc else AnyFold(acc.lambda(it), acc.lambda) | |
}.result | |
val a2 = listOf(1,2,3,0).all{it > 0} | |
val a3 = listOf(1,2,3,0).map{it * 2} | |
val a33 = listOf(1,2,3,0).fold(listOf<Int>()){acc, it-> | |
acc + it * 2 | |
} | |
val sum = listOf(1,2,3,0).fold(0){acc, i -> | |
acc + i | |
} | |
listOf(1,2,3,0).forEach{println(it)} | |
listOf(1,2,3,0).fold({it:Int->println(it)}){acc, it-> | |
acc(it) | |
acc | |
} | |
} | |
data class AnyFold(val result: Boolean, val lambda:(Int)-> Boolean) | |
data class Member(val name:String, val age:Int, val grade:Int = -1) | |
infix fun Member.grade(grade:Int):Member = Member(this.name, this.age, grade) | |
infix fun String.age(age:Int):Member = Member(this, age) | |
infix fun Any.eq(b:String): Boolean { | |
return this.toString() == b | |
} | |
infix fun Boolean.eq(b:String): Boolean { | |
return this.toString() == b | |
} | |
fun optional(condition:()->Boolean, lambda:()->Unit):()->Boolean | |
= { | |
if(condition()) lambda() | |
condition() | |
} | |
fun iterator(init:Int, condition:(Int)->Int?, lambda:(Int)->Unit):()->Unit | |
= { | |
var accumulator = init | |
do{ | |
accumulator = condition(accumulator) ?: break | |
lambda(accumulator) | |
}while (true) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment