Coder Social home page Coder Social logo

marpple / fxts Goto Github PK

View Code? Open in Web Editor NEW
860.0 12.0 64.0 7.29 MB

A functional programming library for TypeScript/JavaScript

Home Page: https://fxts.dev

License: Apache License 2.0

JavaScript 1.72% TypeScript 97.88% Shell 0.09% Makefile 0.04% CSS 0.27%
fp typescript lazy concurrency javascript

fxts's Introduction

Build Status npm version

fxts-icon FxTS

FxTS is a functional library for TypeScript/JavaScript programmers.

Why FxTS?

  • Lazy evaluation
  • Handling concurrent requests
  • Type inference
  • Follow iteration protocols Iterable / AsyncIterable

Installation

npm install @fxts/core

Documentation

Please review the API documentation

Usage

import { each, filter, fx, map, pipe, range, take } from "@fxts/core";

pipe(
  range(10),
  map((a) => a + 10),
  filter((a) => a % 2 === 0),
  take(2),
  each((a) => console.log(a)),
);

// chaining
fx(range(10))
  .map((a) => a + 10)
  .filter((a) => a % 2 === 0)
  .take(2)
  .each((a) => console.log(a));

Usage(concurrent)

import { concurrent, countBy, flat, fx, map, pipe, toAsync } from "@fxts/core";

// maybe 1 seconds api
const fetchWiki = (page: string) =>
  fetch(`https://en.wikipedia.org/w/api.php?action=parse&page=${page}`);

const countWords = async (concurrency: number) =>
  pipe(
    ["html", "css", "javascript", "typescript"],
    toAsync,
    map(fetchWiki),
    map((res) => res.text()),
    map((words) => words.split(" ")),
    flat,
    concurrent(concurrency),
    countBy((word) => word),
  );

await countWords(); // 4 seconds
await countWords(2); // 2 seconds

you can start here

Build

  • npm run build

Running Test

  • npm test

Running Type Test

  • npm run compile:check

License

Apache License 2.0

fxts's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fxts's Issues

Add `omitBy` and `pickBy` function

Suggestion

โญ Suggestion

๐Ÿ’ป Use Cases

pickBy(
  ([key, value]) => key === 'b' || typeof value === 'boolean',
  { a: 1, b: '2', c: true }
);
// { b: '2', c: true }

await pickBy(
  async ([key, value]) => key === 'b' || typeof value === 'boolean',
  { a: 1, b: '2', c: true }
);

pipe(
  { a: 1, b: '2', c: true },
  pickBy(([key, value]) => key === 'b' || typeof value === 'boolean')
);

await pipe(
  { a: 1, b: '2', c: true },
  pickBy(async ([key, value]) => key === 'b' || typeof value === 'boolean')
);
omitBy(
  ([key, value]) => key === 'b' || typeof value === 'boolean',
  { a: 1, b: '2', c: true }
);
// { a: 1 }

await omitBy(
  async ([key, value]) => key === 'b' || typeof value === 'boolean',
  { a: 1, b: '2', c: true }
);

pipe(
  { a: 1, b: '2', c: true },
  omitBy(([key, value]) => key === 'b' || typeof value === 'boolean')
);

await pipe(
  { a: 1, b: '2', c: true },
  omitBy(async ([key, value]) => key === 'b' || typeof value === 'boolean')
);

Missed signature in `reduce` function

Bug Report

๐Ÿ’ป Code

const work = reduce((a, b) => a + b, [1, 2, 3]);
const notWork = reduce((a, b) => a + b, toAsync([1, 2, 3]));

๐Ÿ™ Actual behavior

There is a problem that the value of a is inferred as unknown when AsyncIterable type is passed as an argument.
This bug is probably caused by missing some signatures of the reduce function.

๐Ÿ™‚ Expected behavior

It would be nice to be able to infer the value a in AsyncIterable normally.


Version Infomation

  • Operating system: Mac 12.0.1
  • Typescript: 4.5.0
  • Nodejs: 14.18.1

Add `pick` and `omit` function

Suggestion

โญ Suggestion

๐Ÿ’ป Use Cases

const product = {
  name: 'iphone',
  price: 1000,
  description: '...',
  image_url: '...',
};

