Skip to content

Instantly share code, notes, and snippets.

View ourmaninamsterdam's full-sized avatar

Justin Perry ourmaninamsterdam

View GitHub Profile
@ourmaninamsterdam
ourmaninamsterdam / abstract.md
Last active January 16, 2021 11:32
Abstract list operations and signatures - racket-lang

Natural -> (listof X) - build-list

(listof X) -> (listof X) - filter

(listof X) -> (listof Y) - map

(listof X) -> Boolean - andmap

(listof X) -> Boolean - ormap

(define (find name dir)
(local [(define (c1 n rdirs rimgs)
(or (string=? name n)
rdirs
rimgs))
(define (c2 rdir rdirs)
(or rdir rdirs))
(define (c3 img rimgs)
false)]
(fold-dir c1 c2 c3 false false dir)))
(define-struct node (k v l r))
;; BinaryTree is one of:
;; - false
;; - (make-node Natural String BinaryTree BinaryTree)
(define BT0 false)
(define BT1 (make-node 1 "a" false false))
(define BT4 (make-node 4 "d"
(make-node 2 "b"
(make-node 1 "a" false false)
(make-node 3 "c" false false))
@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 / wait.ts
Last active November 19, 2020 09:55
For pausing execution while debugging
const wait = async (ms: number) =>
new Promise((resolve) => {
setTimeout(() => resolve(), ms);
});
await wait(10000); // wait for 10 seconds
// Continue...
@ourmaninamsterdam
ourmaninamsterdam / useDeviceOnlineStatus.ts
Last active November 19, 2020 09:54
useDeviceOnline wrapper hook for React Native's NetInfo
import NetInfo, { NetInfoState, NetInfoSubscription } from '@react-native-community/netinfo';
import { useState, useRef, useEffect } from 'react';
const useDeviceOnlineStatus = () => {
const [online, setOnline] = useState(false);
const unsubscribeRef = useRef<NetInfoSubscription | undefined>();
useEffect(() => {
unsubscribeRef.current = NetInfo.addEventListener((state: NetInfoState) => {
setOnline(state.isConnected);
@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)))]))