All Projects → Shalimov → lzdash

Shalimov / lzdash

Licence: other
Lazy / non-strict evaluation in JavaScript

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to lzdash

functional-typescript-playground
A create-react-app based boilerplate featuring great functional programming oriented TypeScript-friendly libraries preinstalled for convenient forking & experimentation
Stars: ✭ 34 (+142.86%)
Mutual labels:  fp
fpEs
Functional Programming for EcmaScript(Javascript)
Stars: ✭ 40 (+185.71%)
Mutual labels:  fp
tradeio
A disciplined way to purely functional domain models in Scala
Stars: ✭ 19 (+35.71%)
Mutual labels:  fp
cofree
Fun and Games with Fix, Cofree, and Doobie
Stars: ✭ 37 (+164.29%)
Mutual labels:  fp
php-finder refactoring-kata
🐘🔍Incomprehensible Finder Refactoring Kata port for PHP
Stars: ✭ 22 (+57.14%)
Mutual labels:  fp
iterum
Handling iterables like lazy arrays.
Stars: ✭ 28 (+100%)
Mutual labels:  lazy-evaluation
workshop-edsl-in-typescript
Code template for workshop "Building eDSLs in functional TypeScript"
Stars: ✭ 49 (+250%)
Mutual labels:  fp
safe-string-interpolation
A type driven approach to string interpolation, aiming at consistent, secure, and only-human-readable logs and console outputs !
Stars: ✭ 14 (+0%)
Mutual labels:  fp
order-taking
Idris version of Domain Modeling Made Functional Book.
Stars: ✭ 113 (+707.14%)
Mutual labels:  fp
php-slang
The place where PHP meets Functional Programming
Stars: ✭ 107 (+664.29%)
Mutual labels:  fp
unpythonic
Supercharge your Python with parts of Lisp and Haskell.
Stars: ✭ 53 (+278.57%)
Mutual labels:  lazy-evaluation
apropos
Fast strong typed 'Either' data structure for typescript and flow
Stars: ✭ 20 (+42.86%)
Mutual labels:  fp
ts-belt
🔧 Fast, modern, and practical utility library for FP in TypeScript.
Stars: ✭ 439 (+3035.71%)
Mutual labels:  fp
SeqTools
A python library to manipulate and transform indexable data (lists, arrays, ...)
Stars: ✭ 42 (+200%)
Mutual labels:  lazy-evaluation
hkts
Functional programming tools: option, either, task, state, optics, etc.
Stars: ✭ 20 (+42.86%)
Mutual labels:  fp
scala-functional-programming-tutorial
Functional Programming in Scala Tutorial
Stars: ✭ 23 (+64.29%)
Mutual labels:  fp
fs2-google-pubsub
Google Cloud Pub/Sub stream-based client built on top of cats-effect, fs2 and http4s.
Stars: ✭ 32 (+128.57%)
Mutual labels:  fp
alleycat-reactive
A simple Python library to provide an API to implement the Reactive Object Pattern (ROP).
Stars: ✭ 15 (+7.14%)
Mutual labels:  fp
pyroclastic
Functional dataflow through composable computations
Stars: ✭ 17 (+21.43%)
Mutual labels:  fp
Swift-3-Functional-Programming
Code repository for Swift 3 Functional Programming, published by Packt
Stars: ✭ 78 (+457.14%)
Mutual labels:  fp

Lazy non-strict evaluation in JS; Subset of lodash/fp functions to work with collections effectively

lzdash is a functional utility library for JavaScript, contains subset of Lodash functions (api similar to lodash/fp), but with a lazy engine under the hood that strives to do as little work as possible while being as flexible as possible.

For:

import fp from 'lodash/fp'
import _ from 'lodash'
//---------------------
import lz from 'lzdash'

const fpSuperSum = fp.flow(
  fp.filter(v => v % 2 === 0),
  fp.map(v => v * v),
  fp.reduce((x, y) => x + y, 0)
)

// when u pass a collection to handle
// it starts from reduce and ask for `y` arg to be evaluated
// this request goes to map and then map do the same in relation to filter
// filter ask collection to get 1 item and pass item to map if it fits the condition
// filter holds map evaluation while predicate gives false (other words: condition is not true)
const lzSuperSum = lz.lazy(
  lz.filter(v => v % 2 === 0),
  lz.map(v => v * v),
  lz.reduce((x, y) => x + y, 0)
)

const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

