Skip to content

Instantly share code, notes, and snippets.

View joshwashywash's full-sized avatar
🤡

Josh Oertel joshwashywash

🤡
View GitHub Profile
@joshwashywash
joshwashywash / context.md
Last active November 6, 2023 16:21
typesafe contexts in svelte (maybe typos but you get the idea...)
// contexts/index.ts
import { getContext, hasContext, setContext } from 'svelte';

export const create = <Context>(identifier: string) => {
	const key = Symbol();
	return {
		set(context: Context) {
			return setContext<Context>(key, context);
		},
@joshwashywash
joshwashywash / circular.md
Last active September 16, 2023 05:27
circular interpolation
type Point = {
  x: number;
  y: number;
};

const interpolate = (center: Point) => (amount: number) => (pt: Point) => {
  return (n: number) => {
    const angle = n * amount;
 const c = Math.cos(angle);
@joshwashywash
joshwashywash / conditionally.md
Created August 25, 2023 17:05
conditionally readable in svelte
import { derived } from 'svelte/store';
import type { Readable } from 'svelte/store';

export const conditionally =
	(readable: Readable<boolean>) =>
	<T>(whenTrue: T, whenFalse: T): Readable<T> =>
		derived(readable, ($readable): T => {
			return $readable ? whenTrue : whenFalse;
		});
@joshwashywash
joshwashywash / faq.md
Last active June 18, 2023 04:29
faq.md

Why don't you acknowledge new followers by name or have an alert?

I appreciate anyone that decides to support the stream but some people don't want their presence to be broadcasted.

What music is playing?

I usually just find a playlist or collection of video game music or an OST to put on in the background.

How long have you been programming/coding overall.

@joshwashywash
joshwashywash / mapRange.md
Last active March 31, 2023 23:18
map from one range to another

This gist isn't mathematically rigorous so don't expect it to be as formal as something you'd find in academia.

how do you map from one range of numbers to another?

This problem arises in various math and computer science topics where your input range needs to be transformed to fit into a different range. For example, let's say you had frequency data of a song at a particular moment in time. Each sample is a value that ranges from 0 to 255. In interval notation, it would be written as [0, 255]. The square brackets denote that the endpoints are inclusive - in other words, 0 and 255 are acceptable. We want to take a sample in this range and map it to a different range. Let's say our output range is [-1, 1]. The [-1, 1] range actually pops up a lot in various fields such as graphics programming or trigonometry. For example the range for the sine and cosine functions is [-1, 1].

So we want to take the range [0, 255] and map it to the range [-1, 1]. Of course, we'd want the mapping t

@joshwashywash
joshwashywash / normalize.md
Created March 28, 2023 23:15
normalize a range [min, max] to [0, 1]
const normalize = (min: number) => (max: number) => (x: number) => (x - min) / (max - min);

// usage
const min = 25;
const max = 100;
const n = normalize(min)(max);

const ns = [32, 62.5, 90].map(n);
@joshwashywash
joshwashywash / typedKeys.md
Last active February 13, 2023 18:23
gets keys of an object in typescript
const typedKeys = <O extends {}>(o: O) =>
	Object.keys(o) as Array<keyof O>;

const o = {
	first: 'josh',
	last: 'o'
};

const keys = Object.keys(o);
@joshwashywash
joshwashywash / grid.md
Last active February 13, 2023 18:22
an svg grid svelte component

Svelte SVG Grid

parameter description
radius length from edge of the viewbox to the center (essentialy width/2)
count number of lines
color stroke color of lines
opacity stroke opacity of lines
@joshwashywash
joshwashywash / shuffle.ts
Last active March 6, 2023 16:45
shuffles an array in place
const swap = <T>(t: T[], i: number, j: number) => {
[t[i], t[j]] = [t[j], t[i]];
};
export const shuffle = <T>(t: T[]) => {
for (let i = t.length - 1; i > 0; i -= 1) {
const j = Math.floor(Math.random() * (i + 1));
swap(t, i, j);
}
};
@joshwashywash
joshwashywash / types.json
Created December 31, 2022 20:51
pokemon types
[
{
"name": "bug",
"color": "#A8B820",
"halfEffectiveAgainst": [
"fairy",
"fighting",
"fire",
"flying",
"ghost",