All Projects → TeaEntityLab → fpEs

TeaEntityLab / fpEs

Licence: MIT license
Functional Programming for EcmaScript(Javascript)

Programming Languages

javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to fpEs

Fpgo
Monad, Functional Programming features for Golang
Stars: ✭ 165 (+312.5%)
Mutual labels:  monads, reactive, pubsub, reactive-programming, monad, functional-reactive-programming
Bow
🏹 Bow is a cross-platform library for Typed Functional Programming in Swift
Stars: ✭ 538 (+1245%)
Mutual labels:  fp, reactive-programming, monad
php-slang
The place where PHP meets Functional Programming
Stars: ✭ 107 (+167.5%)
Mutual labels:  fp, monad, optional
Rocket.jl
Functional reactive programming extensions library for Julia
Stars: ✭ 69 (+72.5%)
Mutual labels:  reactive, reactive-programming, functional-reactive-programming
Combinerxswiftperformance
A test suite comparing the performance of Combine and RxSwift
Stars: ✭ 154 (+285%)
Mutual labels:  reactive-programming, functional-reactive-programming, rx
Toy Rx
A tiny implementation of RxJS that actually works, for learning
Stars: ✭ 290 (+625%)
Mutual labels:  reactive, reactive-programming, rx
Awesome Rxjs
A collection of awesome RxJS resources
Stars: ✭ 314 (+685%)
Mutual labels:  reactive, reactive-programming, functional-reactive-programming
Monio
Async-capable IO monad for JS
Stars: ✭ 311 (+677.5%)
Mutual labels:  monads, fp, monad
Rx.Http
A reactive way to make HTTP Request in .NET Core 🚀
Stars: ✭ 62 (+55%)
Mutual labels:  reactive, reactive-programming, rx
assembler
Functional, type-safe, stateless reactive Java API for efficient implementation of the API Composition Pattern for querying/merging data from multiple datasources/services, with a specific focus on solving the N + 1 query problem
Stars: ✭ 102 (+155%)
Mutual labels:  reactive, reactive-programming, functional-reactive-programming
ObservableComputations
Cross-platform .NET library for computations whose arguments and results are objects that implement INotifyPropertyChanged and INotifyCollectionChanged (ObservableCollection) interfaces.
Stars: ✭ 94 (+135%)
Mutual labels:  reactive-programming, functional-reactive-programming, rx
J-Curry
A Java library that enables applying Functional Programming concepts like currying and partial application for functions, also it supports types like Either, Try, etc... using RxJava 2 interfaces, compatible with Java 7 and above
Stars: ✭ 17 (-57.5%)
Mutual labels:  monad, curry, currying
purescript-pop
😃 A functional reactive programming (FRP) demo created with PureScript events and behaviors.
Stars: ✭ 33 (-17.5%)
Mutual labels:  reactive, reactive-programming, functional-reactive-programming
purescript-outwatch
A functional and reactive UI framework based on Rx and VirtualDom
Stars: ✭ 33 (-17.5%)
Mutual labels:  reactive, rx
DARK
Dagger 2 + Anko + Rx + Kotlin
Stars: ✭ 27 (-32.5%)
Mutual labels:  reactive-programming, rx
most-behave
Experimental continuous Behaviors for most.js
Stars: ✭ 32 (-20%)
Mutual labels:  reactive-programming, functional-reactive-programming
InterReact
Interactive Brokers reactive C# API.
Stars: ✭ 28 (-30%)
Mutual labels:  reactive, rx
callbag-subscribe
A callbag sink (listener) that connects an Observer a-la RxJS. 👜
Stars: ✭ 17 (-57.5%)
Mutual labels:  reactive, rx
WhatFilm
Simple iOS app using TMDb API and RxSwift
Stars: ✭ 35 (-12.5%)
Mutual labels:  reactive-programming, functional-reactive-programming
springboot-rsocketjwt-example
Example of using JWT with RSocket and Spring Boot
Stars: ✭ 26 (-35%)
Mutual labels:  reactive, reactive-programming

fpEs

npm download npm version codecov Travis CI Build Status

license stars forks

Functional Programming for EcmaScript(Javascript)

Why

Originally I would like to have some features of Optional & Rx-like & PubSub functions;

however somehow that's too heavy if including them at the same time.

Thus the implementation just includes the core functions, and more clear to use.

Special thanks:

Installation

Node.js

node >= 6.0

  • Installation:
npm i fpes

Browser

bundled files for web/browser usages:

all


fp

maybe

monadio

pattern

publisher

Usage

Import

  • You can include the entire library:
import fpEs from 'fpEs';
  • There are 5 modules in this library, you can include them individually:
    • Facades:
      • maybe
      • monadio
      • publisher
    • FP functions:
      • fp
      • pattern

Just include things you need:

import {Maybe} from "fpEs";
// or this one:
/*
import Maybe from "fpEs/maybe";
*/

var m = Maybe.just(1); // It works

or

import {
  compose, curry,
} from "fpEs";

or

import {
  compose, curry,
} from "fpEs/fp";

Common FP (Compose, Curry)

Example:

import {
  compose, curry,
} from "fpEs/fp";

// compose

console.log(compose((x)=>x-8, (x)=>x+10, (x)=>x*10)(4)) // 42
console.log(compose((x)=>x+2, (x,y)=>x*y)(4,10)) // 42

// curry

console.log(curry((x, y, z) => x + y + z)(1,2,3)) // 6
console.log(curry((x, y, z) => x + y + z)(1)(2,3)) // 6
console.log(curry((x, y, z) => x + y + z)(1,2)(3)) // 6
console.log(curry((x, y, z) => x + y + z)(1)(2)(3)) // 6

PatternMatching

Example:

