All Projects → santinic → Pampy.js

santinic / Pampy.js

Licence: mit
Pampy.js: Pattern Matching for JavaScript

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Pampy.js

Pampy
Pampy: The Pattern Matching for Python you always dreamed of.
Stars: ✭ 3,419 (+528.49%)
Mutual labels:  pattern-matching, lisp-interpreter, functional
when-switch
JavaScript functional implementation of switch/case
Stars: ✭ 20 (-96.32%)
Mutual labels:  functional, pattern-matching
Poica
🧮 A research programming language on top of C macros
Stars: ✭ 231 (-57.54%)
Mutual labels:  pattern-matching, functional-programming
Coconut
Simple, elegant, Pythonic functional programming.
Stars: ✭ 3,422 (+529.04%)
Mutual labels:  functional-programming, functional
Z
Pattern Matching for Javascript
Stars: ✭ 1,693 (+211.21%)
Mutual labels:  pattern-matching, functional-programming
Fpgo
Monad, Functional Programming features for Golang
Stars: ✭ 165 (-69.67%)
Mutual labels:  pattern-matching, functional-programming
Hof
Higher-order functions for c++
Stars: ✭ 467 (-14.15%)
Mutual labels:  functional-programming, functional
Aioreactive
Async/await reactive tools for Python 3.9+
Stars: ✭ 215 (-60.48%)
Mutual labels:  functional-programming, functional
Qo
Qo - Query Object - Pattern matching and fluent querying in Ruby
Stars: ✭ 351 (-35.48%)
Mutual labels:  pattern-matching, functional-programming
Kotlin Result
A multiplatform Result monad for modelling success or failure operations.
Stars: ✭ 369 (-32.17%)
Mutual labels:  functional-programming, functional
Carp
Carp is a programming language designed to work well for interactive and performance sensitive use cases like games, sound synthesis and visualizations.
Stars: ✭ 4,389 (+706.8%)
Mutual labels:  functional-programming, functional
Pattern Matching Ts
⚡ Pattern Matching in Typescript
Stars: ✭ 107 (-80.33%)
Mutual labels:  pattern-matching, functional-programming
Egison
The Egison Programming Language
Stars: ✭ 800 (+47.06%)
Mutual labels:  pattern-matching, functional-programming
Akar
First-class patterns for Clojure. Made with love, functions, and just the right amount of syntax.
Stars: ✭ 176 (-67.65%)
Mutual labels:  pattern-matching, functional-programming
Phunctional
⚡️ λ PHP functional library focused on simplicity and performance
Stars: ✭ 243 (-55.33%)
Mutual labels:  functional-programming, functional
Eslint Plugin Functional
ESLint rules to disable mutation and promote fp in JavaScript and TypeScript.
Stars: ✭ 282 (-48.16%)
Mutual labels:  functional-programming, functional
Helios
A purely functional JSON library for Kotlin built on Λrrow
Stars: ✭ 157 (-71.14%)
Mutual labels:  functional-programming, functional
Deep Waters
🔥Deep Waters is an easy-to-compose functional validation system for javascript developers 🔥
Stars: ✭ 188 (-65.44%)
Mutual labels:  functional-programming, functional
Whyhaskellmatters
In this article I try to explain why Haskell keeps being such an important language by presenting some of its most important and distinguishing features and detailing them with working code examples. The presentation aims to be self-contained and does not require any previous knowledge of the language.
Stars: ✭ 418 (-23.16%)
Mutual labels:  pattern-matching, functional-programming
Yalinqo
Yet Another LINQ to Objects for PHP [Simplified BSD]
Stars: ✭ 400 (-26.47%)
Mutual labels:  functional-programming, functional

Pampy in Star Wars

Pampy.js: Pattern Matching for JavaScript

License MIT Travis-CI Status Coverage Status npm version

Pampy.js is pretty small (250 lines, no dependencies), reasonably fast, and often makes your code more readable, and easier to reason about. There is also a Python version of Pampy.

