From a33c1c701250c160cb657dedb14187156c719067 Mon Sep 17 00:00:00 2001 From: elioat Date: Tue, 20 Aug 2024 09:28:10 -0400 Subject: * --- js/b.js | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 84 insertions(+), 16 deletions(-) (limited to 'js') diff --git a/js/b.js b/js/b.js index 7e22201..459c79f 100644 --- a/js/b.js +++ b/js/b.js @@ -22,28 +22,96 @@ // b is for useful stuff +// pipe: Runs functions from left to right. +// compose: Runs functions from right to left. +// identity: Returns whatever you give it as is. +// curry: Converts a function to its curried form. +// match: Creates a curried function to match a regular expression in a string. +// replace: Creates a curried function to replace parts of a string. +// filter: Curried array filtering. +// map: Curried array mapping. +// deepMap: Curried recursive mapping for nested arrays or matrices. + 'use strict' const b = { - pipe: (...args) => args.reduce((acc, el) => el(acc)), - compose: (...fns) => (...args) => fns.reduceRight((res, fn) => [fn.call(null, ...res)], args)[0], - identity: x => x, - curry: (fn) => { - const curried = (...args) => { - if (args.length >= fn.length) - return fn(...args) - else - return (...rest) => curried(...args, ...rest) - } - return curried - }, - match: () => b.curry((what, s) => s.match(what)), - replace: () => b.curry((what, replacement, s) => s.replace(what, replacement)), - filter: () => b.curry((f, xs) => xs.filter(f)), - map: () => b.curry((f, xs) => xs.map(f)) + /** + * Composes functions from left to right. + * Takes any number of functions as arguments and returns the result of applying them in sequence. + * @param {...Function} args - The functions to be composed. + * @returns {Function} - A function that applies the composed functions from left to right. + */ + pipe: (...args) => args.reduce((acc, el) => el(acc)), + + /** + * Composes functions from right to left. + * Takes any number of functions as arguments and returns a function that applies them in sequence from right to left. + * @param {...Function} fns - The functions to be composed. + * @returns {Function} - A function that applies the composed functions from right to left. + */ + compose: (...fns) => (...args) => fns.reduceRight((res, fn) => [fn.call(null, ...res)], args)[0], + + /** + * A function that returns its input unmodified. + * @param {*} x - The input value. + * @returns {*} - The same input value. + */ + identity: x => x, + + /** + * Converts a function into a curried function. + * A curried function can be partially applied and will return a new function until all arguments are provided. + * @param {Function} fn - The function to curry. + * @returns {Function} - The curried function. + */ + curry: (fn) => { + const curried = (...args) => { + if (args.length >= fn.length) + return fn(...args) + else + return (...rest) => curried(...args, ...rest) + } + return curried + }, + + /** + * Curried function that matches a regular expression against a string. + * @returns {Function} - A curried function that takes a regex and a string and returns the match result. + */ + match: () => b.curry((what, s) => s.match(what)), + + /** + * Curried function that replaces parts of a string based on a regex or substring. + * @returns {Function} - A curried function that takes a regex or substring, a replacement, and a string, and returns the replaced string. + */ + replace: () => b.curry((what, replacement, s) => s.replace(what, replacement)), + + /** + * Curried function that filters an array based on a predicate function. + * @returns {Function} - A curried function that takes a predicate function and an array, and returns the filtered array. + */ + filter: () => b.curry((f, xs) => xs.filter(f)), + + /** + * Curried function that maps a function over an array. + * @returns {Function} - A curried function that takes a mapping function and an array, and returns the mapped array. + */ + map: () => b.curry((f, xs) => xs.map(f)), + + /** + * Curried function that recursively maps a function over an array or matrix of any depth. + * @returns {Function} - A curried function that takes a mapping function and a nested array, and returns the deeply mapped array. + */ + deepMap: () => + b.curry(function deepMap(f, xs) { + return Array.isArray(xs) + ? xs.map((x) => deepMap(f, x)) + : f(xs); + }), }; + // // pipe // const title = '10 Weird Facts About Dogs'; // const toLowerCase = (str) => str.toLowerCase(); -- cgit 1.4.1-2-gfad0 n24' href='#n24'>24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79