import {
  either,
  inCaseOfObject, inCaseOfEqual, inCaseOfClass, otherwise,

  SumType, ProductType, CompType,
  TypeNumber,
  TypeString,
  TypeNaN,
  TypeObject,
  TypeArray,
  TypeNull,
  TypeEqualTo,
  TypeClassOf,
  TypeRegexMatches,
} from "fpEs/pattern";

// PatternMatching

console.log(either({}, inCaseOfObject((x)=>JSON.stringify(x)), otherwise((x)=>false))); // "{}"
console.log(either([], inCaseOfObject((x)=>JSON.stringify(x)), otherwise((x)=>false))); // false
console.log(either(null, inCaseOfObject((x)=>JSON.stringify(x)), otherwise((x)=>false))); // false
console.log(either(undefined, inCaseOfObject((x)=>JSON.stringify(x)), otherwise((x)=>false))); // false
console.log(either("", inCaseOfObject((x)=>JSON.stringify(x)), otherwise((x)=>false))); // false

// otherwise

var err = undefined;

err = undefined;
either(1, inCaseOfEqual(1, (x)=>x+1)).should.equal(2);
(err === undefined).should.equal(true);

err = undefined;
try {
  either(1, inCaseOfEqual(2, (x)=>x+1));
} catch (e) {
  err = e;
}
(err === undefined).should.equal(false);

err = undefined;
try {
  either(1, inCaseOfEqual(2, (x)=>x+1), otherwise((x)=>x+2)).should.equal(3);
} catch (e) {
  err = e;
  console.log(e);
}
(err === undefined).should.equal(true);

// SumType

var s;
s = new SumType(new ProductType(TypeString, TypeNumber), new ProductType(TypeRegexMatches('c+')));
console.log(s.apply("1", "2asdf") === undefined); // true
console.log(s.apply("1", 2) === undefined); // false

console.log(s.apply("1") === undefined); // true
console.log(s.apply("ccc") === undefined); // false

Maybe (Sync)

Example:

import Maybe from "fpEs/maybe";

var m;

// map (sync)

m = Maybe.just(1).map((x)=>x+2).map((x)=>x+3);
console.log(m.unwrap()); // 6

// isPresent/isNull

m = Maybe.just(1);
console.log(m.isPresent()); // true
console.log(m.isNull()); // false
m = Maybe.just(null);
console.log(m.isPresent()); // false
console.log(m.isNull()); // true
m = Maybe.just(undefined);
console.log(m.isPresent()); // false
console.log(m.isNull()); // true

// Or

m = Maybe.just(1);
console.log(m.or(3).unwrap()); // 1
console.log(m.or(4).unwrap()); // 1
m = Maybe.just(null);
console.log(m.or(3).unwrap()); // 3
console.log(m.or(4).unwrap()); // 4
m = Maybe.just(undefined);
console.log(m.or(3).unwrap()); // 3
console.log(m.or(4).unwrap()); // 4

// letDo

m = Maybe.just(1);
v = 0;
m.letDo(function () {
  v = 1;
});
console.log(v); // 1
m = Maybe.just(null);
v = 0;
m.letDo(function () {
  v = 1;
});
console.log(v); // 0
m = Maybe.just(undefined);
v = 0;
m.letDo(function () {
  v = 1;
});
console.log(v); // 0

// letDo & orDo
m = Maybe.just(0);
v = m.letDo(function (p) {
  return p + 2
}).orDo(function () {
  return 3
}).unwrap();
console.log(v); // 2
m = Maybe.just(undefined);
v = m.letDo(function (p) {
  return p + 2
}).orDo(function () {
  return 3
}).unwrap();
console.log(v); // 3

MonadIO/Rx.Observable (Async,Sync)

Example:

import Maybe from "fpEs/maybe";
import MonadIO from "fpEs/monadio";
var {promiseof, doM} = MonadIO;



var p = undefined;
var m = MonadIO.just(0);
var v = 0;

// sync

m = MonadIO.just(0);
v = 0;
m
.map((val)=>val+1)
.map((val)=>val+2)
.flatMap((val)=>MonadIO.just(val+1).map((val)=>val+1).map((val)=>val+1))
.subscribe((val)=>v=val);

console.log(v); // 6

// async

m = MonadIO.just(0);
v = 0;
p = m
.map((val)=>val+1)
.map((val)=>val+2)
.map((val)=>val+3)
.subscribe((val)=>v=val, true); // Async: true

console.log(v); // 0
p.then(function () {
  console.log(v); // 6
});

// DoNotation

v = 0;
p = doM(function *() {
  var value = yield promiseof(5);
  var value2 = yield promiseof(11);
  var value3 = yield Maybe.just(3);
  var value4 = yield MonadIO.just(3);
  return value + value2 + value3 + value4;
});

p.then((x)=>console.log(x)); // 22

Publisher(PubSub-like)

Example:

import Publisher from "fpEs/publisher";

var p = new Publisher();
var v = 0;

// sync

p = new Publisher();
v = 0;
p.subscribe((i)=>v=i);
p.publish(1);

console.log(v); // 1

// async

p = new Publisher();
v = 0;

p.subscribe((i)=>v=i);
p.publish(1, true); // Async: true
console.log(v); // 0

setTimeout(()=>{
  console.log(v); // 1
},100);

// map

p = new Publisher();
v = 0;

p.map((x)=>x+2).map((x)=>x+3).subscribe((i)=>v=i);
p.publish(1, true);
console.log(v); // 0

setTimeout(()=>{
  console.log(v); // 6
},100);

// unsubscribe

p = new Publisher();
v = 0;
var callback = (i)=>v=i;

p.subscribe(callback);
p.publish(1);
console.log(v); // 1
v = 0;
p.unsubscribe(callback);
p.publish(1);
console.log(v); // 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].