Skip to content

Instantly share code, notes, and snippets.

@hassam7
hassam7 / coroutine.js
Created November 10, 2020 19:09
Javascript couroutune with generators
function coroutine(g) {
const iterator = g();
return function() {
return iterator.next.apply(iterator, arguments);
}
}
const run = coroutine(function* () {
const x = 1 + (yield);
function coroutine(fn) {
const it = fn();
return function() {
return it.next.apply(it, arguments);
}
}
var getData = d => setTimeout(() => {
console.log(d);
run(d);
function* createQuoteFetcher() {
const response = yield fetch('http://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=json')
const quote = yield response.json()
return `${quote.quoteText} —${quote.quoteAuthor}`
}
const coroutine = (gen) => {
const generator = gen()
const handle = (result) => {
@hassam7
hassam7 / debounce & throttle.js
Created November 13, 2020 19:47
debounce & throttle
function debounce(fn, time) {
let timeoutId;
return function() {
// Check for existing calls
if (timeoutId) {
// Reset timer
clearTimeout(timeoutId)
}
@hassam7
hassam7 / promises_reduce.js
Created November 13, 2020 23:57 — forked from anvk/promises_reduce.js
Sequential execution of Promises using reduce()
function asyncFunc(e) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(e), e * 1000);
});
}
const arr = [1, 2, 3];
let final = [];
function workMyCollection(arr) {
// Import stylesheets
import "./style.css";
// Write Javascript code!
const appDiv = document.getElementById("app");
appDiv.innerHTML = `<h1>JS Starter</h1>`;
class CustomPromise extends Promise {
constructor(cb) {
super((resolve, reject) => {
import { fromEvent, of, Subscriber } from "rxjs"
import {
scan,
delay,
mergeMap,
switchMap,
concatMap
} from "rxjs/operators"
class MyConcatMapSubscriber extends Subscriber {
<link rel="shortcut icon" width=32px>
<canvas style="display: none" id="loader" width="16" height="16"></canvas>
<script>
class Loader {
constructor(link, canvas) {
this.link = link;
this.canvas = canvas;
this.context = canvas.getContext('2d');
this.context.lineWidth = 2;
@hassam7
hassam7 / 1.js
Created February 24, 2021 17:19 — forked from getify/1.js
Converting English number sentences ("one hundred forty two point three") to numeric digits ("142.3")
convert("one hundred five"); // "105"
convert("six hundred and fifty three"); // "653"
convert("zero zero one two three"); // "123"
convert("twelve o three"); // "1203"
convert("thirteen zero nine"); // "1309"
convert("fifteen sixteen"); // "1516"
convert("fourteen ninety two"); // "1492"
convert("nineteen ten"); // "1910"
convert("twenty twenty"); // "2020" <---- ugh!
convert("twenty twenty one"); // "2021" <---- ehhh...
@hassam7
hassam7 / leetcode-bloomberg.md
Created March 3, 2021 10:37 — forked from jayant91089/leetcode-bloomberg.md
Answers to leetcode questions tagged 'Bloomberg'

121 Best Time to Buy and Sell Stock

Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Example 1: Input: [7, 1, 5, 3, 6, 4] Output: 5