pick(["name", "price"], product); // { name: 'iphone', price: 1000 }

omit(["name", "price"], product); // { description: '...', image_url: '...' }

Add `compress` function

Suggestion

โญ Suggestion

ref https://docs.python.org/3/library/itertools.html#itertools.compress

๐Ÿ’ป Use Cases

const iter1 = compress([false,true,false,false,true],  [1, 2, 3, 4, 5]);
iter1.next(); // {value: 2, done:false}
iter1.next(); // {value: 5, done:false}
iter1.next(); // {value: undefined, done:true }

const iter2 = compress([1,0,0,1,0],  "abcde");
iter2.next(); // {value: "a", done:false}
iter2.next(); // {value: "d", done:false}
iter2.next(); // {value: undefined, done:true }

Need `join` function

Suggestion

โญ Suggestion

I would like to use join function like other library lodash#join

๐Ÿ’ป Use Cases

_.join(['a', 'b', 'c'], '~');
// => 'a~b~c'

Add `intersectionBy` function

Suggestion

โญ Suggestion

๐Ÿ’ป Use Cases

const iter = intersectionBy((o) => o.x, [{ x: 2 }, { x: 1 }], [{ x: 1 }, {x : 3}, {x : 4}]);
iter.next(); // {value:{x: 1}, done:false}
iter.next(); // {value:undefined, done:true}

Add `keys` function

Suggestion

โญ Suggestion

Returns a Iterable/AsyncIterable containing the own properties of the given object

๐Ÿ’ป Use Cases

const iter = keys({a:1, b:2, c:3 });
iter.next(); // {value: 'a', done:false}
iter.next(); //  {value:'b', done:false}
iter.next(); //  {value:'c', done:false}
iter.next(); //  {value:undefined, done:true}

Add `isNil` function

Suggestion

โญ Suggestion

Checks if the given value is null or undefined.

๐Ÿ’ป Use Cases

isNil(1); // false
isNil('1'); // false
isNil(undefined); // true
isNil(null); // true

Return type of pipe using each isn't inferred correctly

Bug Report

๐Ÿ’ป Code

const ex1 = pipe([1, 2, 3], each(console.log)); // const ex1: void      inferred correctly  

const ex2 = pipe([1, 2, 3], take(1), each(console.log)); //   const ex2: IterableIterator<number>     inferred incorrectly 

๐Ÿ™ Actual behavior

Type of ex2 is inferred IterableIterator<number>

๐Ÿ™‚ Expected behavior

Type of ex2 is expected to be void.


Version Infomation

  • Operating system: Linux 5.11.0-40-generic # 44~20.04.2-Ubuntu SMP Tue Oct 26 18:07:44 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
  • Typescript: Version 4.5.2
  • Nodejs: v14.17.4
  • FxTS: v.0.2.2

Add `zipWith` function

Suggestion

โญ Suggestion

Returns Iterable/AsyncIterable out of the two supplied by applying f to each same positioned pair in Iterable/AsyncIterable.

๐Ÿ’ป Use Cases

const iter = zipWith((a,b) => [a,b], [1,2,3], ['a','b','c']);
iter.next(); // {value: [1, 'a'] , done: false}
iter.next(); // {value: [2, 'b'] , done: false}
iter.next(); // {value: [3, 'c'] , done: false}

Add `intersection` function

Suggestion

โญ Suggestion

Combines two Iterable or AsyncIterable into a set (i.e. no duplicates) composed of those elements common to both Iterable/AsyncIterable.

๐Ÿ’ป Use Cases

const iter = intersection([1,2,3,4], [4,3,5,6]);
iter.next() // {value: 4, done:false};
iter.next() // {value: 3, done:false};
iter.next() // {value: undefined, done:true};

Add `consume` function

Suggestion

โญ Suggestion

Consumes the given number of Iterable/AsyncIterable. If the number is empty, all is consumed.

๐Ÿ’ป Use Cases

const iterator = (function *(){ 
  yield 1; 
  yield 2; 
  yield 3;  
})();
consume(iterator, 2);
iterator.next(); // {value:3, done:false}

// with pipe
pipe(
  range(10),
  peek(updateApi),
  concurrent(5),
  consume,
);

Add `cycle` function

