basic-compose

basic functional composition function


Keywords
cursor, functional, lens
License
ICU
Install
npm install basic-compose@5.0.0

Documentation

basic-compose

NPM

basic functional composition function. 366 ES5 bytes gziped.

returns a function that sequentially calls all given functions from last to first:

compose: (..., f3, f2, f1) => {
  x => ...(f3(f2(f1(x))))
  (...c, x) => compose(..., f3.curry(...c), f2.curry(...c), f1.curry(...c))(x)
}

by default, when the composed function is called with more than one argument (...c, x), it acts as if all given functions had been curried with the leading context arguments ...c, without the overhead of actually currying each function. this is useful, e.g. for composing reducers:

const reducer = compose(reducerC, reducerB, reducerA)
reducer(previous, current) ===
  reducerC(previous, reducerB(previous, reducerA(previous, current)))

alternatively, it is possible to define into which argument of each function the result from the previous function is injected, by calling compose.into with a corresponding offset: positive from index zero, negative from the length of the arguments to the composed function. see the API section below for more details.

Example

see this example in this directory. run this example in your browser.

import compose from 'basic-compose'
import { map, take, tap } from 'rxjs/operators'
import { interval } from 'rxjs/observable/interval'
import { Observable } from 'rxjs/Observable';

interval(1000).pipe(
  take(5),
  tap(log('input:')),
  compose<Observable<string>>(
    map((s: string) => `${s.length}${s}`),
    map((x: number) => '.'.repeat(x)),
    map((x: number) => 4 - x)
  )
)
.subscribe(log('output:'), log('error:'), log('done'))

API

declare const compose: Composer
export default compose
export interface Composer {
    <O>(...fns: Function[]): (...args: any[]) => O
    <O>(fns: Function[]): (...args: any[]) => O
    into(offset: number): Composer
}

for a detailed specification of this API, in particular for handling of corner cases, run the unit tests in your browser.

TypeScript

although this library is written in TypeScript, it may also be imported into plain JavaScript code: modern code editors will still benefit from the available type definition, e.g. for helpful code completion.

License

Copyright 2018 Stéphane M. Catala

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and Limitations under the License.