Skip to content

Instantly share code, notes, and snippets.

View babakness's full-sized avatar

Babak B. babakness

View GitHub Profile
@lalkmim
lalkmim / codility_solutions.txt
Last active March 21, 2024 10:34
Codility Solutions in JavaScript
Lesson 1 - Iterations
- BinaryGap - https://codility.com/demo/results/trainingU2FQPQ-7Y4/
Lesson 2 - Arrays
- OddOccurrencesInArray - https://codility.com/demo/results/trainingFN5RVT-XQ4/
- CyclicRotation - https://codility.com/demo/results/trainingSH2W5R-RP5/
Lesson 3 - Time Complexity
- FrogJmp - https://codility.com/demo/results/training6KKWUD-BXJ/
- PermMissingElem - https://codility.com/demo/results/training58W4YJ-VHA/
@Avaq
Avaq / combinators.js
Last active March 18, 2024 20:49
Common combinators in JavaScript
const I = x => x
const K = x => y => x
const A = f => x => f (x)
const T = x => f => f (x)
const W = f => x => f (x) (x)
const C = f => y => x => f (x) (y)
const B = f => g => x => f (g (x))
const S = f => g => x => f (x) (g (x))
const S_ = f => g => x => f (g (x)) (x)
const S2 = f => g => h => x => f (g (x)) (h (x))
@jliuhtonen
jliuhtonen / Reader.elm
Created April 13, 2016 17:53
Reader monad in Elm for fun and profit
module Reader where
type Reader ctx a = Reader (ctx -> a)
unit: a -> Reader any a
unit x =
Reader (\_ -> x)
@mjackson
mjackson / resolvePromise.js
Last active September 9, 2018 08:23
An easy way to do async APIs in JavaScript that support both promises *and* callbacks!
// Here is a function that I use all the time when creating public
// async APIs in JavaScript:
const resolvePromise = (promise, callback) => {
if (callback)
promise.then(value => callback(null, value), callback)
return promise
}
// Sometimes I like to use callbacks, but other times a promise is
@odigity
odigity / Firebase Database API Cheatsheet
Last active June 9, 2022 03:45
Firebase Database API Cheatsheet
There is no way to store an empty object/array/null value.
There are also no actual arrays. Array values get stored as objects with integer keys.
(If all keys are integers, it will be returned as an array.)
Basically, it's one giant tree of hashes with string keys.
Simply write a value to any location, and the intermediary locations will automatically come into existance.
── Classes ──
DataSnapshot : Container for a subtree of data at a particular location.
@nybblr
nybblr / 1-easy.js
Last active July 13, 2022 03:40
3 examples of using Async Generators and Async Iteration in JavaScript!
// Create a Promise that resolves after ms time
var timer = function(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
};
// Repeatedly generate a number starting
// from 0 after a random amount of time
var source = async function*() {
@pboyer
pboyer / dynamicArray.ts
Created February 12, 2017 18:46
A generic, dynamically sized TypedArray in TypeScript
interface TypedArray {
readonly length : number;
readonly [n: number]: number;
BYTES_PER_ELEMENT : number;
set(n : ArrayLike<number>, offset : number) : void;
}
interface TypedArrayConstructor<T extends TypedArray> {
new (buffer: ArrayBuffer, byteOffset?: number, length?: number): T;
}
@KiaraGrouwstra
KiaraGrouwstra / stdlib.ts
Last active March 24, 2020 03:17
Type-level standard library for TypeScript
// NOTE: code now moved to https://github.com/tycho01/typical
// older revision left here, but it no longer runs well in TS Playground
export type Obj<T> = { [k: string]: T };
export type NumObj<T> = { [k: number]: T };
// export type List = ArrayLike; // no unapplied generic types :(
export type List<T> = ArrayLike<T>;
// progress: https://github.com/Microsoft/TypeScript/issues/16392
export function force<T, V extends T>() {}

Everything I Know About UI Routing

Definitions

  1. Location - The location of the application. Usually just a URL, but the location can contain multiple pieces of information that can be used by an app
    1. pathname - The "file/directory" portion of the URL, like invoices/123
    2. search - The stuff after ? in a URL like /assignments?showGrades=1.
    3. query - A parsed version of search, usually an object but not a standard browser feature.
    4. hash - The # portion of the URL. This is not available to servers in request.url so its client only. By default it means which part of the page the user should be scrolled to, but developers use it for various things.
    5. state - Object associated with a location. Think of it like a hidden URL query. It's state you want to keep with a specific location, but you don't want it to be visible in the URL.
@vincentriemer
vincentriemer / usePersistedState.js
Created August 6, 2019 15:06
My take on a type-safe usePersistedState hook, leveraging React suspense/concurrent-mode, DOM custom events, and the kv-storage builtin module.
/**
* @flow
*/
import * as React from "react";
import { unstable_createResource } from "react-cache";
import * as Scheduler from "scheduler";
import { StorageArea } from "std:kv-storage";
// Creates a ref that tracks the latest value of the argument passed to it