hermann-p / pragmatic-fp-ts Goto Github PK
View Code? Open in Web Editor NEWOppinionated tools for functional programming in TypeScript. Usability before mathematical rigor.
Oppinionated tools for functional programming in TypeScript. Usability before mathematical rigor.
Currently the lib exports some hundred individual small .js- and .d.ts-files. I would like to bundle them together into a single, minified .min.js file that can be used in node- and browser projects, while keeping type definitions to keep IDEs happy.
This code has problems with large arrays:
pragmatic-fp-ts/src/flatten.ts
Lines 1 to 16 in 8cfb564
Reduce with spread is an anti-pattern because it implicitly makes nested iterations -> see https://www.richsnapp.com/article/2019/06-09-reduce-spread-anti-pattern which is very bad for performance.
Also it would be an advantage to distinguish between a single flatten and a recursive flatten function, like lodash does it with flatten
and flattenDeep
?
A more performant version of flatten/flattenDeep could be that one:
const doFlattenDeep = (elems: any): any[] =>
elems instanceof Array
? elems.reduce((accum: any[], elem: any) => {
elem instanceof Array ? accum.push(...doFlattenDeep(elem)) : accum.push(elem);
return accum;
}, [])
: elems;
const doFlatten = (elems: any): any[] =>
elems instanceof Array
? elems.reduce((accum: any[], elem: any) => {
accum.push(elem);
return accum;
}, [])
: elems;
function flattenDeep<A = any>(coll: any[]): A[] {
return doFlattenDeep(l.getValueOr([], coll));
}
function flatten<A = any>(coll: any[]): A[] {
return doFlatten(l.getValueOr([], coll));
}
These are the perf results with the new functions compared against lodash and the old one (nested array with 1000 entries):
flattenDeep
is still slower than the lodash versions (but much faster than the old one), maybe because of not be an tail recursion?
Currently using monads with promises can be a bit awkward. For this reason I'd like to implement monadic futures.
Todo:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.