You can write many patterns

Patterns are evaluated in the order they appear.

You can write Fibonacci

The operator _ means "any other case I didn't think of". If you already use _, you can require ANY, which is exactly the same.

let {match, _} = require("pampy");

function fib(n) {
    return match(n,
        1, 1,
        2, 1,
        _, (x) => fib(x - 1) + fib(x - 2)
    );
}

You can write a Lisp calculator in 5 lines

let {match, REST, _} = require("pampy");

function lisp(exp) {
    return match(exp,
        Function,           (x) => x,
        [Function, REST],   (f, rest) => f.apply(null, rest.map(lisp)),
        Array,              (l) => l.map(lisp),
        _,                  (x) => x
    );
}
let plus = (a, b) => a + b;
let minus = (a, b) => a - b;
let reduce = (f, l) => l.reduce(f);

lisp([plus, 1, 2]);                 // => 3
lisp([plus, 1, [minus, 4, 2]]);     // => 3
lisp([reduce, plus, [1, 2, 3]]);    // => 6

You can match so many things!

let {match, _} = require("pampy");

match(x,
    3,                "this matches the number 3",

    Number,           "matches any JavaScript number",

    [String, Number], (a, b) => "a typed Array [a, b] that you can use in a function",

    [1, 2, _],        "any Array of 3 elements that begins with [1, 2]",

    {x: _},           "any Object with a key 'x' and any value associated",

    _,                "anything else"
)

You can match TAIL

let {match, _, TAIL} = require("pampy");

x = [1, 2, 3];

match(x, [1, TAIL],   (t) => t);            // => [2, 3]

match(x, [_, TAIL],   (h, t) => [h, t]);    // => [1, [2, 3])

You can nest Arrays

let {match, _, TAIL} = require("pampy");

x = [1, [2, 3], 4];

match(x, [1, [_, 3], _], (a, b) => [1, [a, 3], b]);   // => [1, [2, 3], 4]

You can nest Objects. And you can use _ as key!

pet = { type: 'dog', details: { age: 3 } };

match(pet, {details: {age: _}}, (age) => age);        // => 3

match(pet, {_: {age: _}}, (a, b) => [a, b]);          // => ['details', 3]

Admittedly using _ as key is a bit of a trick, but it works for most situations.

You can use functions as patterns

match(x,
  x => x > 3,     x => `${x} is > 3`,
  x => x < 3,     x => `${x} is < 3`,
  x => x === 3,   x => `${x} is = 3`
)

You can pass [pattern, action] array pairs to matchPairs for better Prettier formatting.

let { matchPairs, _ } = require("pampy");

function fib(n) {
  return matchPairs(
    n,
    [0, 0],
    [1, 1],
    [2, 1],
    [3, 2],
    [4, 3],
    [_, x => fib(x - 1) + fib(x - 2)]
  )
}

All the things you can match

Pattern Example What it means Matched Example Arguments Passed to function NOT Matched Example
"hello" only the string "hello" matches "hello" nothing any other value
Number Any javascript number 2.35 2.35 any other value
String Any javascript string "hello" "hello" any other value
Array Any array object [1, 2] [1, 2] any other value
_ Any value that value
ANY The same as _ that value
[1, 2, _] An Array that starts with 1, 2 and ends with any value [1, 2, 3] 3 [1, 2, 3, 4]
[1, 2, TAIL] An Array that start with 1, 2 and ends with any sequence [1, 2, 3, 4] [3, 4] [1, 7, 7, 7]
{type:'dog', age: _ } Any Object with type: "dog" and with an age {type:"dog", age: 3} 3 {type:"cat", age:2}
{type:'dog', age: Number } Any Object with type: "dog" and with an numeric age {type:"dog", age: 3} 3 {type:"dog", age:2.3}
x => x > 3 Anything greater than 3 5 3 2
null only null null nothing any other value
undefined only undefined undefined nothing any other value

How to install

npm install pampy

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].