Skip to content

Instantly share code, notes, and snippets.

@paulroth3d
Last active March 31, 2024 19:44
Show Gist options
  • Save paulroth3d/fc97580636ba706783c6a84467a625db to your computer and use it in GitHub Desktop.
Save paulroth3d/fc97580636ba706783c6a84467a625db to your computer and use it in GitHub Desktop.
Simple chain monad

Overview

Creates a very simple monad, so you can give it a value - continually modifying it as you'd like, and then get a value out of it.

Example

addTwo = (value) => value + 2;
addTwo(3); // 5

console.log(
 utils.chain(3)
   .chain(addTwo) // (3 + 2)
   .chain(addTwo) // (5 + 2)
   .debug() // consoles 7 and passes the value along
   // define a function inline
   .chain((value) => value + 3) // (7 + 3)
   .close()
);

// 7
// 10

available as a bookmarklet:

  • save a bookmark
  • edit and replace the url with the following:
javascript:void%20function(){function%20chain(r){return{value:r,chain:function(n){return%20chain(n(r))},close:function(){return%20r},toArray:function(){return%20chain(Array.from(r))},chainFilter:function(n){if(!Array.isArray(r))throw%20Error(`chainFilter%20expected%20an%20array,%20but%20was%20passed:${r}`);return%20chain(r.filter(n))},chainMap:function(n){if(!Array.isArray(r))throw%20Error(`chainMap%20expected%20an%20array,%20but%20was%20passed:${r}`);return%20chain(r.map(n))},chainReduce:function(n,a){if(!Array.isArray(r))throw%20Error(`chainReduce%20expected%20an%20array,%20but%20was%20passed:${r}`);return%20chain(r.reduce(n,a))},selectAt:function(n){if(!Array.isArray(r))throw%20Error(`selectAt%20expected%20an%20array,%20but%20was%20passed:${r}`);return%20chain(r[n])},replace:function(r){return%20chain(r)},exec:function(n){return%20n(r),chain(r)},debug:function(n){return%20n%3Fn(r):console.log(r),chain(r)}}}window.chain=chain}();
function chain(value) {
return ({
value,
chain: function innerChain(fn) {
return chain(fn(value));
},
close: function close() {
return value;
},
toArray: function toArray() {
return chain(Array.from(value));
},
chainFilter: function chainFilter(fn) {
if (!Array.isArray(value)) throw Error(`chainFilter expected an array, but was passed:${value}`);
return chain(value.filter(fn));
},
chainMap: function chainMap(fn) {
if (!Array.isArray(value)) throw Error(`chainMap expected an array, but was passed:${value}`);
return chain(value.map(fn));
},
chainReduce: function chainReduce(fn, initialValue) {
if (!Array.isArray(value)) throw Error(`chainReduce expected an array, but was passed:${value}`);
return chain(value.reduce(fn, initialValue));
},
selectAt: function selectAt(index) {
if (!Array.isArray(value)) throw Error(`selectAt expected an array, but was passed:${value}`);
return chain(value[index]);
},
replace: function replace(val) {
return chain(val);
},
exec: function exec(fn) {
fn(value);
return chain(value);
},
debug: function debug(fn) {
if (fn) {
fn(value);
} else {
console.log(value);
}
return chain(value);
}
});
};
window.chain = chain;

Short Way

function bookmarkify(str) {
    const cleanStr = encodeURIComponent(str.replaceAll(/[ \t]*\n[ \t\n]+/g, ''));
    const newStr = `(()=>{${cleanStr}})()`;
    return `javascript:${cleanStr}`;
}

Long Way

  1. Minfy chain like this:

https://minify-js.com/

(() => {
function chain(value) {
    ...
};
window.chain = chain;
console.log(`window.chain added`);
})();
  1. urlencodeComponent

https://www.urlencoder.org/

  1. javascript:${RESULT]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment