<commit_before># Functional programming in R
[![Build Status](https://travis-ci.org/dirkschumacher/transduceR.png?branch=master)](https://travis-ci.org/dirkschumacher/transduceR)
[![Coverage Status](https://coveralls.io/repos/dirkschumacher/transduceR/badge.svg?branch=master)](https://coveralls.io/r/dirkschumacher/transduceR?branch=master)

A small package that implements Clojure's [transducers](http://clojure.org/transducers) in R. It also contains lazy sequences to work with infinite collection of items. 

Transducers let you define functions like map and filter decoupled from the underlying list datastructure. 

This package is currently in an alpha state and feedback is very welcome. Also the public API is far from ready and probably not optimal. I initially implemented it as an exercise but it might make sense to create a package out of it.

Current version: 0.2.0

# Installation

To install the current development version use devtools:

```R 
devtools::install_github("dirkschumacher/transduceR")
```

# Quick start
```R 
library(transducer)
library(magrittr)
keep(function(x) x %% 7 == 0) %.%
  map(function(x) x + 1) %.%
  take(2) %>%
  transduce(plus, 1:100)
# > 23
```

# API

## Reducing functions

* `transduce` takes a transducer, a step function and some collection and applies the transducers to it.


## Transducers
All functions return transducers.

* `contains`takes a predicate and terminates the process if the predicate returns `TRUE`. See prime number example.
* `discard` filters out elements that satisfy the predicate
* `distinct` filters outs duplicate elements
* `keep` filters elements that satisfy a predicate
* `flatten` flattens a list/sequence
* `flat_map` maps and then flattens the result
* `map` takes a functions and applies that function to each element. 
* `map_indexed` map, but the first element is the index of the element.
* `random_sample` randomly samples according to some success probability (supports a seed).
* `take` stops the process after n elements
* `take_nth` takes every nth element<commit_msg>Add example to read<commit_after># Functional programming in R
[![Build Status](https://travis-ci.org/dirkschumacher/transduceR.png?branch=master)](https://travis-ci.org/dirkschumacher/transduceR)
[![Coverage Status](https://coveralls.io/repos/dirkschumacher/transduceR/badge.svg?branch=master)](https://coveralls.io/r/dirkschumacher/transduceR?branch=master)

A small package that implements Clojure's [transducers](http://clojure.org/transducers) in R. It also contains lazy sequences to work with infinite collection of items. 

Transducers let you define functions like map and filter decoupled from the underlying list datastructure. 

This package is currently in an alpha state and feedback is very welcome. Also the public API is far from ready and probably not optimal. I initially implemented it as an exercise but it might make sense to create a package out of it.

Current version: 0.2.0

# Installation

To install the current development version use devtools:

```R 
devtools::install_github("dirkschumacher/transduceR")
```

# Quick start
```R 
library(transducer)
library(magrittr)
keep(function(x) x %% 7 == 0) %.%
  map(function(x) x + 1) %.%
  take(2) %>%
  transduce(plus, 1:100)
# > 23

map(function(x) x %% 7) %.%
  distinct() %.%
  take(2) %>%
  transduce(as_list, 1:100)
  
# > [[1]]
# > [1] 1
# > 
# > [[2]]
# > [1] 2
```

# API

## Reducing functions

* `transduce` takes a transducer, a step function and some collection and applies the transducers to it.


## Transducers
All functions return transducers.

* `contains`takes a predicate and terminates the process if the predicate returns `TRUE`. See prime number example.
* `discard` filters out elements that satisfy the predicate
* `distinct` filters outs duplicate elements
* `keep` filters elements that satisfy a predicate
* `flatten` flattens a list/sequence
* `flat_map` maps and then flattens the result
* `map` takes a functions and applies that function to each element. 
* `map_indexed` map, but the first element is the index of the element.
* `random_sample` randomly samples according to some success probability (supports a seed).
* `take` stops the process after n elements
* `take_nth` takes every nth element
{'fraction_non_alphanumeric': 0.09910965323336457, 'fraction_numerical': 0.008903467666354265, 'mean_word_length': 4.945682451253482, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '154844', 'n_tokens_mistral': 1337, 'n_tokens_neox': 1251, 'n_words': 487}