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

collatz-wasm

v0.1.2

Published

Collatz Conjecture function in WASM

Downloads

2

Readme

collatz-wasm

  1. pick a positive integer number
  2. a) when odd: 3x + 1
  3. b) when even: x/2
  4. repeat until you find a number you had before

on positive integers this algorithm (the mathematical equivalent is called Collatz Conjecture) ends always at 1

XKCD Collatz Conjecture

it is an unsolved mathematical problem, but you could give it a try in the browser or another system which can run wasm with this module ;-)

it accepts any javascript number (internally i32) and gives back an Array of numbers

usage

By installing from npm

npm install collatz-wasm

and by using a bundler you should be able to import it in javascript like this:

import init, { collatz } from "collatz-wasm"

...

init()
   .then(() => {
      collatz(1337)
   })

plans for the future

I plan on adding support for i64 (javascript BigInt), but have not yet reached a proper understanding of Rust Traits to do so. An alternative would be to copy code and replace i32 with i64, but who would do such a thing??

uses

The Collatz Conjecture can be used to plot nice graphs and art, which was a motivation to create this module

building from rust to wasm

install dependencies

First install a rust environment (e.g. rustup) and then install wasm-pack like-a-so: $ cargo install wasm-pack

build wasm bundle

based on this howto: https://developer.mozilla.org/en-US/docs/WebAssembly/Rust_to_wasm

$ wasm-pack build --target web

This builds the rust lib.rs into a file bundle at pkg/ which is ready to be imported into a javascript environment that has wasm support.

using it locally from the git repository

 import init, { collatz } from "./pkg/collatz_wasm.js"
 init()
   .then(() => {
      const list = Array.from(collatz(10))
      console.log(JSON.stringify(list))
   })

test wasm bundle locally

Start a local webserver (e.g. python3 -m http.server) and navigate to index.html to see a test page importing the compiled wasm (open Developer Tools to see result logged in the Javascript console).