Suggestion

โญ Suggestion

ref https://docs.python.org/3/library/itertools.html#itertools.cycle

๐Ÿ’ป Use Cases

const iter = cycle("abc")
iter.next(); // {value:"a", done: false}
iter.next(); // {value:"b", done: false}
iter.next(); // {value:"c", done: false}
iter.next(); // {value:"a", done: false}
iter.next(); // {value:"b", done: false}

// with pipe
pipe(
  cycle([1,2,3,4]),
  take(5),
  toArray,
); // [1,2,3,4,1]

Add `average` function

Suggestion

โญ Suggestion

๐Ÿ’ป Use Cases

average([1,2,3,4,5]); // 3
await average(toAsync([1,2,3,4,5])); // 3

Add `values` function

Suggestion

โญ Suggestion

Returns a Iterable/AsyncIterable of all values of the given object

๐Ÿ’ป Use Cases

const iter = values({a:1,b:2,c:3});
iter.next(); // {value:1, done:false}
iter.next(); // {value:2, done:false}
iter.next(); // {value:3, done:false}
iter.next(); // {value:undefined, done:true}

Feature request - `curry` utility

Suggestion

โญ Suggestion

curry is an essential utility for FP, yet most libraries (especially lodash) fail to provide correct type definition.
Are there any plans for exporting curry? (since other utilities from fxts support currying, as I noticed)

๐Ÿ’ป Use Cases

const strictEq = curry(<T>(left: T, right: T) => left === right)

const twos = pipe([1, 1, 1, 2, 2, 2], filter(strictEq(2))) // [2, 2, 2]

Add `includes` function

Suggestion

โญ Suggestion

Checks if the specified value is equal.

๐Ÿ’ป Use Cases

includes('c', 'abcd'); // true
includes(3, [1,2,3,4]); // true

Add `juxt` function

Suggestion

โญ Suggestion

Applies a list of function to a Iterable/AsyncIterable of values.

๐Ÿ’ป Use Cases

juxt([Math.max, Math.min])(1,2,3,-4,5,6) // [6, -4]

Add `slice` function

Suggestion

โญ Suggestion

Returns an Iterable/AsyncIterable of the given elements from startIndex(inclusive) to endIndex(exclusive).

๐Ÿ’ป Use Cases

const iter1 = slice(0, 2, [1,2,3,4,5]); // [1, 2]
iter1.next(); // {value:1, done:false}
iter1.next(); // {value:2, done:false}
iter1.next(); // {value:undefined, done:true}


const iter2 = slice(0, 2, 'abcde'); // [1, 2]
iter2.next(); // {value:'a', done:false}
iter2.next(); // {value:'b', done:false}
iter2.next(); // {value:undefined, done:true}

Add `reverse` function

Suggestion

โญ Suggestion

Returns the given elements in reverse order.

๐Ÿ’ป Use Cases

reverse([1,2,3,4,5,6]); // [6,5,4,3,2,1]
reverse('abcde'); // 'edcba'

Add `size` function

Suggestion

โญ Suggestion

๐Ÿ’ป Use Cases

size(range(10, 16)); // 5
await size(toAsync(range(10, 16))); // 5

Add `findIndex` function

Suggestion

โญ Suggestion

Returns the index of the first element of Iterable/AsyncIterable which matches f, or -1 if no element matches.

๐Ÿ’ป Use Cases

const arr = [{a:1}, {a:2}, {a:3}]
findIndex((obj) =>  obj.a === 1, arr); // 0 
findIndex((obj) =>  obj.a === 2, arr); // 1 
findIndex((obj) =>  obj.a === 4, arr); // -1

Add `apply` function

Suggestion

โญ Suggestion

๐Ÿ’ป Use Cases

apply(Math.max, [1, 2, 3, 4, 5]); // 5

pipe(
  repeat(10),
  map(a => a * Math.random())
  take(5),
  apply(max)
);

Add `noop` function

Suggestion

โญ Suggestion

Returns undefined

๐Ÿ’ป Use Cases

noop() // undefined

Add `evolve` function

Suggestion

โญ Suggestion

