Skip to content

Instantly share code, notes, and snippets.

View lupuszr's full-sized avatar

Viktor Pelle lupuszr

View GitHub Profile
@lupuszr
lupuszr / urlParser.ts
Created April 12, 2022 19:01
typelevel url parser in typescript
type ParamParser<T extends string> = T extends `${infer param}/${infer rest}` ? [param, ...ParamParser<rest>] : [T]
type TakeApart<T extends string> = T extends `${infer p}=${infer v}` ? [p, v] : []
type QueryParser<T extends string> = T extends `${infer el}&${infer rest}` ? [TakeApart<el>, ...QueryParser<rest>]: [TakeApart<T>]
type urlWithParamAndQuery<T extends string, FB = never> = T extends `${infer Base}/${infer Param}?${infer Query}` ? {base: Base, param: ParamParser<Param>, query: QueryParser<Query>} : FB;
type urlWithQuery<T extends string, FB = never> = T extends `${infer Base}?${infer Query}` ? {base: Base, param: [], query: QueryParser<Query>} : FB;
type urlWithParam<T extends string, FB = never> = T extends `${infer Base}/${infer Param}` ? {base: Base, param: ParamParser<Param>, query: []} : FB;
type staticUrl<T extends string> = {base: T, param: [], query: []};
type url<T extends string> = urlWithParamAndQuery<T, urlWithParam<T, urlWithQuery<T, staticUrl<T>>>>;
package main
import (
"errors"
"fmt"
)
type RS[R any] struct {
a R
err error
@lupuszr
lupuszr / VersionMatch.ts
Created March 3, 2022 10:00
VersionMatch
type SortedArray<T> = Array<T> & {readonly _tag: "SortedArray ${T}"};
type VersionA<T extends string> = T extends `${infer major}.${infer minor}.${infer patch}` ? T : never;
type ComparisionRes = 'LT' | 'EQ' | 'GT';
class Version<T extends string> {
readonly major: T extends `${infer major}.${infer minor}.${infer patch}` ? major : never;
private readonly majorN: number;
readonly minor: T extends `${infer major}.${infer minor}.${infer patch}` ? minor : never;
@lupuszr
lupuszr / SketchSystems.spec
Last active October 25, 2021 18:40
Video Tutor
Video Tutor
Login Module
Unauthorized Dashboard
existing user -> SignIn
new user -> SignUp
SignUp
already an user -> SignIn
submit info -> SubscribeUser
SubscribeUser
user pick a plan -> 2checkoutSubscribtion
@lupuszr
lupuszr / Try.ts
Last active October 15, 2021 06:28
// Proper try data structure implementation in typescript
// pass as thunk
type $<A> = () => A
function $<A>(a: A): $<A>{
return () => a
}
// make function lazy
function $fn<A extends (...args: any) => any>(a: A): ((...p: Parameters<A>) => $<ReturnType<A>>) {
@lupuszr
lupuszr / ContravariantFunctor.ts
Last active March 4, 2021 20:03
Playing with Contravariant functors in typescript - Contravariant functor for comparison
Lets define a sum type `Ordering` with its data constructors:
```typescript
class LT {}
class EQ {}
class GT {}
type Ordering = LT | EQ | GT;
class Comparison<A> {
@lupuszr
lupuszr / README.md
Last active April 24, 2020 10:16 — forked from pbojinov/README.md
Two way iframe communication

Two way iframe communication

The main difference between the two pages is the method of sending messages. Recieving messages is the same in both.

Parent

Send messages to iframe using iframeEl.contentWindow.postMessage Recieve messages using window.addEventListener('message')

iframe

Applied Functional Programming with Scala - Notes

Copyright © 2016-2018 Fantasyland Institute of Learning. All rights reserved.

1. Mastering Functions

A function is a mapping from one set, called a domain, to another set, called the codomain. A function associates every element in the domain with exactly one element in the codomain. In Scala, both domain and codomain are types.

val square : Int => Int = x => x * x
abstract class Season {
cata<W, Su, Sp, Au>(match: {
Winter: () => W,
Summer: () => Su,
Autumn: () => Au,
Spring: () => Sp
}) {
type constraintsT = keyof typeof match;
switch (this.constructor.name as unknown as constraintsT) {
case 'Winter': {
// Universal constructions based on Bartosz Milewski talk on Scala World
// type ProductT<C, A, B> =
function fanout<C, A, B>(f: (c: C) => A, g: (c: C) => B): ((c: C) => [A, B]) {
return c => [f(c), g(c)]
}
// const x = fanout((a: number) => a + 1, b => b + 2)(4)