Skip to content

Instantly share code, notes, and snippets.

View supasympa's full-sized avatar
🎧
💻⌨️🖥 @barclaytweets

Lewis Barclay supasympa

🎧
💻⌨️🖥 @barclaytweets
  • Supa Sympa Ltd
  • London
View GitHub Profile
@supasympa
supasympa / connect-4.js
Created May 13, 2022 22:26
Simple JavaScript Connect four
function pubSubRegistry(){
const subscriptions = [];
return {
publish: function publish(event) {
subscriptions.forEach(function(subscription) {
subscription(event);
});
},
@supasympa
supasympa / fp.ts
Last active February 9, 2022 11:01
useful functional programming snippets (in Typescript)
/*
A set of useful functional programming snippets
*/
/** pipe
* @param fns Function[] - array of functions to execute
*
*/
const pipe = (...fns: Function[]) => init => fns.reduce((acc, fn) => fn(acc), init);
@supasympa
supasympa / binary-serialisation.js
Created December 18, 2020 14:50
binary-serialisation - some thoughts for feature flags
const reg = {
foo: true,
bar: false,
qux: true,
wc_123:true,
new_feature:true
};
function dec2bin(dec){
return (dec >>> 0).toString(2);
@supasympa
supasympa / monad-example.js
Last active August 8, 2019 11:35
Monad example.
// https://hackernoon.com/functional-javascript-functors-monads-and-promises-679ce2ab8abe
const Thing = value => ({
value,
map: morphism => Thing(morphism(value)),
flatMap: morphism => morphism(value)
})
const thing1 = Thing(1) //=> Thing (1)
const thing2 = thing1.flatMap(x => Thing(x + 1)) //=> Thing (2)
@supasympa
supasympa / .hyper.js
Created May 19, 2019 09:36
My hyper config
// Future versions of Hyper may add additional config options,
// which will not automatically be merged into this file.
// See https://hyper.is#cfg for all currently supported options.
module.exports = {
config: {
// Choose either "stable" for receiving highly polished,
// or "canary" for less polished but more frequent updates
updateChannel: 'stable',
@supasympa
supasympa / useful-nvm
Created May 18, 2019 14:25
useful NVM commands to install default node and npm
nvm ls-remote
nvm install 10.15.3
nvm alias LTS v10.15.3
nvm default LTS
nvm alias default 10.15.3
nvm use 10.15.3
npx npm -g install npm@latest
npm -v
@supasympa
supasympa / deep-merge.js
Last active April 17, 2019 22:37
A cheap deep merge
const { keys } = Object;
const isObject = a => typeof a === "object" && !Array.isArray(a);
const bothObjects = (a, b) => isObject(a) && isObject(b);
const neitherObjects = (a, b) => !isObject(a) && !isObject(b);
const sameType = (a, b) => (typeof a === typeof b);
const merge = (a, b) =>
bothObjects(a, b)?
deepMerge(a, b):
@supasympa
supasympa / filter-file-paths.ts
Last active March 26, 2019 17:28
Filter file paths!
import * as assert from 'assert';
const paths = [
'src',
'src/react-dom',
'src/react-dom/src',
'src/react-dom/src/client',
'src/react-dom/src/client/ReactDOMHostConfig.js',
'packages',
'packages/react-dom',
@supasympa
supasympa / fp-functors.ts
Last active March 11, 2019 21:19
functional programming functors example
const Id = <T extends {}>(v: T) => {
return {
valueOf: () => v,
map: (fn:any) => Id(fn(v))
}
};
console.log(
Id('foo')
.map(
@supasympa
supasympa / fp-example.js
Created March 5, 2019 10:51
a simple example of functional programming / chaining
const addFive = (v) => (v + 5);
const timesTen = (v) => (v * 10);
const toObj = (v) => ({value: v});
const addFiveTimesTenObj = (v) => [addFive, timesTen, toObj].reduce((acc, item) => (item(acc)), v);
console.log(addFiveTimesTenObj(123));
console.log(addFiveTimesTenObj(13));