Skip to content

Instantly share code, notes, and snippets.

View erodactyl's full-sized avatar
🎯
Focusing

Erik Davtyan erodactyl

🎯
Focusing
View GitHub Profile
@erodactyl
erodactyl / arc.cpp
Created November 25, 2017 13:13
Automatic reference counting
#include <iostream>
using namespace std;
template <class T>
class Smart {
private:
T* pointee;
int* ref_count;
public:
Smart(T* ptr) : pointee(ptr), ref_count(new int(0)) {
@erodactyl
erodactyl / reactCreateContext.js
Last active February 25, 2019 11:22
Context API implementation
import React from 'react';
const createContext = (defaultState = undefined) => {
let state = defaultState;
let subscribers = [];
const subscribe = cb => {
// We call this function ourselves so no need to check the typeof cb === 'function'
subscribers.push(cb);
return () => {
subscribers = subscribers.filter(c => c !== cb);
namespace List {
const EmptyList = new TypeError('The list is empty');
const IndexOutOfBounds = new RangeError('Index out of bounds');
type Empty = '@@EMPTY';
export const EMPTY: Empty = '@@EMPTY';
export type IList<T> = Empty | readonly [T, IList<T>];
export const head = <T>(list: IList<T>): T => {
import { NotLoggedIn } from '../models/errors'
const AuthGuardMethods = Symbol('AuthGuardMethods')
export function AuthGuard() {
return function(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
target[AuthGuardMethods] = target[AuthGuardMethods] || new Map()
// Here we just add some information that class decorator will use
target[AuthGuardMethods].set(propertyKey, descriptor)
}
@erodactyl
erodactyl / deeply_nested_flatten_unflatten.ts
Last active August 6, 2021 08:34
Helper functions for deeply nested data structures. Can be used with GraphQL queries that have to return arbitrarily deep trees. "flatten" is called in the backend resolver to traverse the tree into an array, "unflatten" is called in the frontend resolver to bring it to original shape. The shape has a key "id" and an arbitrary key for nested chi…
type IFlattenable<K extends string> = {
[k in K]?: IFlattenable<K>[];
} & { id: number };
type IFlattened<T extends IFlattenable<K>, K extends string> = Omit<
T,
K | 'id'
> & {
parentId: number;
id: number;
import { useEffect, useReducer, useMemo } from "react";
import { BehaviorSubject } from "rxjs";
export const createStateHook = <S>(init: S) => {
const state$ = new BehaviorSubject(init);
const setState = (change: S | ((state: S) => S)) => {
state$.next(change instanceof Function ? change(state$.value) : change);
};
@erodactyl
erodactyl / createStore.ts
Last active October 3, 2021 16:30
State management solution for easily and safely handling complex async state like Promises or eventListeners.
import { useCallback, useEffect, useReducer, useRef } from "react";
type Comparator = (value1: any, value2: any) => boolean;
/** Core */
type Listener = () => void;
type Destroy = () => void;
@erodactyl
erodactyl / mini-rxjs.ts
Created October 10, 2021 20:22
Implementation of basic rxjs observable and a few operators.
interface Observer<T> {
next: (el: T) => void;
}
type Dispose = () => void;
type Operator<T, U> = (source$: Observable<T>) => Observable<U>
class Observable<T> {
constructor(private cb: (observer: Observer<T>) => Dispose) {}
@erodactyl
erodactyl / maybe.ts
Last active July 11, 2022 06:06
Maybe monad implemented functionally in typescript
class None {}
class Some<T> {
constructor(public value: T) {}
}
type Maybe<T> = None | Some<T>;
export const none = new None();
@erodactyl
erodactyl / sonicpi.ts
Created August 18, 2022 13:51
Sonic Pi in Typescript
import { default as player } from "play-sound";
const play = (name: string) => player().play(`./assets/${name}.wav`);
const sleep = (time: number) => {
return new Promise((res) =>
setTimeout(() => {
res(null);
}, time)
);
};