Skip to content

Instantly share code, notes, and snippets.

View joshburgess's full-sized avatar
💭
🤔

Josh Burgess joshburgess

💭
🤔
View GitHub Profile
@joshburgess
joshburgess / LensLabels.hs
Created February 12, 2019 14:38 — forked from carymrobbins/LensLabels.hs
Using labels as lenses with GHC 8
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE DeriveGeneric #-}
module LensLabels where
import Prelude
import Control.Lens
import Data.Generics.Labels
import GHC.Generics
@joshburgess
joshburgess / EIO.hs
Created February 8, 2019 21:36 — forked from gelisam/EIO.hs
Keeping track of which exceptions have and haven't been handled
-- a continuation of https://gist.github.com/gelisam/137effb33d2777328d366dcb563d8d13
{-# LANGUAGE FlexibleContexts, FlexibleInstances, GeneralizedNewtypeDeriving, LambdaCase, MultiParamTypeClasses #-}
module EIO where
import Control.Monad
import Data.Void
import Test.DocTest
import qualified Control.Exception as E
@joshburgess
joshburgess / io-ts-option-encoding-decoding.ts
Last active February 5, 2019 03:48
io-ts Option encoding/decoding
import * as t from 'io-ts'
import { Option, None, Some, none, fromEither } from 'fp-ts/lib/Option'
export type JSONNone = { _tag: 'None' }
export type JSONSome<A> = { _tag: 'Some'; value: A }
export type JSONOption<A> = JSONNone | JSONSome<A>
export const jsonNone: JSONOption<never> = { _tag: 'None' }
@joshburgess
joshburgess / HKT.swift
Created January 27, 2019 19:49 — forked from anandabits/HKT.swift
Emulating HKT in Swift
// This example shows how higher-kinded types can be emulated in Swift today.
// It acheives correct typing at the cost of some boilerplate, manual lifting and an existential representation.
// The technique below was directly inspired by the paper Lightweight Higher-Kinded Polymorphism
// by Jeremy Yallop and Leo White found at http://ocamllabs.io/higher/lightweight-higher-kinded-polymorphism.pdf
/// `ConstructorTag` represents a type constructor.
/// `Argument` represents an argument to the type constructor.
struct Apply<ConstructorTag, Argument> {
/// An existential containing a value of `Constructor<Argument>`
/// Where `Constructor` is the type constructor represented by `ConstructorTag`
@joshburgess
joshburgess / electric_wadleroo.rs
Created January 23, 2019 02:35 — forked from bodil/electric_wadleroo.rs
Who needs higher kinded types anyway
trait Higher<A, B> {
type Target;
}
trait Higher3<A, B, C> {
type Target2;
type Target3;
}
impl<A, B> Higher<A, B> for Option<A> {
@joshburgess
joshburgess / main.md
Created January 22, 2019 19:18 — forked from hediet/main.md
Proof that TypeScript's Type System is Turing Complete
type StringBool = "true"|"false";


interface AnyNumber { prev?: any, isZero: StringBool };
interface PositiveNumber { prev: any, isZero: "false" };

type IsZero<TNumber extends AnyNumber> = TNumber["isZero"];
type Next<TNumber extends AnyNumber> = { prev: TNumber, isZero: "false" };
type Prev<TNumber extends PositiveNumber> = TNumber["prev"];
@joshburgess
joshburgess / Main.hs
Created January 14, 2019 02:49 — forked from gelisam/Main.hs
Union types (with explicit upcast) in Haskell
-- Example usage
module Main where
import Union
unionValue1 :: Union (String, (Double, ((Int, Int), ())))
unionValue1 = mkUnion "Foo"
unionValue2 :: Union (String, (Double, ((Int, Int), ())))
@joshburgess
joshburgess / List.ts
Created January 6, 2019 11:05
Simulating a recursive linked List data type (a la Haskell, PureScript, etc.) in TypeScript
// A list is either empty (represented by the `Nil` constructor) or non-empty, in
// which case it consists of a head element, and another list (represented by the
// `Cons` constructor).
//
// data List a = Nil | Cons a (List a)
interface Nil {
readonly tag: 'Nil'
}
@joshburgess
joshburgess / Main.hs
Created December 4, 2018 04:41 — forked from gelisam/Main.hs
IndexedMonad example
-- in reply to http://www.reddit.com/r/haskell/comments/21mja6/make_lllegal_state_transitions_unrepresentable/
--
-- We implement a tiny language with three commands: Open, Close, and Get.
-- The first Get after an Open returns 1, the second Get returns 2, and so on.
--
-- Get is only valid while the state is open, and
-- Open must always be matched by a Close.
-- We enforce both restrictions via the type system.
--
-- There are two valid states: Opened and Closed.
@joshburgess
joshburgess / sequence-traverse-mixed-records-options.ts
Last active December 3, 2018 08:31
Sequence & traverse functions for records with mixed Option value types
import { Option, isNone, none, some } from 'fp-ts/lib/Option'
import * as record from 'fp-ts/lib/Record'
export type SafePrimitive = string | number | boolean | object
export type ExtractFromOption<A> = A extends Option<infer B> ? B : never
export const sequenceMixedRecordOptions = <
A extends {
[key: string]: Option<SafePrimitive>
},