Skip to content

Instantly share code, notes, and snippets.

View ourmaninamsterdam's full-sized avatar

Justin Perry ourmaninamsterdam

View GitHub Profile
@ourmaninamsterdam
ourmaninamsterdam / sumAllKeys.ts
Last active October 10, 2020 12:44
Sum all keys from a BST
type BST = {
key: number;
value: string;
left: BST | false;
right: BST | false;
} | false;
const BST0: BST = false;
const BST1: BST = {
key: 1,
@ourmaninamsterdam
ourmaninamsterdam / useLocationProviderStatus.ts
Last active October 10, 2020 11:52
Reports LocationProviderStatus (GPS/network or passive location Android only) from expo-location
import { getProviderStatusAsync, LocationProviderStatus } from 'expo-location';
import { useState } from 'react';
import { Platform } from 'react-native';
import useInterval from 'use-interval';
const useLocationProviderStatus = (delay: number = 2000) => {
const [locationProviderAvailable, setLocationProviderAvailable] = useState<boolean | undefined>(undefined);
const getLocationProviderStatus = ({
gpsAvailable,
@ourmaninamsterdam
ourmaninamsterdam / doubleList.ts
Created September 18, 2020 10:59
Double number in list (recursive)
function doubleList(listOfNumbers: number[]): number[] {
if(!listOfNumbers.length) return [];
return [
double(listOfNumbers[0]),
...doubleList(listOfNumbers.slice(1, listOfNumbers.length))
];
}
function double(n: number) {
@ourmaninamsterdam
ourmaninamsterdam / fact.rkt
Created June 21, 2020 17:29
Simple factorial fn in Racket
(check-expect (fact 0) 1)
(check-expect (fact 3) 6)
(define (fact n)
(cond [(zero? n) 1]
[else
(* n
(fact (sub1 n)))]))
@ourmaninamsterdam
ourmaninamsterdam / enumerators.js
Created November 1, 2019 14:57
As Enumerables
const asEnumeration = dictionary => Object.freeze({
from(value) {
if (dictionary[value]) {
return dictionary[value];
}
throw Error(`Invalid enumeration value ${value}`);
}
});
const getEvenNumbers = limit => includeIfConditionIsMet(limit, number => number % 2 === 0);
@ourmaninamsterdam
ourmaninamsterdam / generateIncrementedNumbersList.js
Last active November 1, 2019 14:55
Generate incremented numbers
const generateIncrementedNumbersList = (min, max, increment, includeMin) => {
const items = [];
let incrementor = Math.max(min, increment);
if (includeMin) {
items.push(min);
}
while (incrementor <= max) {
items.push(incrementor);
const add = (a, b) => a + b;
const add10 = num => add(num, 10);
const add200 = num => add(num, 200);
const addNew = a => b => a + b;
console.log(add(10, 20)) // 30
console.log(add10(10)) // 20
console.log(addNew(10)(20)) // 30
console.log(add200(10)) // 210
@ourmaninamsterdam
ourmaninamsterdam / rxRangeSlider.js
Last active October 17, 2019 20:31
RxRangeSlider - first play with RxJS
const rxRangeSlider = ({ rangeNode, handlesContainerNode, handleStartNode, handleEndNode, start: [stepStart, stepEnd], stepSize, bounds: [rangeStart, rangeEnd] }) => {
const KEY_LEFT = 37;
const KEY_RIGHT = 39;
const VALID_KEYS = [KEY_LEFT, KEY_RIGHT];
const INCREMENT_LOWER = 'INCREMENT_LOWER';
const DECREMENT_LOWER = 'DECREMENT_LOWER';
const INCREMENT_UPPER = 'INCREMENT_UPPER';
const DECREMENT_UPPER = 'DECREMENT_UPPER';
const restrictToRange = (min, max, num) => Math.min(Math.max(min, num), max);
@ourmaninamsterdam
ourmaninamsterdam / omit.js
Created October 16, 2019 14:50
Omit object property
const omit = (object, key) => {
const objectCopy = Object.assign({}, object);
Object.keys(objectCopy).forEach(itemKey => {
if(key === itemKey) {
delete objectCopy[itemKey];
}
})
return objectCopy;
};
@ourmaninamsterdam
ourmaninamsterdam / rxArrayStream.js
Last active October 16, 2019 14:08
RxJS quick example of switching between array and stream
Rx.Observable.range(1, 10)
.toArray()
.mergeMap(items => Rx.Observable.from(items))
.map(a => a + a)
.subscribe({
next(items) {
console.log(items);
}