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

khepri

v1.3.0

Published

Khepri Programming Language

Downloads

51

Readme

Khepri

Khepri is a programming language that reworks ECMAScript to be better for untyped functional-style programming. Khepri makes ECMAScript's syntax more concise and expressive, with support for lambdas, unpacks, and user defined infix and prefix operators.

Khepri also makes it easy to perform operations like function composition, partial application, binding variables in expressions, and using operators as functions, all with minimal performance overhead.

// Khepri function that sums elements of an input array and divides result by 2
var sum := foldl@((+), 0) \> (_ / 2);
sum [1, 2, 3, 4];

// VS ECMAScript to do the same
var sum = function(x) {
    return foldl(function(a, b) { return a + b; }, 0, x) / 2;
};
sum([1, 2, 3, 4]);

Unlike many other *script languages, Khepri's goal is not to replace Javascript by introducing new heavy weight language features, but rather to rework ECMAScript's syntax to be more flexible and expressive, while also helping programmers write safer code that can be better optimized.

Khepri compiles to plain old Javascript and does not require any runtime libraries. Khepri and Javascript can also be freely mixed in a project.

Links

Overview

Tiny lazy annotated stream library in Khepri. See documentation for details.

 // Declare a package and some exports
package (stream cons first rest forEach foldl reverse toArray from (+<))
{
// Declare some immutable bindings.
// These are only visible in the package.
var NIL := null;

// Using Khepri's lambda syntax.
// Lambda functions can be inlined by the compiler.
var flip := \f -> \x y -> f(y, x);

var constant := \x -> \ -> x;

// Export a symbol.
// This assignment marks `stream` as the export and declares that this is
// the final value of `stream`.
// Uses Khepri lambda syntax for a lambda returning object literal.
stream := \val f -> ({'first': val, 'rest': f});

// Function application without parens in `constant`
cons := \val s -> stream(val, constant s);

// Khepri's prefix and infix user defined operators can be used
// almost anywhere an identifier symbol can. They are lexically
// scoped as well, and you can even redefine builtin operators locally.
(+<) := cons;

// Convert an operator to a function and bind an argument
var isEmpty := (=== NIL);

// Unpacks parameters in lambda
first = \{first} -> first;
rest = \{rest} -> rest();

// Lambda with function block body
forEach = \f s -> {
    for (var head = s; !isEmpty head; head = rest head)
       f <| first head; // reverse pipe
};

foldl = \f z s -> {
    var r = z;
    forEach(\x -> { r = f(r, x); }, s);
    return r;
};

// Partial application
reverse = foldl@(flip cons, NIL);

toArray = foldl@([].concat.bind([]), []);

// Let Expression to bind a value in an expression
from = let
    // Recursive binding using `:=`
    fromImpl := \arr i len ->
        // Conditional expression
        ? i >= len
            :NIL
            :stream(
                arr.(i), // Get computed member i
                fromImpl@(arr, i + 1, len))
in
    // Unpacking with as pattern.
    \arr#{length} ->
        fromImpl(arr, 0, length);
}

Using the stream library in another file

package ()
with
    // Import the stream library and unpack some symbols.
    // We can now use our custom cons infix operator.
    import 'stream' stream#{(+<) foldl reverse}
in {
    // Declare global.
    // Khepri is lexically scoped and vars must be defined before use
    static console;
    
    var s = stream.from [2, 3, 4];
    
    // pipelining
    1 +< s // using our custom cons operator
        |> reverse
        |> foldl@((/), 120) // create function from operator
        |> console.log; // prints 5
}

See Nu for a more complete example of a lazy stream library written in Khepri.

Using Khepri

Install

$ npm install -g khepri

Compile

See the wiki for more complete compiler documentation.

# Compile file to stdout
$ khepri file.kep

# Get help
$ khepri --help

# Compile file to output file
$ khepri file.kep -o build/file.js

# Generate node package code in output
$ khepri file.kep -o build/file.js --package_manager=node

# Compile all *.kep files in a directory to an output directory
$ khepri lib -o dist

Code

Khepri is written in Javascript / Khepri. Node files are generated into dist_node.

Khepri is implemented in two libraries: khepri-parse for lexing and parsing, and khepri-compile for compiling Khepri to ECMAScript.