Skip to content

Instantly share code, notes, and snippets.

View Restuta's full-sized avatar
🦄
Hacking fast and slow.

Anton Vynogradenko Restuta

🦄
Hacking fast and slow.
View GitHub Profile
function main() {
const fn1 = a => ({
get: () => a,
set: () => {a = 'Good bye!'}
})
const a = 'hello'
const {get, set} = fn1(a)
function memoize(foo) {
let cache = {}
return args => {
if(cache[args]) {
return cache[args]
}
const executionResult = foo(args)
@Restuta
Restuta / null-or-undefined.md
Last active September 24, 2020 01:59
null or undefined

null in javascript is an object, which has few important, arguably confusing implications. Unfortunately JS has two ways to declare that something is "nothing", and the default way to do so is undefined.

Having two ways to declare that something is "nothing" might be confusing and could be unnecessary. I can't come up with a use-case where I'd need two (and Douglas C. thinks the same), therefore we should pick one or another.

Below is why I think we should pick undefined and not null:

ES5 world

We have plenty of code like this:

@Restuta
Restuta / mapValuesDeep.js
Last active August 30, 2017 01:06
mapValuesDeep with circular references detection
// based on vanilla lodash, todo: rewrite with lodash/fp
// uses isPlainObject to detect ojbects to go deep into
// detects circular references using Set()
function mapValuesDeep(originalObj, mapFunc) {
const visitedObjects = new Set()
const mapValues = (originalObj, mapFunc) =>
_.mapValues(originalObj, value => {
if (_.isPlainObject(value)) {
@Restuta
Restuta / rx-switch-map.js
Last active July 18, 2017 04:50
RxJS Switch Map
// Will and I tried to merge sorted observables with swithcMap, but we cound't
console.clear()
function main() {
// var observables = [
// Rx.Observable.of([1,2,3]),
// Rx.Observable.of([2,4,5]),
// //Rx.Observable.of([1,7,9])
// ]
@Restuta
Restuta / p-co-and-lodash-curry.js
Last active May 18, 2017 00:44
P.coroutine and _.curry issue
function asyncSum(x, y) {
return new Promise(resolve => setInterval(
() => resolve(x + y)), 10
)
}
function asyncSquare(x, y) {
return new Promise(resolve => setInterval(
() => resolve(x * y)), 10
)
@Restuta
Restuta / progress.js
Last active May 5, 2017 22:21
Simple Progress Reporter for JavaScript
// gets time now in milliseconds
const msNow = () => (+new Date())
function createProgress ({
maxItems = 100,
onProgressChange = () => {},
onComplete = () => {}
}) {
let currentProgress = 0 // %
let lastProgress = 0
@Restuta
Restuta / gamblers-fallacy.js
Last active June 17, 2017 11:43
Gamblers Fallacy
/*
Models flip-a-coin game with variative chance of "heads" and "tails". Chance depends on betWinPayoutRatio.
For 2 it's 50% like you wold expect and for 10 it's 1/10th
Once you copy paste this code to Chrome's console, run
main({
gamesToPlay: 100,
timesToPlayEachGame: 1000000,
chainLenghtToWaitFor: 5,
@Restuta
Restuta / find-circles-in-permutations.js
Last active June 17, 2017 11:43
Find Circles in Permutations of Unique Numbers (for 100 prisoners problem)
console.clear();
function shuffle(origArray) {
var array = origArray.slice(0)
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (currentIndex !== 0) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
@Restuta
Restuta / .block
Last active May 9, 2018 05:27
V4 simple network graph
license: mit