Skip to content

Instantly share code, notes, and snippets.

@ice1000
Last active April 27, 2017 15:48
Show Gist options
  • Save ice1000/015d44294fd1c5fd33cdbf7e65d2c7ed to your computer and use it in GitHub Desktop.
Save ice1000/015d44294fd1c5fd33cdbf7e65d2c7ed to your computer and use it in GitHub Desktop.
A Point-Free function implementation in Kotlin
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