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-tree

v1.0.2

Published

An implementation for lazily reducing values over trees

Downloads

9

Readme

Lazy Tree

Build Status

This package helps you lazily compute values over a tree.

npm install --save lazy-tree

API

import node, { reduce, thunk } from 'lazy-tree'

node is just a helper function for creating nodes on a tree. The "value" of a node must be an object, and the children of a node must be an array of nodes. Both "value" and "children" are optional. For example:

const tree = node({count: 1}, [
  node({count: 2}),
  node([
    node({count: 3}),
    node({count: 4}),
  ])
])

Next, we might want to reduce over that tree to get the total count. You don't need to null-check for nodes without values in your reducer. The second argument is the previous computation (we'll get to that in a second), and the third argument is the tree.

const add = (a,b) => ({count: a.count + b.count})
const computation = reduce(add, undefined, tree)
computation.result
// 10

One place where these trees can be useful is in representing some information in a UI component hierarchy. As the UI changes, we can lazily evaluation the next reduction over the tree by reusing the old computation. To do this, first we need to understand thunks.

thunk is a nifty little function. First you give it some means of comparing the arguments of a function:

const lazy = thunk((args1, args2) => args1[0] === args2[0])

Then you can wrap a pure function that generates a tree:

const render = lazy(count =>
  node({count}, [
    node({count: count * 2}),
    node({count: count * 3}),
  ])
)

Now, when you call this function with a value, you are returned a partially applied function that contains information about the original function and the partially applied arguments so you can compare them. For example:

render(1)()
// node({count: 1}, [
//   node({count: 2}),
//   node({count: 3}),
// ])
render(1).equals(render(1))
// true

You can also use these lazy nodes in place of nodes as you generate your tree:

render2 = lazy(count =>
  node([
    render(count),
    render(Math.abs(count * 2))
  ])
)

Now lets reduce over this lazy tree as we did before:

let calls = 0
const watch = (fn) => (...args) => {
  calls += 1
  return fn(...args)
}
const computation = reduce(watch(add), undefined, render2(1))
computation.result
// 18
calls
// 5

We've had to merge values 5 times, two for each render subtree and then once more to merge those subtrees. Now, the next time we want to reduce over our the tree, we can pass the previous computation object as the second argument, and we'll lazily reduce the tree, reusing the previous computation where the lazy nodes are equal.

calls = 0
const computation2 = reduce(watch(add), computation, render2(1))
computation2.result
// 18
calls
// 0

Because the lazy node is the same as before, we've reused the entire previous computation. Lets look at a more nuanced example:

calls = 0
const computation3 = reduce(watch(add), computation2, render2(-1))
computation3.result
// 6
calls
// 3

Now the top-level lazy node has changes, but because of the Math.abs in there, one of the subtrees is exactly the same lazy node so we don't have to traverse that whole subtree and were able to reuse the result from the previous computation.