Skip to content

Instantly share code, notes, and snippets.

View alexeden's full-sized avatar

Alex Eden alexeden

View GitHub Profile
@alexeden
alexeden / 001-union-types.md
Last active November 19, 2024 17:29
Pattern 001: Leverage Typescript union types (part 1)

Pattern 001: Leverage Typescript union types (part 1)

Leverage what?

Union types. They enable cool things and can eliminate an entire class of bugs that would only otherwise be discovered at runtime.

They look like this:

type Key = string | number | symbol;
@alexeden
alexeden / watch-keys.operator.ts
Created July 22, 2019 22:44
RxJS watchKeys operator for tracking added, removed, and persisted items within an object
import { Observable, Subscriber } from 'rxjs';
type Dictionary<T> = {
[key: string]: T;
};
export type AddedEvent<T> = {
type: 'added';
value: T;
};
@alexeden
alexeden / tag.operator.ts
Created July 12, 2019 18:07
Tag RxJS Operator
import { Observable, Subscriber } from 'rxjs';
const nextCss = `background-color: cyan; color: #000000; font-weight: bold;`;
const errorCss = `background-color: #f64040; color: #ffffff; font-weight: bold;`;
const completeCss = `background-color: rgb(24, 255, 148); color: #000000; font-weight: bold;`;
export const tag = (tagText: string, stringify = false) => {
return <T>(source: Observable<T>) =>
new Observable((observer: Subscriber<T>) => {
let count = 0;
@alexeden
alexeden / math.utils.ts
Created July 11, 2019 23:52
Math Utility Functions
import { curry } from 'ramda';
export const range = (from: number, to: number) => {
const result = [];
let n = from - 1;
while (++n < to) result.push(n);
return result;
};
export const clamp = curry((min: number, max: number, value: number) => {
@alexeden
alexeden / until-destroyed.operator.ts
Created July 11, 2019 22:28
untilDestroyed Angular Component Operator
import { Observable, Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
export const untilDestroyed = <C extends any>(
componentInstance: C,
destroyMethodName = 'ngOnDestroy'
) => <T>(source: Observable<T>) => {
const originalDestroy = componentInstance[destroyMethodName];
if (typeof originalDestroy !== 'function') {
throw new Error(
@alexeden
alexeden / simple-search-engine.md
Last active May 1, 2019 17:42
Building a Search Engine

Simple Search Engine

You provide:

  • Data (data) as an array of items (item)
  • Key generator function (keygen): A pure function that, given an item from data, returns a deterministic key as a string.
  • Tokenizer (tokenizer): A pure function that, given an item from data, returns a list of tokens (strings) describing that item.

What the search engine does when created:

@alexeden
alexeden / data-transformation-notes.md
Last active April 17, 2019 17:41
Notes for the Data Transformation problems

Data transformations

Essential Array Methods

These are the JS Array methods that should be your go-to tools for everything. They're the building blocks of some extremely powerful combinations of algorithmic logic.

Prototype methods (methods called on an instance of an Array)

  • [].reduce: The king of the array methods; nearly all array methods can be derived from reduce. Learn it and get really good at using it.
  • [].map
@alexeden
alexeden / finish-the-functions-answers.md
Last active April 17, 2019 18:01
Answers to Finish the Function data transformation problems

Finish the Function: Data transformations

Sum of Numbers

const sum = (...nums) => nums.reduce((accum, x) => accum + x, 0);

Combine Strings

@alexeden
alexeden / finish-the-functions.md
Last active September 5, 2019 18:16
Finish the Functions: Data transformation practice problems

Finish the Function: Data transformations

Problem Rules

The problems below all require that you write a function that performs a simple data transformation on a generic, opaque set of data.

Note that many functions are variadic by way of the rest operator. If you're not sure what this means, read this.

These are the rules:

@alexeden
alexeden / .clang-format
Created March 28, 2019 01:31
clang-format Config
---
Language: Cpp
BasedOnStyle: Mozilla
AccessModifierOffset: -2
AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignTrailingComments: true
AlignOperands: true