xerocross.fu
v3.0.1
Published
This is a collection of JavaScript utility functions written in a functional programming style. I just started this. It's still under construction. But what _is_ here is thoroughly tested. The code even contains mathematical proofs of correctness and
Downloads
7
Readme
xerocross.fu
This is a collection of JavaScript utility functions written in a functional programming style. I just started this. It's still under construction. But what is here is thoroughly tested. The code even contains mathematical proofs of correctness and internal verification that will throw errors at runtime if there is an unexpected problem.
It stands to reason that these functions might be noticeably slower than functions written in the traditional JavaScript imperative style. The (possible) lack of speed is a tradeoff for the benefit of functions that can be validated by mathematical proof.
about functional programming in JavaScript
JavaScript interpreters (as of this writing) tend to have a rather small stack limit for function recursion. This is an immovable object, and it forces me to write some functions in a style that is not fully functional. An important example here is number.isNaturalNumber
. We cannot recurse down from 35802 to check that this is a natural number by recursion. Instead, I included a small while loop.
author
This is a one-man project so far. I'm the author, Adam Cross, AKA Xerocross. I'm an experienced software engineer and a PhD mathematician.
updates
I don't maintain any project-specific update blog, but I do maintain a blog about all of my programming activities at https://adamcross.blog/. If you are interested in the dev process or any updates about this project, check the blog.
validation and testing
This library has a rather extensive testing suite written in Jest notation. It also uses WeAssert (https://github.com/xerocross/we-assert) for internal validation of data. For example, this library defines a number.isInteger function and many of the other functions use that function internally to verify input data. These functions will throw an error at runtime if the input type is invalid.
npm run test
tests the source files
functions
This is a list of the available functions and their signatures and return types. To use them, import the module. const futil = require("xerocross.fu");
. Then to access a function, say "array.joinRight", you call futil.array.joinRight
.
None of these functions mutate the input data.
function list
Here is the declaration of the exported types and methods
declare type ComparisonFunction = (i: number, j: number) => number;
declare type IsEqualFunction = (left: any, right: any) => boolean;
declare const _default: {
array: {
clone: (arr: any[]) => any[];
isArraysEqual: (arr1: any[], arr2: any[], isEqual: IsEqualFunction) => boolean;
isSorted: (arr: any[], upTo: number, compareFunction: ComparisonFunction) => boolean;
joinRight: (arr: any[], newValue: any) => any[];
joinLeft: (arr: any[], newValue: any) => any[];
subarrayMax: (arr: any[], max: number) => any[];
subarrayMin: (arr: any[], min: number) => any[];
joinTwoArrays: (arr1: any[], arr2: any[]) => any[];
subarray: (arr: any[], min: number, max: number) => any[];
replace: (arr: any[], index: number, value: any) => any[];
swap: (arr: any[], i: number, j: number) => any[];
bubbleUp: (arr: any[], bubbleIndex: number, compareFunction: ComparisonFunction) => any[];
bubbleSort: (arr: any[], compareFunction: ComparisonFunction) => any[];
};
number: {
isNaturalNumber: (num: number) => boolean;
isInteger: (num: number) => boolean;
multiply: (arr: number[]) => number;
getFirstFactor: (num: number) => number;
getPrimeFactorsRecursion: (num: number, knownFactors: number[]) => number[];
getPrimeFactors: (num: number) => number[];
};
};