Skip to content

Instantly share code, notes, and snippets.

// 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)
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': {

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
@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

@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 / 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 / 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 / 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;
package main
import (
"errors"
"fmt"
)
type RS[R any] struct {
a R
err error