const _filterResult = _.filter(data, v => v % 2 === 0) // 10 iterations => [2, 4, 6, 8, 10]
const _mapResult = _.map(_filterResult, v => v * v) // 5 iterations => [4, 16, 36, 64, 100]
const _result = _.reduce(_mapResult, (x, y) => x + y, 0) // 5 iterations => 220

console.log(
  fpSuperSum(data), // returns 220, iterations 10 by filter, 5 by map, 5 by reduce, total iterations 20
  _result, // returns 220, iterations 10 by filter, 5 by map, 5 by reduce, total iterations 20
  // ---------------------
  lzSuperSum(data), // returns 220, total iterations 10 because of lazy nature + sharing
)

It has no external dependencies, so you can get started right away with:

yarn add lzdash
// or
npm install lzdash

What we have:

export {
  lazy,
  map,
  flatMap,
  flatMapDeep,
  flatten,
  flattenDeep,
  consecutive,
  intersection,
  intersectionBy,
  difference,
  differenceBy,
  filter,
  reject,
  compact,
  reduce,
  take,
  takeWhile,
  drop,
  dropWhile,
  uniq,
  uniqBy,
  chunk,
  zip,
  zipWith,
  groupBy,
  countBy,
  keys,
  values,
  entries,
  fromPairs,
  // Infinite/Lazy sources
  lazySource,
  range,
  repeat,
  generator,
}

Caveats:

Because of the lazy nature of some of the functions, logical restrictions apply; explanation to why can be found below. Here is a list of restrictions:

  • reduce, groupBy, countBy, fromPairs makes sense to use only as trailing functions
  • keys, values, entries works a bit different in case if they are not leading (will be shown below)

NB!: Main point here being: you can use them wherever you want in the pipeline, but in almost all cases it makes sense for some of them be either trailing or leading. Because they are lazy and work with args as well as other lazy functions (whisper: one by one).

Example of usage:

import lz from 'lzdash'

// # trailing functions
lz.lazy(
  lz.filter(value => value % 2 === 0),
  lz.map(value => value ** 2),
  lz.reduce(...)
)

lz.lazy(
  lz.filter(value => value % 2 === 0),
  lz.map(value => value ** 2),
  lz.groupBy(...)
)

lz.lazy(
  lz.filter(value => value % 2 === 0),
  lz.map(value => value ** 2),
  lz.countBy(...)
)

// # leading functions

lz.lazy(lz.keys, ...otherFuncs)
lz.lazy(lz.values, ...otherFuncs)
lz.lazy(lz.entries, ...otherFuncs)

Let's start with reduce/groupBy/countBy:

Example:

const lzReduce = lz.lazy(
  lz.filter(value => value % 2 === 0),
  lz.map(value => value ** 2),
  lz.reduce((x, y) => x + y, 0)
)

lzReduce([1, 2, 3, 4, 5, 6]) // 56 -> works fine

// # BIG DIFF IS HERE

const lzReduce1 = lz.lazy(
  lz.map(value => value ** 2),
  lz.reduce((x, y) => x + y, 0),
  lz.map(afterReduce => afterReduce) // yep that's possible
)

// pls note, that map takes all intermediate results
// from reduce work, cuz reduce obey the rules
// of lazy evaluation and produce one result per iteration
lzReduce1([1, 2, 3]) // [1, 5, 14]

In lzReduce reduce returns the result of only one iteration and that's why we get such an effect. If this is not enough for you to understand, pls let me know in issues (tag your issue as [Question]) and I will replay as soon as I can.


When I'm talking about keys/values/entries I told that we have some diffs while working with this set of functions:

  • if you use them as leading functions, then there is no diff for u in comparison to other libs
  • if you use them inside flow (not leading position), it will take arg and do its magic over it, please take a look:
const fn = lz.lazy(
  lz.keys,
  lz.map(k => `key:${k}`)
)

fn({ a: 1, b: 2 }) // ['key:a', 'key:b']


const fnKeys = lz.lazy(
  lz.map(v => v) // actually we can remove it and it will work the same.
  lz.keys,
  lz.map(key => `_${key}_`)
)

// lz.lazy(lz.keys, lz.map(key => `_${key}_`)) almost the same as code example above
// but diff is in case if `keys` is leading func u can pass object to fnKeys
// otherwise it should be an array

const data = [
  { a: 1, b: 2 },
  { c: 1, d: 2 },
]

fnKeys(data) // ['_a_', '_b_', '_c_', '_d_']

Here you see that object after first map(v => v) goes to keys function, keys create a sequence of keys and move first of them to next map(key ....) and that's why we got the result like this.


Please, let me know if something still unclear for you and I will try to reveal all magic around.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].