z-pattern-matching / Z

Licence: apache-2.0
Pattern Matching for Javascript

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Z

Akar
First-class patterns for Clojure. Made with love, functions, and just the right amount of syntax.
Stars: ✭ 176 (-89.6%)
Mutual labels:  pattern-matching, functional-programming
Eslint Plugin Functional
ESLint rules to disable mutation and promote fp in JavaScript and TypeScript.
Stars: ✭ 282 (-83.34%)
Mutual labels:  immutability, functional-programming
Fpgo
Monad, Functional Programming features for Golang
Stars: ✭ 165 (-90.25%)
Mutual labels:  pattern-matching, functional-programming
Phunctional
⚡️ λ PHP functional library focused on simplicity and performance
Stars: ✭ 243 (-85.65%)
Mutual labels:  immutability, functional-programming
Pampy.js
Pampy.js: Pattern Matching for JavaScript
Stars: ✭ 544 (-67.87%)
Mutual labels:  pattern-matching, functional-programming
Prelude Ts
Functional programming, immutable collections and FP constructs for typescript and javascript
Stars: ✭ 315 (-81.39%)
Mutual labels:  immutability, functional-programming
Poica
🧮 A research programming language on top of C macros
Stars: ✭ 231 (-86.36%)
Mutual labels:  pattern-matching, functional-programming
Immutable Tuple
Immutable finite list objects with constant-time equality testing (===) and no memory leaks.
Stars: ✭ 29 (-98.29%)
Mutual labels:  immutability, functional-programming
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 (-75.31%)
Mutual labels:  pattern-matching, functional-programming
Qo
Qo - Query Object - Pattern matching and fluent querying in Ruby
Stars: ✭ 351 (-79.27%)
Mutual labels:  pattern-matching, functional-programming
Egison
The Egison Programming Language
Stars: ✭ 800 (-52.75%)
Mutual labels:  pattern-matching, functional-programming
Pattern Matching Ts
⚡ Pattern Matching in Typescript
Stars: ✭ 107 (-93.68%)
Mutual labels:  pattern-matching, functional-programming
Codezilla
⚡️ codezilla ⚡️ One giant 🦖 collection of algorithms & design patterns.
Stars: ✭ 127 (-92.5%)
Mutual labels:  functional-programming
Rxsealedunions
Compile-time checked Unions of different types for Domain Modeling [STABLE]
Stars: ✭ 130 (-92.32%)
Mutual labels:  functional-programming
List
🐆 An immutable list with unmatched performance and a comprehensive functional API.
Stars: ✭ 1,604 (-5.26%)
Mutual labels:  functional-programming
Functional Data Grid
Data grids in functional style with ReactJS
Stars: ✭ 125 (-92.62%)
Mutual labels:  functional-programming
Rubico
[a]synchronous functional programming
Stars: ✭ 133 (-92.14%)
Mutual labels:  functional-programming
Evm Lang Design
Language Design Community for the EVM: Intro and Resources
Stars: ✭ 130 (-92.32%)
Mutual labels:  functional-programming
Luafun
Lua Fun is a high-performance functional programming library for Lua designed with LuaJIT's trace compiler in mind.
Stars: ✭ 1,654 (-2.3%)
Mutual labels:  functional-programming
Patoline
Patoline typesetting system
Stars: ✭ 124 (-92.68%)
Mutual labels:  functional-programming

z Pattern matching for JavaScript

Build Status Coverage Status NPM version

Usage

  • Install via npm: npm install z
  • Require z in your code and use the matches function: const { matches } = require('z')

Avaiable Patterns

  • Matches by value: (x = 1) =>, (x = null) =>, (x = 'true') =>
  • Matches by object or array: (x = {a: 1}) =>, (x = [1, 2]) =>
  • Matches by type: (x = String) =>, (x = Boolean) =>
  • Matches by instance: (x = Date) =>, (x = Person) =>
  • Matches by splitting array into elements and tail (head, tail) => , (a, b, c, tail) =>, etc…

Examples

  • Example: Matches by Object property
const { matches } = require('z')

const person = { name: 'Maria' }
matches(person)(
  (x = { name: 'John' }) => console.log('John you are not welcome!'),
  (x)                    => console.log(`Hey ${x.name}, you are welcome!`)
)

//output: `Hey Maria, you are welcome!`
  • Example: Matches by type or instances
const { matches } = require('z')

const result = matches(1)(
  (x = 2)      => 'number 2 is the best!!!',
  (x = Number) => `number ${x} is not that good`,
  (x = Date)   => 'blaa.. dates are awful!'
)

console.log(result) // output: number 1 is not that good
  • Example: matches Array content

To match array content you need create multiple arguments for the match function, such as (a, b, c, tail) => {} , then each variable match each item from array. Note: last variable contains all remaining array items, formally named tail. Examples:

const { matches } = require('z')

matches([1, 2, 3, 4, 5])(
  (a, b, c, tail) => 'a = 1, b = 2, c = 3, tail = [4, 5]'  
)

matches([1, 2])(
  (a, tail) => 'a = 1, b = [2]'  
)

matches([1])(
  (a, b,  tail)       => 'Will not match here',
  (a = 2, tail = [])  => 'Will not match here',
  (a = 1, tail = [])  => 'Will match here, tail = []'
)
  • Example: Powerful recursive code which will remove sequential repeated items from Array.

Can be mind blowing if it’s the first time you meet pattern matching, but you are gonna understand it!

const { matches } = require('z')

const compress = (numbers) => {
  return matches(numbers)(
    (x, y, xs) => x === y
      ? compress([x].concat(xs))
      : [x].concat(compress([y].concat(xs))),
    (x, [y]) => x === y // stopping condition
      ? [x]
      : [x, y],
    x => x
  )
}

compress([1, 1, 2, 3, 4, 4, 4]) //output: [1, 2, 3, 4]

License

Apache 2.0

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].