Skip to content

Instantly share code, notes, and snippets.

View elsaulio's full-sized avatar
🎭

saŭlo elsaulio

🎭
View GitHub Profile
@elsaulio
elsaulio / signal.worksheet.sc
Last active September 30, 2024 23:56
Scala basic signal
// Translated from https://www.youtube.com/watch?v=bUy4xiJ05KY
import scala.collection.mutable.ListBuffer
var RUNNING: Option[() => Any] = None
class Signal[T](initial: T) {
private var _value = initial
private var observers = ListBuffer[() => Any]()
//> using scala 3.nightly
import language.experimental.namedTuples
type Person = (name: String, age: Int)
type Dog = (name: String, age: Int, breed: String)
type LivingThing = Person | Dog
@main
def main(): Unit =
@elsaulio
elsaulio / 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) =
@elsaulio
elsaulio / 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) => {