Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
lazy-compose
Advanced tools
Function composition monoid with lazy evaluation and strict equality
Referential transparent lazy composition.
npm i --save @shammasov/lazy-compose
Композиция функций это чистая функция, которая зависит только от аргументов. Таким образов композируя одни и те же функции в одинаковом порядке мы должны получить идентичную фукнцию .
Частично вопрос идентичности композиций можно решить мемоизацией аргументов. Однако популярные реализации композиции функций не решают вопроса ассоциативной идентичности композиции.
кроме этого, рекурсивная реализация функциональной композиции создаёт оверхед при создании дополнительных элементов стека вызовов. При вызове композиции 5-ти и более функции это хорошо заметно.
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
Advantages of using with redux mapStateToProps and selectors. Because composition of same function returns the same function
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)
)
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,
})
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'}),
)
Lazy Monads, where map - is a binary lazy composition, with possibility of strict equality implementation
FAQs
Function composition monoid with lazy evaluation and strict equality
We found that lazy-compose demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.