# Kotlin ：强行 Point-Free

by

``````generate :: (Num a, Enum a) => [a] -> [a]
-- not point free
generate ls = zipWith (*) [0..] ls
``````

``````generate :: (Num a, Enum a) => [a] -> [a]
-- point free
generate = zipWith (*) [0..]
``````

## 解释

### 柯里化

``````fun a(a: Int,b: Int) = a + b
``````

``````val x = a(1)
val y = x(2)
print(y) // 3
``````

### Point-Free

``````fun plus1(x: Int) = a(1)(x)
``````

``````fun plus1() = a(1)
``````

## Kotlin 强行模仿

``````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 generate() = zipWith { x: Int, y: Int -> x * y } (
generateSequence(0, Int::inc)
)
``````

``````generate :: (Num a, Enum a) => [a] -> [a]
generate = zipWith (*) [0..]
``````

``````fun main(args: Array<String>) {
generate()(sequenceOf(1, 1, 2, 3, 5, 8, 13, 21))
// .forEach(::println)
}
``````

``````generate [1, 1, 2, 3, 5, 8, 13, 21]
``````

Easy~

## 完整的代码

Tweet this
Top

Create an issue to apply for commentary