Skip to content

Commit

Permalink
feat(transducers): add toggle() xform
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Mar 20, 2019
1 parent f644ecd commit b5c744e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 31 deletions.
63 changes: 32 additions & 31 deletions packages/transducers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,37 @@ This project is part of the

<!-- TOC depthFrom:2 depthTo:3 -->

- [About](#about)
- [Tutorial](#tutorial)
- [5.0.0 release](#500-release)
- [Related packages](#related-packages)
- [Installation](#installation)
- [Dependencies](#dependencies)
- [Usage examples](#usage-examples)
- [Basic usage patterns](#basic-usage-patterns)
- [Fuzzy search](#fuzzy-search)
- [Histogram generation & result grouping](#histogram-generation--result-grouping)
- [Pagination](#pagination)
- [Multiplexing / parallel transducer application](#multiplexing--parallel-transducer-application)
- [Moving average using sliding window](#moving-average-using-sliding-window)
- [Benchmark function execution time](#benchmark-function-execution-time)
- [Apply inspectors to debug transducer pipeline](#apply-inspectors-to-debug-transducer-pipeline)
- [Stream parsing / structuring](#stream-parsing--structuring)
- [CSV parsing](#csv-parsing)
- [Early termination](#early-termination)
- [Scan operator](#scan-operator)
- [Weighted random choices](#weighted-random-choices)
- [Keyframe interpolation](#keyframe-interpolation)
- [API](#api)
- [Types](#types)
- [IReducible](#ireducible)
- [Transducer](#transducer)
- [Composition & execution](#composition--execution)
- [Transducers](#transducers)
- [Generators / Iterators](#generators--iterators)
- [Reducers](#reducers)
- [Authors](#authors)
- [License](#license)
- [About](#about)
- [Tutorial](#tutorial)
- [5.0.0 release](#500-release)
- [Related packages](#related-packages)
- [Installation](#installation)
- [Dependencies](#dependencies)
- [Usage examples](#usage-examples)
- [Basic usage patterns](#basic-usage-patterns)
- [Fuzzy search](#fuzzy-search)
- [Histogram generation & result grouping](#histogram-generation--result-grouping)
- [Pagination](#pagination)
- [Multiplexing / parallel transducer application](#multiplexing--parallel-transducer-application)
- [Moving average using sliding window](#moving-average-using-sliding-window)
- [Benchmark function execution time](#benchmark-function-execution-time)
- [Apply inspectors to debug transducer pipeline](#apply-inspectors-to-debug-transducer-pipeline)
- [Stream parsing / structuring](#stream-parsing--structuring)
- [CSV parsing](#csv-parsing)
- [Early termination](#early-termination)
- [Scan operator](#scan-operator)
- [Weighted random choices](#weighted-random-choices)
- [Keyframe interpolation](#keyframe-interpolation)
- [API](#api)
- [Types](#types)
- [IReducible](#ireducible)
- [Transducer](#transducer)
- [Composition & execution](#composition--execution)
- [Transducers](#transducers)
- [Generators / Iterators](#generators--iterators)
- [Reducers](#reducers)
- [Authors](#authors)
- [License](#license)

<!-- /TOC -->

Expand Down Expand Up @@ -716,6 +716,7 @@ tx.transduce(tx.map((x) => x*10), tx.push(), tx.range(4))
- [take](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/xform/take.ts)
- [throttleTime](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/xform/throttle-time.ts)
- [throttle](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/xform/throttle.ts)
- [toggle](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/xform/toggle.ts)
- [trace](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/xform/trace.ts)
- [wordWrap](https://github.com/thi-ng/umbrella/tree/master/packages/transducers/src/xform/word-wrap.ts)

Expand Down
1 change: 1 addition & 0 deletions packages/transducers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export * from "./xform/take-while";
export * from "./xform/take";
export * from "./xform/throttle";
export * from "./xform/throttle-time";
export * from "./xform/toggle";
export * from "./xform/trace";
export * from "./xform/word-wrap";

Expand Down
44 changes: 44 additions & 0 deletions packages/transducers/src/xform/toggle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Reducer, Transducer } from "../api";
import { iterator1 } from "../iterator";

/**
* Stateful transducer which accepts any input and flips between given
* `on` / `off` values for every value received. The `initial` state can
* be optionally provided (default: false) and must be given if used as
* an iterator.
*
* ```
* [...toggle(1, 0, false, [1, 2, 3, 4])]
* // [ 1, 0, 1, 0 ]
*
* [...tx.toggle("on", "off", true, [1, 2, 3, 4])]
* // [ 'off', 'on', 'off', 'on' ]
* ```
* @param on result for "on" state
* @param off result for "off" state
* @param initial initial state
*/
export function toggle<T>(on: T, off: T, initial?: boolean): Transducer<any, T>;
export function toggle<T>(
on: T,
off: T,
initial: boolean,
src: Iterable<any>
): IterableIterator<boolean>;
export function toggle<T>(
on: T,
off: T,
initial = false,
src?: Iterable<any>
): any {
return src
? iterator1(toggle(on, off, initial), src)
: ([init, complete, reduce]: Reducer<any, T>) => {
let state = initial;
return [
init,
complete,
(acc) => reduce(acc, (state = !state) ? on : off)
];
};
}

0 comments on commit b5c744e

Please sign in to comment.