This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type Either<L,R> = Left<L> | Right<R> | |
export class Left<L> { | |
constructor(private readonly value: L) {} | |
public getValue() { | |
return this.value | |
} | |
} | |
export class Right<R> { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo t where | |
returnLast :: Int -> t | |
class Integral i => IntsBro i where | |
toInt :: i -> Int | |
instance IntsBro Integer where | |
-- toInt :: Integer -> Int | |
toInt i = fromIntegral i :: Int |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// navigate to https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#Intersection_observer_concepts_and_usage | |
// open console and run the code below | |
const target = document.getElementById('How_intersection_is_calculated') | |
const observer = new IntersectionObserver(entries => console.log(entries[0], entries[0].isIntersecting), {root: null, threshold: 0.9}) | |
observer.observe(target) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let socket = new WebSocket('wss://echo.websocket.org') | |
async function main(socket) { | |
for await (let {value, done} of getDataFromWebSocket(socket)) { | |
if (!done) console.log('Data:', value) | |
else console.log('WebSocket is closed.') | |
} | |
return 'Done running the main fn!' | |
} // hit return/enter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function makeArrayNegativeIndexable(arr) { | |
if (!Array.isArray(arr)) return arr | |
return new Proxy(arr, { | |
get(target, prop) { | |
return target[getIndex(target.length, prop)] | |
}, | |
set(target, prop, val) { | |
let index = getIndex(target.length, prop) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// basic destructuring | |
const { key1 } = { key1: 10 } // key1 = 10 | |
const [x, y, z] = [1, 2, 3] // x = 1, y = 2, z = 3 | |
const [head, ...tail] = [1, 2, 3] // head = 1, tail = [2, 3] | |
const { a: { b } } = { a: { b: 20 } } // b = 20 | |
// storing value in a variable with a different name | |
const { key1: varForKey1 } = { key1: 20 } // varForKey1 = 20 | |
const { 'quoted-key': x } = { 'quoted-key': 10 } // x = 10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function co(genInstance) { | |
const success = v => step(genInstance, {value: v, failed: false}) | |
const failure = err => step(genInstance, {error: err, failed: true}) | |
if ('function' === typeof genInstance) genInstance = genInstance() | |
// kick start generator | |
const retVal = genInstance.next() | |
if (retVal.done) return retVal.value | |
else return retVal.value.then(success, failure) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Our goal is to create a type describing a list of events. This is our | |
-- type-level DSL. | |
-- We will then use typeclass resolution to "interpret" this type-level DSL | |
-- into two things: | |
-- 1. A comma-separated list of events | |
-- 2. A method that, when given an event name and a payload, will try to parse | |
-- that event type with the payload. A form of dynamic dispatching | |
-- | |
-- To model a list of types we will use tuples. You can imagine the list of | |
-- types "Int, String, Char" to look like: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// If multiple calls to fn are made within time t | |
// this will call fn only once after a delay of | |
// t milliseconds after the most recent call | |
function debounce1(fn, t) { | |
let timeout | |
return function (...args) { | |
let self = this | |
clearTimeout(timeout) |