https://github.com/junkdog/transducers-kotlin
transducers for kotlin, adapted from https://gist.github.com/hastebrot/aa7b5366309d42270cc1
https://github.com/junkdog/transducers-kotlin
kotlin transducers
Last synced: 6 months ago
JSON representation
transducers for kotlin, adapted from https://gist.github.com/hastebrot/aa7b5366309d42270cc1
- Host: GitHub
- URL: https://github.com/junkdog/transducers-kotlin
- Owner: junkdog
- License: apache-2.0
- Created: 2017-01-19T03:30:17.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-03-31T21:28:26.000Z (over 8 years ago)
- Last Synced: 2025-04-23T02:49:07.243Z (6 months ago)
- Topics: kotlin, transducers
- Language: Kotlin
- Homepage:
- Size: 58.6 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/junkdog/transducers-kotlin)
## Transducers for kotlin
From the official [clojure documentation](https://clojure.org/reference/transducers):
> Transducers are composable algorithmic transformations. They are independent from the context of their input and output sources and specify only the essence of the transformation in terms of an individual element. Because transducers are decoupled from input or output sources, they can be used in many different processes - collections, streams, channels, observables, etc. Transducers compose directly, without awareness of input or creation of intermediate aggregates.
Refer to [CHANGELOG.md](https://github.com/junkdog/transducers-kotlin/blob/master/CHANGELOG.md) for latest updates.
### Basic usage
```
transduce(xf = ,
rf = ,
init = ,
input = )
``````kotlin
// '+' composes transducers
val composedTransducer = filter { i: Int -> i % 5 == 0 } +
sum { i: Int -> i / 5 }
// applying transducer + step function to input
transduce(xf = composedTransducer,
rf = { result, input -> result + input },
init = 0,
input = (0..20)
) assertEquals (1 + 2 + 3 + 4)
```Kotlin-like shorthands for transducing straight into a collection: `listOf`, `setOf`, and
`mapOf` take _transducer_ and _input_.```kotlin
listOf(xf = copy() +
branch(test = { it % 4 == 0 },
xfTrue = map { i: Int -> i * 100 },
xfFalse = map { i: Int -> i / 4 } +
distinct() +
map(Int::toString)),
input = (1..9)
) assertEquals listOf("0", 400, 400, "1", 800, 800, "2")
```## New/Experimental Transducers
_Experimental_ implies transducers not included with [transducers-java](https://github.com/cognitect-labs/transducers-java).### new regular transducers
- `collect`: input into a mutable collection, releases it upon computing the final result.
- `debug`: prints debug statements.
- `distinct`: no two equal elements shall pass.
- `head`: takes the first elements of an iterable input.
- `pairCat`: concatenates `Pair>` into `Pair`.
- `resultGate`: ensures _completion_ is calculated once, used with branching/muxing transducers.
- `sort`: collects and sorts all input.
- `tail`: takes the last elements of an iterable input.### new higher-order transducers
- `branch`: routes input through one out of two transducers, based on predicate.
- `join`: turns `List>` into `Transducer, B>`.
- `mapPair`: creates pairs based on two transducers.
- `mux`: input multiplexer, routing input over several transducer paths based on predicates.### new single item transducers
- `count`: number of input.
- `sum`: calculates sum, can be parameterized by a function for producing a custom value per input.Refer to the [API documentation][api-docs] and [tests][test-src] for details.
## Original source code:
The code in this repository is based on https://gist.github.com/hastebrot/aa7b5366309d42270cc1,
which in turn was based on the original port from [transducers-java][transducers-java]:
Spasi's [Transducers.kt][orig-gist1] and [TransducersTest.kt][orig-gist2] from Oct 12, 2014.## Resources:
- http://clojure.org/transducers
- http://blog.cognitect.com/blog/2014/8/6/transducers-are-coming
- https://github.com/cognitect-labs/transducers-java## Getting started
### Maven
```xml
net.onedaybeard.transducers
transducers
0.4.0```
### Gradle
```groovy
dependencies { compile "net.onedaybeard.transducers:transducers:0.4.0" }
```[transducers-java]: https://github.com/cognitect-labs/transducers-java
[test-src]: https://github.com/junkdog/transducers-kotlin/tree/master/src/test/kotlin/net/onedaybeard/transducers
[orig-gist1]: https://gist.github.com/Spasi/4052e4e8c8d88a7325fb
[orig-gist2]: https://gist.github.com/Spasi/2a9d7d420b20f37513d5
[api-docs]: http://junkdog.github.io/apidoc/transducers-0.4.0/net.onedaybeard.transducers/index.html