npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lazy-compose

v0.1.0

Published

Function composition monoid with lazy evaluation and strict equality

Downloads

12

Readme

Referential transparent lazy composition.

npm i --save @shammasov/lazy-compose

Abstract

Композиция функций это чистая функция, которая зависит только от аргументов. Таким образов композируя одни и те же функции в одинаковом порядке мы должны получить идентичную фукнцию .

Motivation

Частично вопрос идентичности композиций можно решить мемоизацией аргументов. Однако популярные реализации композиции функций не решают вопроса ассоциативной идентичности композиции.

кроме этого, рекурсивная реализация функциональной композиции создаёт оверхед при создании дополнительных элементов стека вызовов. При вызове композиции 5-ти и более функции это хорошо заметно.

Solution

Create monoid (or Semigroppoid & Category) in terms of fantasy-land for function composition. Laws:

import compose, {identity} from './src/lazyCompose'
import {add} from 'ramda'
const a = add(1)
const b = add(2)
const c = add(3)
compose(a, compose(b, c)) === compose(compose(a, b), c) // associativity

compose(a, identity) === a  //right identity
compose(identity, a) === a  //left identity

Use cases

  1. Advantages of using with redux mapStateToProps and selectors. Because composition of same function returns the same function

  2. Lens composition. You can create the same lenses by different composition ways. You can calculate lenses on a fly with result function equality. In this example I memoize lens constructor to return the same functions for same arguments

    import {lensProp, memoize} from 'ramda'
    import compose from './src/lazyCompose'
       
    const constantLens = memoize(lensProp)
    const lensA = constantLens('a')
    const lensB = constantLens('b')
    const lensC = constantLens('c')
    const lensAB = compose(lensB, lensA)
       
    console.log(
        compose(lensC, lensAB) === compose(lensC, lensB, lensA)
    )
           
  3. Memoized react callbacks directly composable with dispatch. In the following example elements of the list will not rerender with out changing id

    // constant - returns memoized identity function for an argument 
    // just like React.useCallback
    import {compose, constant} from './src/lazyCompose'
       
    const List = ({dispatch, data}) =>
        data.map( id =>
            <Button
                key={id}
                onClick={compose(dispatch, makeAction, contsant(id)) 
            />
        )
    )
           
    const Button = React.memo( props => 
        <button {...props} />
    )
           
    const makeAction = payload => ({
        type: 'onClick',
        payload,
    })
       
  4. Lazy composition of react components with no extra HOCs. lazy composition folds plain list of functions, with no additional closures

    import {memoize, mergeRight} from 'ramda'
    import {constant, compose} from './src/lazyCompose'
       
       
    const defaultProps = memoize(mergeRight)
       
    const withState = memoize( defaultState =>
        props => {
            const [state, setState] = React.useState(defaultState)
            return {...props, state, setState}
        }
    )
    
    const Component = ({value, label, ...props)) => 
        <label {...props}>{label} : {value}</label>    
           
    const withCounter = compose(  
        ({setState, state, ...props}) => ({
            ...props
            value: state,
            onClick: compose(setState,  constant(state + 1))
        }),
        withState(0),
    )   
    const Counter = compose(
        Component, 
        withCounter,
        defaultProps({label: 'Clicks'}),
    )
    
  5. Lazy Monads, where map - is a binary lazy composition, with possibility of strict equality implementation