๐Ÿ’ป Use Cases

  const add1 = (a: number) => a + 1;
  const toUpper = (a: string) => a.toUpperCase();

  const obj = { a: 1, b: 'hello', c: true };

  evolve({ a: add1, b: toUpper, c: not }, obj);
  // { a: 2, b: 'HELLO', c: false }

Add `repeat` function

Suggestion

โญ Suggestion

Returns a Iterable/AsyncIterable of size n containing a specified value.

๐Ÿ’ป Use Cases

const iter = repeat(2, 10);
iter.next(); // {value: 10, done:false}
iter.next(); // {value: 10, done:false}
iter.next(); // {value: undefined, done:true}

// with pipe
pipe(
  repeat(2, 10),
  toArray,
); // [10, 10]

Add `differenceBy` function

Suggestion

โญ Suggestion

๐Ÿ’ป Use Cases

const iter = differenceBy(a => a.x, [{ x: 1 }, { x: 4 }], [{ x: 1 },  { x: 2 },  { x: 3 } ])
iter.next(); // {value: {x: 2}, done: false}
iter.next(); // {value: {x: 3}, done: false}
iter.next(); // {value: undefined, done: true}

Add `split` function

Suggestion

โญ Suggestion

Splits string by separator. (not support unicode)

๐Ÿ’ป Use Cases

const iter = split(',', '1,2,3,4,5,6');
iter.next(); // 1
iter.next(); // 2
iter.next(); // 3
iter.next(); // 4
iter.next(); // 5
iter.next(); // 6
iter.next(); // undefined

Provide lazy functions first

Suggestion

โญ Suggestion

tc39 also prepares methods for lazy evaluation.
We need to develop the functions being prepared in tc39 in advance.

Additional context
fxts can be evaluated concurrently, and it is also meaningful in that it is provided as a function rather than a method.

If the above proposal(tc39) is reflected, it is likely that our library can be used with native methods. like lodash

๐Ÿ’ป Use Cases

refer
https://github.com/tc39/proposal-iterator-helpers

Add `difference` function

Suggestion

โญ Suggestion

Returns Iterable/AsyncIterable of all elements in the iterable2 not contained in the iterable1.

๐Ÿ’ป Use Cases

const iter = difference([2,1], [2,3]);
iter.next(); // {value: 3, done:false}
iter.next(); // {value: undefined, done: true}

Add `sort` function

Suggestion

โญ Suggestion

Same logic as the Array.prototype.sort function, but acceptsIterable and AsyncIterable as arguments.

๐Ÿ’ป Use Cases

const iter = [1, 2, 3, 4, 5].revers()[Symbol.iterator]();
const asyncIter = toAsync([1, 2, 3, 4, 5].revers()[Symbol.iterator]());

sort((a, b) => a - b, iter); // [1, 2, 3, 4, 5]
await sort((a, b) => a - b, asyncIter); // [1, 2, 3, 4, 5]

Add `add` function

Suggestion

โญ Suggestion

๐Ÿ’ป Use Cases

add(1, 2); // 3
await add(1, Promise.resolve(3)); // 4

add('a', 'b'); // 'ab'
await add('a', Promise.resolve('c')); // 'ac'

Suggest squash merge when merged main branch

Suggestion

Hello. If you open the main branch's command log now, you can see unnecessary merge commits mixed in.

These noise commits do not help make pretty commitments.

Why don't we force a square merge when we merge the main branch?

Image

image

Add `scan` function

Suggestion

โญ Suggestion

๐Ÿ’ป Use Cases

const iter = scan((acc, cur) => acc * cur, 1, [1, 2, 3, 4 ]);
iter.next(); // {value: 1, done:false}
iter.next(); // {value: 1, done:false}
iter.next(); // {value: 2, done:false}
iter.next(); // {value: 6, done:false}
iter.next(); // {value: 24, done:false}
iter.next(); // {value: undefined, done: true}

Add `min` and `max` functions

Suggestion

โญ Suggestion

๐Ÿ’ป Use Cases

min([5, 3, 1]); // 1;
min([5, NaN, 1]); // NaN;

max([1, 3, 5]); // 5;
max([1, NaN, 1]); // NaN;
max([1, Infinity, 1]); // Infinity;

await min(toAsync([5, 3, 1])); // 1;
await max(toAsync([1, 3, 5])); // 5;

