Created
October 12, 2014 18:57
-
-
Save Spasi/2a9d7d420b20f37513d5 to your computer and use it in GitHub Desktop.
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
// Copyright 2014 Cognitect. All Rights Reserved. | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS-IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
// This is a Kotlin port of https://github.com/cognitect-labs/transducers-java | |
package transducers | |
import org.testng.annotations.Test | |
import java.util.concurrent.atomic.AtomicBoolean | |
import java.util.ArrayList | |
import stdlib.lang.* | |
Test public class TransducersTest { | |
class object { | |
private val STRINGIFY = map {(i: Long) -> i.toString() } | |
private val ADD_STRING = object : StepFunction<MutableList<String>, String> { | |
override fun apply(result: MutableList<String>, input: String, reduced: AtomicBoolean): MutableList<String> { | |
result add input | |
return result | |
} | |
} | |
private val ADD_INT = object : StepFunction<MutableList<Int>, Int> { | |
override fun apply(result: MutableList<Int>, input: Int, reduced: AtomicBoolean): MutableList<Int> { | |
result add input | |
return result | |
} | |
} | |
} | |
private fun ints(n: Int): List<Int> { | |
val list = ArrayList<Int>(n) | |
for ( i in n.indices ) | |
list add i | |
return list | |
} | |
private fun longs(n: Int): List<Long> { | |
val list = ArrayList<Long>(n) | |
for ( i in n.indices ) | |
list add i.L | |
return list | |
} | |
fun testMap() { | |
transduce( | |
map { it.toString() }, | |
object : StepFunction<String, String> { | |
override fun apply(result: String, input: String, reduced: AtomicBoolean): String { | |
return "$result$input " | |
} | |
}, "", ints(10) | |
) assertEquals "0 1 2 3 4 5 6 7 8 9 " | |
transduce( | |
map { it }, | |
object : StepFunction<MutableList<Int>, Int> { | |
override fun apply(result: MutableList<Int>, input: Int, reduced: AtomicBoolean): MutableList<Int> { | |
result add input + 1 | |
return result | |
} | |
}, ArrayList(), ints(10) | |
) assertEquals arrayListOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) | |
transduce(STRINGIFY, ADD_STRING, ArrayList(), longs(10)) assertEquals arrayListOf("0", "1", "2", "3", "4", "5", "6", "7", "8", "9") | |
} | |
fun testFilter() { | |
transduce( | |
filter { it % 2 != 0 }, ADD_INT, ArrayList(), ints(10) | |
) assertEquals arrayListOf(1, 3, 5, 7, 9) | |
} | |
fun testCat() { | |
val data = arrayListOf( | |
ints(10), | |
ints(20) | |
) | |
val vals = transduce( | |
cat(), ADD_INT, ArrayList(), data | |
) | |
vals.size assertEquals 30 | |
var nums = ints(10) | |
var i = 0 | |
for ( j in nums.indices ) | |
nums[j] assertEquals vals[i++] | |
nums = ints(20) | |
for ( j in nums.indices ) | |
nums[j] assertEquals vals[i++] | |
} | |
fun testMapcat() { | |
transduce( | |
mapcat {(i: Int) -> i.toString().toCharList() }, | |
object : StepFunction<MutableList<Char>, Char> { | |
override fun apply(result: MutableList<Char>, input: Char, reduced: AtomicBoolean): MutableList<Char> { | |
result add input | |
return result | |
} | |
}, ArrayList(), ints(10) | |
) assertEquals arrayListOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9') | |
} | |
fun testComp() { | |
transduce( | |
filter<Int> { it % 2 != 0 } comp map { it.toString() }, ADD_STRING, ArrayList(), ints(10) | |
) assertEquals arrayListOf("1", "3", "5", "7", "9") | |
transduce( | |
filter<Long> { it % 2L != 0L } comp STRINGIFY, ADD_STRING, ArrayList(), longs(10) | |
) assertEquals arrayListOf("1", "3", "5", "7", "9") | |
} | |
fun testTake() { | |
transduce( | |
take(5), ADD_INT, ArrayList(), ints(20) | |
) assertEquals arrayListOf(0, 1, 2, 3, 4) | |
} | |
fun testTakeWhile() { | |
transduce( | |
takeWhile { it < 10 }, ADD_INT, ArrayList(), ints(20) | |
) assertEquals arrayListOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) | |
} | |
fun testDrop() { | |
transduce( | |
drop(5), ADD_INT, ArrayList(), ints(10) | |
) assertEquals arrayListOf(5, 6, 7, 8, 9) | |
} | |
fun testDropWhile() { | |
transduce( | |
dropWhile { it < 10 }, ADD_INT, ArrayList(), ints(20) | |
) assertEquals arrayListOf(10, 11, 12, 13, 14, 15, 16, 17, 18, 19) | |
} | |
fun testTakeNth() { | |
transduce( | |
takeNth(2), ADD_INT, ArrayList(), ints(10) | |
) assertEquals arrayListOf(0, 2, 4, 6, 8) | |
} | |
fun testReplace() { | |
transduce( | |
replace(hashMapOf( | |
3 to 42 | |
)), ADD_INT, ArrayList(), ints(5) | |
) assertEquals arrayListOf(0, 1, 2, 42, 4) | |
} | |
fun testKeep() { | |
transduce( | |
keep { if ( it % 2 == 0 ) null else it }, ADD_INT, ArrayList(), ints(10) | |
) assertEquals arrayListOf(1, 3, 5, 7, 9) | |
} | |
fun testKeepIndexed() { | |
transduce( | |
keepIndexed {(index, value) -> if ( index == 1L || index == 4L ) value else null }, ADD_INT, ArrayList(), ints(10) | |
) assertEquals arrayListOf(0, 3) | |
} | |
fun testDedupe() { | |
transduce( | |
dedupe(), ADD_INT, ArrayList(), arrayListOf(1, 2, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 0) | |
) assertEquals arrayListOf(1, 2, 3, 4, 5, 0) | |
} | |
fun testPartitionBy() { | |
transduce( | |
partitionBy {(i: Int) -> i }, | |
object : StepFunction<MutableList<Iterable<Int>>, Iterable<Int>> { | |
override fun apply(result: MutableList<Iterable<Int>>, input: Iterable<Int>, reduced: AtomicBoolean): MutableList<Iterable<Int>> { | |
val ret = ArrayList<Int>() | |
for ( i in input ) | |
ret add i | |
result add ret | |
return result | |
} | |
}, ArrayList(), arrayListOf(1, 1, 1, 2, 2, 3, 4, 5, 5) | |
) assertEquals arrayListOf( | |
arrayListOf(1, 1, 1), | |
arrayListOf(2, 2), | |
arrayListOf(3), | |
arrayListOf(4), | |
arrayListOf(5, 5) | |
) | |
} | |
fun testPartitionAll() { | |
transduce( | |
partitionAll<Int>(3), | |
object : StepFunction<MutableList<Iterable<Int>>, Iterable<Int>> { | |
override fun apply(result: MutableList<Iterable<Int>>, input: Iterable<Int>, reduced: AtomicBoolean): MutableList<Iterable<Int>> { | |
val ret = ArrayList<Int>() | |
for ( i in input ) | |
ret add i | |
result add ret | |
return result | |
} | |
}, ArrayList(), ints(10) | |
) assertEquals arrayListOf( | |
arrayListOf(0, 1, 2), | |
arrayListOf(3, 4, 5), | |
arrayListOf(6, 7, 8), | |
arrayListOf(9) | |
) | |
} | |
fun testSimpleCovariance() { | |
val m = map<Int, Int> { it * 2 } | |
val input = ints(20) | |
val ADD_NUMBER = object : StepFunction<MutableCollection<Number>, Number> { | |
override fun apply(result: MutableCollection<Number>, input: Number, reduced: AtomicBoolean): MutableCollection<Number> { | |
result add input | |
return result | |
} | |
} | |
transduce( | |
m, ADD_NUMBER, ArrayList<Number>(input.size), input | |
).size assertEquals 20 | |
transduce( | |
m comp filter<Number> { it.toDouble() > 10.0 }, ADD_NUMBER, ArrayList<Number>(input.size), input | |
).size assertEquals 14 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment