Last active
April 27, 2017 15:48
-
-
Save ice1000/015d44294fd1c5fd33cdbf7e65d2c7ed to your computer and use it in GitHub Desktop.
A Point-Free function implementation in Kotlin
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
package main | |
/** | |
* Created by ice1000 on 2017/4/27. | |
* | |
* @author ice1000 | |
*/ | |
fun <A, B, C : Any> zipWith(op: (A, B) -> C) = { x: Sequence<A> -> | |
{ y: Sequence<B> -> | |
val iX = x.iterator() | |
val iY = y.iterator() | |
generateSequence { | |
if (iX.hasNext() and iY.hasNext()) op(iX.next(), iY.next()) | |
else null | |
} | |
} | |
} | |
fun <T : Any> generate() = zipWith { x: Int, y: T -> "[$y * x^$x]" } ( | |
generateSequence(0, Int::inc) | |
) | |
fun main(args: Array<String>) = | |
generate<Int>()(sequenceOf(1, 1, 2, 3, 5, 8, 13, 21)).forEach(::println) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment