Skip to content

Instantly share code, notes, and snippets.

@i-am-tom
i-am-tom / db-alt.js
Last active February 20, 2020 03:01
Database failover modelled with the `alt` typeclass.
const Task = require('data.task')
Task.prototype.alt = function (that) {
return new Task((rej, res) =>
this.fork(_ => that.fork(rej, res), res))
}
const hosts = [
[ 'db1.mysite.com', 'user', 'password' ],
[ 'db2.mysite.com', 'user', 'password' ],
@i-am-tom
i-am-tom / Json.purs
Last active February 15, 2020 17:14
Parsing, Generating, and Diffing JSON in PureScript
module Main where
-- | JSON is an incredibly simple format. Even its lists are untyped.
-- | As with all languages, functional programming encourages us to
-- | make a domain-specific language (or DSL) to capture the "ideas"
-- | of the language, which we can then use to talk about its content.
-- | In this little snippet, we'll build a JSON DSL, transform it into
-- | a recursive structure, and then use that result to generate some
@i-am-tom
i-am-tom / Read.hs
Created December 23, 2019 13:39
Replacing function arguments with `MonadReader` calls.
-----------------------------------------------------------
-- HOW TO ASK FOR HELP AND AVOID ARGUMENTS.
--
-- When we want to use an externally-provided package (such as logging,
-- database connections, etc) in our work code, it might require some initial
-- config that we traditionally store in our environment.
--
-- We might even require something like a database connection or file handler
-- /throughout/ the lifetime of the code, in order to make queries or similar.
--
@i-am-tom
i-am-tom / Record.hs
Created January 14, 2019 22:14
Declarative record migration.
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
@i-am-tom
i-am-tom / 01-DependentTypes.idr
Created October 10, 2019 13:59
HaskellX code examples!
module DependentTypes
TypeOf : {t : Type} -> t -> Type
TypeOf {t} _ = t
-- EXAMPLES
eg0 : TypeOf 3 = Integer
eg0 = Refl
@i-am-tom
i-am-tom / Main.hs
Last active June 27, 2019 14:07
Using Higgledy to create parser fallbacks.
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MonoLocalBinds #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeApplications #-}
module Main where
import Control.Applicative (Alternative (..))
@i-am-tom
i-am-tom / Transformers.hs
Created June 1, 2019 19:03
A little talk for Friday afternoon.
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
@i-am-tom
i-am-tom / Service.hs
Created May 8, 2019 12:49
Sketching out composite validation
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
@i-am-tom
i-am-tom / Listify.hs
Created February 5, 2019 13:54
JavaScript.method(... xs)
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Main where
import Data.Kind
@i-am-tom
i-am-tom / DAG.hs
Last active April 18, 2019 09:48
Cached evaluation
{-# OPTIONS_GHC -Wno-name-shadowing #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}