Created
June 8, 2017 16:51
-
-
Save kurogelee/e5e14bfe3e860d97a88ebbf1eb9a01c3 to your computer and use it in GitHub Desktop.
KotlinでもClojureの便利な関数を使いたい ref: http://qiita.com/kurogelee/items/13e018d97cde98b211dc
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
(conj list2 10) | |
(concat list1 list2) |
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
list2 + 10 | |
list1 + list2 |
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
(distinct list2) | |
(dedupe list2) |
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 <T> Iterable<T>.dedupe(): List<T> { | |
if (count() == 0) return toList() | |
return fold(listOf(first())) { acc, v -> if (v != acc.last()) acc + v else acc } | |
} | |
list2.distinct() | |
list2.dedupe() |
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
(filter even? list1) | |
(remove even? list1) |
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
list1.filter { it % 2 == 0 } | |
list1.filterNot { it % 2 == 0 } |
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
(group-by #(mod % 2) list2) | |
(frequencies list2) |
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 <T> Iterable<T>.frequencies(): Map<T, Int> = groupingBy { it }.eachCount() | |
list2.groupBy { it % 2 } | |
list2.frequencies() |
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
(interleave list1 list2) | |
(interpose 999 list1) |
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 <T> Iterable<T>.interleave(list: Iterable<T>): List<T> = zip(list).flatMap { it.toList() } | |
fun <T> Iterable<T>.interpose(sep: T): List<T> = | |
zip(Collections.nCopies(count(), sep)).flatMap { it.toList() }.dropLast(1) | |
list1.interleave(list2) | |
list1.interpose(999) |
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
(map #(* % 2) list1) | |
(keep #(when (> % 3) (* % 2)) list1) | |
(map-indexed #(* %1 %2) list1) | |
(keep-indexed #(when (> % 3) (* % %2)) list1) | |
(mapcat #(list % (* % %)) list1) |
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
list1.map { it * 2 } | |
list1.mapNotNull { if (it > 3) it * 2 else null } | |
list1.mapIndexed { i, v -> i * v } | |
list1.mapIndexedNotNull { i, v -> if (i > 3) i * v else null } | |
list1.flatMap { listOf(it, it * it) } |
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
(partition 3 list1) | |
(partition-all 3 list1) | |
(partition 4 2 list1) | |
(partition-all 4 2 list1) | |
(partition 4 2 [:a :b] list1) |
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 <T> Iterable<T>.partition(n: Int, step: Int = n, pad: List<T> = listOf<T>()): List<List<T>> { | |
var tmp = toList() | |
return mutableListOf<List<T>>().apply { | |
while (tmp.count() >= n) { | |
add(tmp.take(n)) | |
tmp = tmp.drop(step) | |
} | |
if (!pad.isEmpty() && !tmp.isEmpty()) | |
add((tmp + pad).take(n)) | |
} | |
} | |
fun <T> Iterable<T>.partitionAll(n: Int, step: Int = n): List<List<T>> { | |
var tmp = toList() | |
return mutableListOf<List<T>>().apply { | |
while (!tmp.isEmpty()) { | |
add(tmp.take(n)) | |
tmp = tmp.drop(step) | |
} | |
} | |
} | |
list1.partition(3) | |
list1.partitionAll(3) | |
list1.partition(4,2) | |
list1.partitionAll(4,2) | |
list1.partition(4,2,listOf("a","b")) |
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
(partition-by identity list2) | |
(partition-by #(> % 7) list2) |
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 <T, R> Iterable<T>.partitionBy(function: (T) -> R): List<List<T>> { | |
if (count() == 0) return listOf() | |
return drop(1).fold(listOf(listOf(first()))) { acc, v -> | |
if (function(acc.last().last()) != function(v)) | |
acc + listOf(listOf(v)) | |
else | |
acc.dropLast(1) + listOf(acc.last() + v) | |
} | |
} | |
list2.partitionBy { it } | |
list2.partitionBy { it > 7 } |
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
(def list1 (range 1 6)) | |
(def list2 [6 6 7 8 7 7]) |
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
val list1 = IntRange(1, 5) | |
val list2 = listOf(6, 6, 7, 8, 7, 7) |
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
(reduce #(* %1 %2) input) | |
(reduce #(* %1 %2) 10 input) | |
(reduce #(if (> % 20) (reduced %) (* % %2)) input) |
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
list1.reduce { acc, v -> acc * v } | |
list1.fold(10) { acc, v -> acc * v }, | |
list1.reduce { acc, v -> if (acc > 20) return@reduce acc else acc * v } |
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
(reductions * list1) | |
(reductions * 10 list1) |
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 <T> Iterable<T>.reductions(function: (T, T) -> T): List<T> { | |
if (count() <= 1) return toList() | |
return drop(1).reductions(first(), function) | |
} | |
fun <T, R> Iterable<T>.reductions(init: R, function: (R, T) -> R): List<R> { | |
if (count() == 0) return listOf(init) | |
return fold(listOf(init)) { acc, v -> acc + function(acc.last(), v) } | |
} | |
list1.reductions { i, j -> i * j } | |
list1.reductions(10, Int::times) |
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
(->> (repeat 123) (take 3)) | |
(->> (iterate #(* 4 %) 3) (take 3)) |
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
generateSequence { 123 }.take(3) | |
generateSequence(3) { it * 4 }.take(3) |
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
(shuffle list2) |
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
import java.util.Collections | |
fun <T> Iterable<T>.shuffle(): List<T> = toMutableList().apply { Collections.shuffle(this) } | |
list2.shuffle() |
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
(some #(when (even? %) %) list1) |
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
list1.find { it % 2 == 0 } | |
list1.first { it % 2 == 0 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment