Skip to content

Instantly share code, notes, and snippets.

View shawn-dsz's full-sized avatar
🎯
Solving Problems beautifully

Shawn D'Souza shawn-dsz

🎯
Solving Problems beautifully
View GitHub Profile
@shawn-dsz
shawn-dsz / memo-fib.js
Created December 16, 2021 12:37
Learn to write Memoize func
const memoize = (func) => {
const results = {};
return (...args) => {
const argsKey = JSON.stringify(args);
if (!results[argsKey]) {
results[argsKey] = func(...args);
}
return results[argsKey];
};
};
@shawn-dsz
shawn-dsz / functional-js-traversing-trees-with-recursive-reduce.ts
Created December 16, 2021 11:29
functional-js-traversing-trees-with-recursive-reduce
// https://jrsinclair.com/articles/2019/functional-js-traversing-trees-with-recursive-reduce/
const menu1 = [
{
type: 'title',
text: 'Works of George Macdonald',
},
{
type: 'link',
href: '/books',
@shawn-dsz
shawn-dsz / await-forloop.ts
Created December 16, 2021 11:19
await loop
// https://zellwk.com/blog/async-await-in-loops/
const fruitBasket = {
apple: 27,
grape: 0,
pear: 14,
};
const sleep = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms));
@shawn-dsz
shawn-dsz / merge-array.ts
Created December 16, 2021 10:50
Merge two arrays
const assert = (val: boolean) => {
if (!val) {
throw new Error('false');
}
return true;
};
const mergeArray = <T>(key: keyof T, ...arrays: T[][]): T[] => {
return Object.values(
arrays.flat().reduce((acc, curr) => {
We couldn’t find that file to show.
const total = (arr) => arr.reduce((acc, v) => acc + v);
console.log(total([1, 2, 3])); // 6
/**====================================================================== */
const stringConcat = (arr) => arr.reduce((acc, v) => `${acc}${v}`);
console.log(stringConcat([1, 2, 3])); // "123"
/**====================================================================== */
const totalVotes = (arr) => {
return arr.reduce((acc, curr) => (curr.voted ? acc + 1 : acc), 0);
@shawn-dsz
shawn-dsz / pub-sub
Created December 13, 2021 08:31
Publish subscriber
function pubSub() {
const subscribers = {};
function publish(eventName, data) {
if (!Array.isArray(subscribers[eventName])) {
return;
}
subscribers[eventName].forEach((callback) => {
callback(data);
});
@shawn-dsz
shawn-dsz / RenderToStream.js
Created December 1, 2021 01:22
Using Render to Stream
const ReactDOMServer = require('react-dom/server');
const render = (reactComponent) => {
return new Promise((resolve, reject) => {
const body = [];
const bodyStream = ReactDOMServer.renderToNodeStream(reactComponent);
bodyStream.on('data', (chunk) => {
body.push(chunk.toString());
});
bodyStream.on('error', (err) => {
@shawn-dsz
shawn-dsz / arrays.js
Created November 29, 2021 23:25
array functions
const numbers = [10, 20, 30, 40];
const numbersTimes10 = numbers.map((v) => v * 10);
numbersTimes10;
const people = [
{
first: 'Jane',
last: 'Doe',
address: '123 Main St',
city: 'New York',
@shawn-dsz
shawn-dsz / engiineering-culture.md
Last active November 4, 2021 00:30
Factors that influence Engineering Culture

Outcome

  • Identify improvement areas
  • Publish your culture - Having a document that describes our culture our values

3 Areas

  • Impact
  • Choice
  • Improvement