Add `sum` function

Suggestion

โญ Suggestion

๐Ÿ’ป Use Cases

sum([1,2,3,4,5]); // 15
sum(['a', 'b', 'c']); // 'abc'

await sum(toAsync([1,2,3,4,5])); // 15
await sum(toAsync(['a', 'b', 'c'])); // 'abc'

Add `entries` function

Suggestion

โญ Suggestion

Returns an Iterable/AsyncIterable of key-value pairs for the given object

๐Ÿ’ป Use Cases

const iter = entries({a:1,b:2,c:3});
iter.next(); // {value: ['a', 1] , done:false}
iter.next(); // {value: ['b', 2] , done:false}
iter.next(); // {value: ['c', 3] , done:false}
iter.next(); // {value: undefined , done: true}

Feature ` dropWhile` `dropUntil` function

Suggestion

โญ Suggestion

  • dropWhile(Lazy):
    Returns Iterable/AsyncIterable excluding elements dropped from the beginning. Elements are dropped until the value applied to f returns falsey.

  • dropUntil(Lazy):
    Returns Iterable/AsyncIterable excluding elements dropped from the beginning. Elements are deleted until the value applied to f returns truly. (It is deleted including the value applied as true.)

๐Ÿ’ป Use Cases

const dropWhileIter = dropWhile(a => a < 5, [1,2,3,4,5,6,7,8,9,10]);
dropWhileIter.next(); // 5
dropWhileIter.next(); // 6
dropWhileIter.next(); // 7
dropWhileIter.next(); // 8
dropWhileIter.next(); // 9
dropWhileIter.next(); // 10

const dropUntilIter = dropUntil(a => a > 5, [1,2,3,4,5,6,7,8,9,10]);
dropUntilIter.next(); // 7
dropUntilIter.next(); // 8
dropUntilIter.next(); // 9
dropUntilIter.next(); // 10

Add `sortBy` function

Suggestion

โญ Suggestion

๐Ÿ’ป Use Cases

const products = [
  { id: 1, name: 'T-shirt', price: 50 },
  { id: 2, name: 'Phone case', price: 30 },
  { id: 3, name: 'Key ring', price: 15 },
];
sortBy(a => a.price, products);

Add `isEmpty` function

Suggestion

โญ Suggestion

Returns true if the given value is empty value, false otherwise.

๐Ÿ’ป Use Cases

isEmpty(1); // false
isEmpty(0); // false
isEmpty(false); // false
isEmpty(true); // false
isEmpty(new Date()); // false
isEmpty(undefined); // true
isEmpty(null); // true

isEmpty({}); // true
isEmpty({a:1}); // false

isEmpty([]); // true
isEmpty([1]); // false

isEmpty(""); // true
isEmpty("a"); // false

isEmpty(function(){}); // false
isEmpty(Symbol("")); // false

`npm run test` failed intermittently due to timeout

Bug Report

๐Ÿ’ป Code

npm run test failed intermittently due to timeout.

image

๐Ÿ™ Actual behavior

The setTimeout set in test does not match intermittently.

๐Ÿ™‚ Expected behavior

The test action should always succeed.


Version Infomation

  • Operating system:
  • Typescript:
  • Nodejs: 16.3.0

Add `pluck` function

Suggestion

โญ Suggestion

Returns Iterable/AsyncIterable by plucking the same named property off all objects in Iterable/AsyncIterable supplied.

๐Ÿ’ป Use Cases

const iter = pluck('age', [{age:21}, {age:22}, {age:23}]);
iter.next(); // 21
iter.next(); // 22
iter.next(); // 23

Show test coverage

Suggestion

โญ Suggestion

All the intended parts are meeting the tests.
By displaying coverage to users, we can make it appear more reliable.

Add `nth` function

Suggestion

โญ Suggestion

Returns the nth element of the given Iterable/AsyncIterable

๐Ÿ’ป Use Cases

nth(2, [1,2,3,4]); // 1
nth(5, [1,2,3,4]); // undefined
nth(2, ['name', 'gender', 'age']); // 'age'
nth(3, ['name', 'gender', 'age']); // undefined
nth(2, 'abcdefg'); // 'b'
nth(10, 'abcdefg'); // undefined

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.