Coder Social home page Coder Social logo

jfet97 / jducers Goto Github PK

View Code? Open in Web Editor NEW
23.0 3.0 2.0 35 KB

A js transducers-like implementation using ES9

License: MIT License

JavaScript 100.00%
javascript javascript-library generators generator-functions async asynchronous-programming hacktoberfest hacktoberfest2018 ecmascript ecmascript2015

jducers's Introduction

jducers

A js transducers-like implementation using generators and ES9 async generators.

A composed function expecting a combination function to make a reducer is called transducer. Transducers are useful to compose adjacent map(), filer() and reduce() operations together to improve performaces.

A jducer is similar to a transducer because it is a composed function and can be used to compose map(), filter() and reduce() operations together, but there are some differences.

It was a challenge against myself to see how far could I go using generators.

install

$ npm i --save jducers

utility

A little fp utility library used by jducers and available for you with pipe, compose, curry, partial and partialRight

import { pipe, compose, curry, partial, partialRight } from 'jducers/src/utility';

sync

Sync's jducers helpers:

import { map, filter, reduce, run }  from 'jducers/src/jducers/sync'

or

import * as SJ from 'jducers/src/jducers/sync'
  • map: accepts only a mapper function that is up to you and returns a function used in the creation of a jducer
  • filter: accepts only a predicate function that is up to you and returns a function used in the creation of a jducer
  • reduce: accepts two parameters: a reducer function with some constraints and an optional initial value and returns a function used in the creation of a jducer. The reducer function will be called with only two parameters: the accumulator and the current processed value
  • run: accepts two parameters: a composition (you can use pipe or compose from my utility library) and a sync iterable like an array. Returns the resuling array or a single value.It depends on the functions used in the composition
import * as SJ from 'jducers/src/jducers/sync'
import { pipe, partialRight } from 'jducers/src/utility';

let array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];

const isOdd = i => !!(i % 2); // predicate function
const double = i => i * 2; // mapper function
const sum = (acc, val) => acc + val; // reducer

const syncIsOddFilter = SJ.filter(isOdd);
const syncDoubleMap = SJ.map(double);
const syncSumReduce = SJ.reduce(sum);

const run = partialRight(SJ.run, array);

let jducer = pipe(syncIsOddFilter, syncDoubleMap);
let res = run(jducer);
console.log(res); // [2, 6, 10, 14, 18, 22, 26, 30, 34, 38]

jducer = pipe(syncIsOddFilter, syncDoubleMap, syncSumReduce);
res = run(jducer);
console.log(res); // 200

async

Async's jducers helpers (useful for async iterables and concurrent async iterations):

import { map, filter, reduce, run, observerFactory }  from 'jducers/src/jducers/async'

or

import * as AJ from 'jducers/src/jducers/async'
  • map: accepts only a mapper function that is up to you and returns a function used in the creation of a jducer
  • filter: accepts only a predicate function that is up to you and returns a function used in the creation of a jducer
  • reduce: accepts two parameters: a reducer function with some constraints and an optional initial value and returns a function used in the creation of a jducer. The reducer function will be called with only two parameters: the accumulator and the current processed value
  • run: accepts two parameters: a composition (you can use pipe or compose from my utility library) and an async iterable like an array of promises. Returns a promise that will be fulfilled with the resulting array or a single value. It depends on the functions used in the composition
  • observerFactory: accepts one or more callbacks and returns a simple observer that calls them when each single value of our async iterable flows through it. The observer has to be placed in a composition to form the jducer
import * as AJ from 'jducers/src/jducers/async'
import { pipe, partialRight } from 'jducers/src/utility';

const asyncArray = {
    array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
    [Symbol.asyncIterator]: async function* () {
        for (const x of this.array) {
            await new Promise(ok => setTimeout(() => ok(x), 2000));
            yield x;
        }
    }
}

const isOdd = i => !!(i % 2); // predicate function
const double = i => i * 2; // mapper function
const sum = (acc, val) => acc + val; // reducer

const asyncIsOddFilter = AJ.filter(isOdd);
const asyncDoubleMap = AJ.map(double);
const asyncSumReduce = AJ.reduce(sum);

const run = partialRight(AJ.run, asyncArray);

let jducer = pipe(asyncIsOddFilter, asyncDoubleMap);
let res = run(jducer); 
res.then(x => console.log(x)); // [2, 6, 10, 14, 18, 22, 26, 30, 34, 38]

jducer = pipe(asyncIsOddFilter, asyncDoubleMap, asyncSumReduce);
res = run(jducer);
res.then(x => console.log(x)); // 200

const observer = AJ.observerFactory(console.log);
/*
  const observer = observerFactory();
  observer.add(console.log);
*/
  
jducer = pipe(observer, asyncDoubleMap, observer, asyncSumReduce);
// we will see each value before and after the double mapper function
// 1 2 2 4 3 6 4 8 5 10 6 12 ...
res = run(jducer); 
res.then(x => console.log(x)); // 420

// WARNING: OUTPUTS ARE IN CONCURRENCY

weight

All modules and functions together are 3.284Kb without compression

performance

Bad, because yield is still an expensive operation

fun

A lot! ES9 is so powerful, in few lines I created something very difficult to do before without a library

license

MIT

jducers's People

Contributors

jfet97 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.