Skip to content

Instantly share code, notes, and snippets.

@utenma
utenma / unionMatch.worksheet.sc
Created May 12, 2023 16:01
Scala 3 Pattern Matching on Unions
type Primitive = Boolean | Int | Float | Double | Long | String
val x: Int = 1
// x: Int = 1
val y: String = "i"
// y: String = i
val z: Double = 1.0
// z: Double = 1.0
def primitiveMatch(x: Primitive) =
@utenma
utenma / useInterval.ts
Last active December 19, 2022 22:44
A simple hook for managing intervals with React
import { useEffect, useRef } from 'react';
/**
* Activate with a delay greater than zero
* The last callback is applied immediately
*
* Adapted from https://usehooks-ts.com/react-hook/use-interval
*/
export function useInterval(callback: () => void, delay: number | null) {
const savedCallback = useRef(callback);
/* eslint-disable */
type PipeFlow<Payload, Stage extends string> = {
stage: Stage
status: boolean
payload?: Payload
}
type FilterFunction<In = any, Out = any, Stage extends string = string> = (input: In) => PipeFlow<Out, Stage>
/* eslint-disable */
type FilterFunction<In = any, Out = any> = (input: In, next: (flow: boolean, nextInput: Out) => any) => any
export function pipelineSync<FirstIn, FirstOut>(first: FilterFunction<FirstIn, FirstOut>) {
const pipeline: Array<FilterFunction> = [first]
const run = (input: FirstIn): any => pipeline
.reduceRight((nextFunc, currentFunc, index) => {