|
| 1 | +/** |
| 2 | + * @name Transducer.map |
| 3 | + * |
| 4 | + * @synopsis |
| 5 | + * ```coffeescript [specscript] |
| 6 | + * type Reducer = ( |
| 7 | + * accumulator any, |
| 8 | + * value any, |
| 9 | + * indexOrKey? number|string, |
| 10 | + * collection? Foldable, |
| 11 | + * )=>(nextAccumulator Promise|any) |
| 12 | + * |
| 13 | + * type Transducer = Reducer=>Reducer |
| 14 | + * |
| 15 | + * Transducer.map(mapperFunc function) -> mappingTransducer Transducer |
| 16 | + * ``` |
| 17 | + * |
| 18 | + * @description |
| 19 | + * Creates a mapping transducer. Items in the final reducing operation are transformed by the mapper function. It is possible to use an asynchronous mapper, however the reducing operation must support asynchronous execution. This library provides such implementations as [reduce](/docs/reduce) and [transform](/docs/transform). |
| 20 | + * |
| 21 | + * ```javascript [playground] |
| 22 | + * const square = number => number ** 2 |
| 23 | + * |
| 24 | + * const concat = (array, item) => array.concat(item) |
| 25 | + * |
| 26 | + * const mapSquare = Transducer.map(square) |
| 27 | + * // mapSquare is a transducer |
| 28 | + * |
| 29 | + * const squareConcatReducer = mapSquare(concat) |
| 30 | + * // now mapSquare is passed the reducer function concat; squareConcatReducer |
| 31 | + * // is a reducer with chained functionality square and concat |
| 32 | + * |
| 33 | + * console.log( |
| 34 | + * reduce([1, 2, 3, 4, 5], squareConcatReducer, []) |
| 35 | + * ) // [1, 4, 9, 16, 25] |
| 36 | + * |
| 37 | + * // the same squareConcatReducer is consumable with vanilla JavaScript |
| 38 | + * console.log( |
| 39 | + * [1, 2, 3, 4, 5].reduce(squareConcatReducer, []) |
| 40 | + * ) // [1, 4, 9, 16, 25] |
| 41 | + * |
| 42 | + * // concat is implicit when transforming into arrays |
| 43 | + * console.log( |
| 44 | + * transform([1, 2, 3, 4, 5], Transducer.map(square), []) |
| 45 | + * ) // [1, 4, 9, 16, 25] |
| 46 | + * ``` |
| 47 | + * |
| 48 | + * Read more on transducers [here](/blog/transducers-crash-course-rubico-v2). |
| 49 | + */ |
| 50 | +export function map(mapper: any): (arg0: any) => any; |
| 51 | +/** |
| 52 | + * @name Transducer.filter |
| 53 | + * |
| 54 | + * @synopsis |
| 55 | + * ```coffeescript [specscript] |
| 56 | + * type Reducer = ( |
| 57 | + * accumulator any, |
| 58 | + * value any, |
| 59 | + * indexOrKey? number|string, |
| 60 | + * collection? Foldable, |
| 61 | + * )=>(nextAccumulator Promise|any) |
| 62 | + * |
| 63 | + * type Transducer = Reducer=>Reducer |
| 64 | + * |
| 65 | + * Transducer.filter(predicate function) -> filteringTransducer Transducer |
| 66 | + * ``` |
| 67 | + * |
| 68 | + * @description |
| 69 | + * Creates a filtering transducer. A filtering reducer skips items of reducing operation if they test falsy by the predicate. It is possible to use an asynchronous predicate, however the reducing operation must support asynchronous execution. This library provides such implementations as [reduce](/docs/reduce) and [transform](/docs/transform). |
| 70 | + * |
| 71 | + * ```javascript [playground] |
| 72 | + * const isOdd = number => number % 2 == 1 |
| 73 | + * |
| 74 | + * const concat = (array, item) => array.concat(item) |
| 75 | + * |
| 76 | + * const concatOddNumbers = filter(isOdd)(concat) |
| 77 | + * |
| 78 | + * console.log( |
| 79 | + * [1, 2, 3, 4, 5].reduce(concatOddNumbers, []), |
| 80 | + * ) // [1, 3, 5] |
| 81 | + * ``` |
| 82 | + */ |
| 83 | +export function filter(predicate: any): (arg0: any) => any; |
| 84 | +/** |
| 85 | + * @name Transducer.flatMap |
| 86 | + * |
| 87 | + * @synopsis |
| 88 | + * ```coffeescript [specscript] |
| 89 | + * type Reducer = ( |
| 90 | + * accumulator any, |
| 91 | + * value any, |
| 92 | + * indexOrKey? number|string, |
| 93 | + * collection? Foldable, |
| 94 | + * )=>(nextAccumulator Promise|any) |
| 95 | + * |
| 96 | + * type Transducer = Reducer=>Reducer |
| 97 | + * |
| 98 | + * Transducer.flatMap(flatMapper) -> flatMappingTransducer Transducer |
| 99 | + * ``` |
| 100 | + * |
| 101 | + * @description |
| 102 | + * Creates a flatMapping transducer. A flatMapping transducer applies the flatMapping function to each item of the reducing operation, concatenating the results of the flatMapper execution into the final result. It is possible to use an asynchronous flatMapper, however the reducing operation must support asynchronous execution. This library provides such implementations as [reduce](/docs/reduce) and [transform](/docs/transform). |
| 103 | + * |
| 104 | + * ```javascript [playground] |
| 105 | + * const powers = number => [number, number ** 2, number ** 3] |
| 106 | + * |
| 107 | + * const numbers = [1, 2, 3, 4, 5] |
| 108 | + * |
| 109 | + * console.log( |
| 110 | + * transform(numbers, Transducer.flatMap(powers), []) |
| 111 | + * ) // [1, 1, 1, 2, 4, 8, 3, 9, 27, 4, 16, 64, 5, 25, 125] |
| 112 | + * ``` |
| 113 | + * |
| 114 | + * Read more on transducers [here](/blog/transducers-crash-course-rubico-v2). |
| 115 | + */ |
| 116 | +export function flatMap(flatMapper: any): (arg0: any) => any; |
| 117 | +/** |
| 118 | + * @name Transducer.forEach |
| 119 | + * |
| 120 | + * @synopsis |
| 121 | + * ```coffeescript [specscript] |
| 122 | + * type Reducer = ( |
| 123 | + * accumulator any, |
| 124 | + * value any, |
| 125 | + * indexOrKey? number|string, |
| 126 | + * collection? Foldable, |
| 127 | + * )=>(nextAccumulator Promise|any) |
| 128 | + * |
| 129 | + * type Transducer = Reducer=>Reducer |
| 130 | + * |
| 131 | + * Transducer.forEach(func function) -> forEachTransducer Transducer |
| 132 | + * ``` |
| 133 | + * |
| 134 | + * @description |
| 135 | + * Creates an effectful pasthrough transducer. The effectful passthrough transducer applies the effectful function to each item of the reducing operation, leaving the reducing operation unchanged. It is possible to use an asynchronous effectful function, however the reducing operation must support asynchronous execution. This library provides such implementations as [reduce](/docs/reduce) and [transform](/docs/transform). |
| 136 | + * |
| 137 | + * ```javascript [playground] |
| 138 | + * const numbers = [1, 2, 3, 4, 5] |
| 139 | + * transform(numbers, compose([ |
| 140 | + * Transducer.map(number => number ** 2), |
| 141 | + * Transducer.forEach(console.log), // 1 4 9 16 25 |
| 142 | + * ]), null) |
| 143 | + * ``` |
| 144 | + */ |
| 145 | +export function forEach(func: any): (arg0: any) => any; |
| 146 | +/** |
| 147 | + * @name Transducer.passthrough |
| 148 | + * |
| 149 | + * @synopsis |
| 150 | + * ```coffeescript [specscript] |
| 151 | + * type Reducer = ( |
| 152 | + * accumulator any, |
| 153 | + * value any, |
| 154 | + * indexOrKey? number|string, |
| 155 | + * collection? Foldable, |
| 156 | + * )=>(nextAccumulator Promise|any) |
| 157 | + * |
| 158 | + * type Transducer = Reducer=>Reducer |
| 159 | + * |
| 160 | + * Transducer.passthrough(func function) -> passthroughTransducer Transducer |
| 161 | + * ``` |
| 162 | + * |
| 163 | + * @description |
| 164 | + * Creates a pasthrough transducer. The passthrough transducer passes each item of the reducing operation through, leaving the reducing operation unchanged. |
| 165 | + * |
| 166 | + * ```javascript [playground] |
| 167 | + * const createAsyncNumbers = async function* () { |
| 168 | + * let number = 0 |
| 169 | + * while (number < 10) { |
| 170 | + * yield number |
| 171 | + * number += 1 |
| 172 | + * } |
| 173 | + * } |
| 174 | + * |
| 175 | + * transform(createAsyncNumbers(), Transducer.passthrough, []) |
| 176 | + * .then(console.log) // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 177 | + * ``` |
| 178 | + */ |
| 179 | +export function passthrough(reducer: any): any; |
| 180 | +/** |
| 181 | + * @name Transducer.tryCatch |
| 182 | + * |
| 183 | + * @synopsis |
| 184 | + * ```coffeescript [specscript] |
| 185 | + * type Reducer = ( |
| 186 | + * accumulator any, |
| 187 | + * item any, |
| 188 | + * indexOrKey? number|string, |
| 189 | + * collection? Foldable, |
| 190 | + * )=>(nextAccumulator Promise|any) |
| 191 | + * |
| 192 | + * type Transducer = Reducer=>Reducer |
| 193 | + * |
| 194 | + * Transducer.tryCatch( |
| 195 | + * transducerTryer Transducer, |
| 196 | + * catcher (error Error, item any)=>Promise|any, |
| 197 | + * ) -> tryCatchTransducer Transducer |
| 198 | + * ``` |
| 199 | + * |
| 200 | + * @description |
| 201 | + * Creates an error handling transducer. The error handling transducer wraps a transducer and catches any errors thrown by the transducer with the catcher function. The catcher function is provided the error as well as the original item (before any processing by the transducer) for which the error was thrown. It is possible for either the transducer or the catcher to be asynchronous, however the reducing operation must support asynchronous execution. This library provides such implementations as [reduce](/docs/reduce) and [transform](/docs/transform). |
| 202 | + * |
| 203 | + * ```javascript [playground] |
| 204 | + * const db = new Map() |
| 205 | + * db.set('a', { id: 'a', name: 'George' }) |
| 206 | + * db.set('b', { id: 'b', name: 'Jane' }) |
| 207 | + * db.set('c', { id: 'c', name: 'Jill' }) |
| 208 | + * db.set('e', { id: 'e', name: 'Jim' }) |
| 209 | + * |
| 210 | + * const userIds = ['a', 'b', 'c', 'd', 'e'] |
| 211 | + * |
| 212 | + * transform(userIds, Transducer.tryCatch(compose([ |
| 213 | + * Transducer.map(async userId => { |
| 214 | + * if (db.has(userId)) { |
| 215 | + * return db.get(userId) |
| 216 | + * } |
| 217 | + * throw new Error(`user ${userId} not found`) |
| 218 | + * }), |
| 219 | + * |
| 220 | + * Transducer.forEach(user => { |
| 221 | + * console.log('Found', user.name) |
| 222 | + * }) |
| 223 | + * ]), (error, userId) => { |
| 224 | + * console.error(error) |
| 225 | + * console.log('userId in catcher:', userId) |
| 226 | + * // original userId for which the error was thrown is provided |
| 227 | + * }), null) |
| 228 | + * ``` |
| 229 | +
|
| 230 | + */ |
| 231 | +export function tryCatch(transducerTryer: any, catcher: any): (arg0: any) => any; |
0 commit comments