about summary refs log tree commit diff stats
path: root/js
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2024-08-20 09:28:10 -0400
committerelioat <elioat@tilde.institute>2024-08-20 09:28:10 -0400
commita33c1c701250c160cb657dedb14187156c719067 (patch)
tree9ff74b649ae8c5c29382e70f86c7e6645002540a /js
parente78bf7a21ad8dc2971e061eb01fd335739997c31 (diff)
downloadtour-a33c1c701250c160cb657dedb14187156c719067.tar.gz
*
Diffstat (limited to 'js')
-rw-r--r--js/b.js100
1 files changed, 84 insertions, 16 deletions
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();