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

unary-js

v0.0.7

Published

Only unary function, for the glory of FP in JS.

Downloads

1

Readme

Unary JavaScript

Only unary functions, for the glory of Functional Programming in JS.

⚠️ DO NOT USE IN PROD️ ⚠️

Description

Unary JS is an educational attempt to demonstrate the concepts of Functional Programming (FP) in JavaScript.
The idea is to stick to pure unary functions to do any kind of calculation.

This implies that there is no longer any operator or control structure in the language, but only first-class functions, to compose and combine.

For example, iterations are always performed by recursion (no for / no while) and so on.
It's sadly preventing JS from becoming efficient in intensive iteration.
Even if TCO is part of ES6 standards, only Safari implements it. SpiderMonkey (Mozilla) don't have it and Chromium V8 dropped it therefore Firefox, Chrome, Edge and Node.js cannot ensure a good execution of this library !

We hopefully still can bypass absence of TCO, using a safe Y combinator (@see tco.ts file and tests).

But why ?

Example of use

Why this strange syntaxe actually works ?

Lambda Calculus and Turing Machine are two competing concepts covering the same field: automated calculations. They are very similar, both prove that with a machine capable of "memory" (state) and "jump instructions" (goto), we can perform any calculation.

With the Turing machine (and the Von Neuman architecture which inspires procedural and OOP languages), your program will be a set of "statements" and shared states:

  • create variables
  • mutate them with operations (+, -...), control structures and methods until the desired state is obtained

On the contrary, Lambda Calculus, leads to FP: your program will be a bunch of "expressions" and pure functions.

  • create functions (in terms of Input -> Transformation -> Output)
  • combine them, run them with immutable inputs, get your result

Most modern languages ​​are chaotic mixtures of these two approaches, giving us everything we need to code in both styles, often without even showing the boundary.

But due to equal Turing's completeness, you can substitute one approach for the other and get the same result with 2 very different programs - these are just different ways of describing the same problem.

This package focuses entirely on the Lambda Calculus / FP approach.

Why TypeScript?

JavaScript is loosely and dynamically typed. This is useful for quick POCs and small / solo projects. That said, it's not a perfect solution for creating libraries because the types:

  • helps you find a bug before runtime, even before compile time with a good IDE
  • forces to think of your function "by signature", helping you to define your problem before implementation
  • simplifies teamwork by denoting your intentions
  • provides automatic documentation
  • helps you understand your sets and categories of primitives / objects
  • helps you know when you are in a step of a curry function, or in the final return

Arguably, these advantages are theoretical, but practically overwhelming, and only shift the problems from runtime to compile-time. I can understand this limit, but here we want to discover and share the 1001 most important functions of Computer Science. It will be a total mess without documentation.

Also, we really want to provide a "signature search" tool, like in Haskell or Rust documentations. Indeed, if you know that you want to make your array a string, there will be few functions that will do it, and you will find what you are looking quickly, even without knowing the name "join", for example.

In the end, you can consume this library with or without TypeScript, the best of both worlds is available!

Why not just Ramda?

Unary-js really looks like Ramda: the functions have the same signature, the same name, you can combine them in the same way and they are curried.

But Ramda is a production project, aiming to make operators and main functions useful in a point-free style, without losing performance. In Ramda's implementations, you'll find a ton of "while", "if" and other statements that we don't want to use here. We are not focusing on performance at all, but only on unary functions without statements, from the bottom to the top.