Skip to content

Instantly share code, notes, and snippets.

@bradparker
bradparker / Networking.hs
Last active December 8, 2019 03:47
Low-level Networking in Haskell (largely C so far)
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -Wall #-}
module Main where
@bradparker
bradparker / iterate.js
Last active November 1, 2019 05:13
Flattening nested iterators ... for use with weird DynamoDB scan API
const testItems = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const limit = 2;
const getBatch = (cursor = null) => {
const currentCursor = cursor || 0;
const nextPotentialCursor = currentCursor + limit;
const nextCursor = nextPotentialCursor > 10 ? null : nextPotentialCursor;
return new Promise(resolve =>
setTimeout(() => resolve({
@bradparker
bradparker / Ind.hs
Created July 28, 2019 02:43
Trying to figure out B. Milewski's slide from Lambda Jam 2019
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -Wall #-}
@bradparker
bradparker / Refined.ts
Last active July 25, 2019 10:35
Refinement type-y kinda things
interface Refinement<A> {
tag: string;
value: A;
}
interface Refined<A, R> {
value: A;
refinement: R;
}
import Control.Monad.State
import Data.Traversable
import Data.Tuple
toByteBits :: Integral n => n -> [n]
toByteBits =
evalState (for [1..8] (\_ -> state (\s -> swap (divMod s 2))))
main = do
print $ toByteBits 1
@bradparker
bradparker / Byte.hs
Last active July 6, 2019 01:35
Bytes and Folds / Traversals
{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE RankNTypes #-}
module Byte where
import Data.Bool (bool)
import Data.Functor.Const (Const(Const, getConst))
import Data.Bits ((.&.), (.|.), xor)
import Prelude hiding (sum)
@bradparker
bradparker / concurrency.ts
Created July 1, 2019 00:05
JS concurrency _stuff_
interface Person {
name: string;
age: number;
}
const people: Person[] = [{ name: "Bob", age: 100 }, { name: "Jane", age: 50 }];
const findPeople = (candidate: string): Promise<Person[]> => {
return new Promise(resolve => {
const timeout = Math.floor(Math.random() * 100);
@bradparker
bradparker / App.ts
Created June 16, 2019 05:33
Geneator function do notation experiment
import * as Either from "./Either";
type Action<I, E, A> = (input: I) => Promise<Either.Either<E, A>>;
export class App<I, E, A> {
public run: Action<I, E, A>;
public constructor(action: Action<I, E, A>) {
this.run = action;
}
@bradparker
bradparker / prepare-commit-message
Last active May 31, 2019 00:54
Git Hook to prepend a commit message with the Jira ticket id at the start of the current branch's name
#!/usr/bin/env bash
branchname=$(git branch | grep ^* | sed 's/\* //g')
regex="([A-Z]*-[0-9]*)"
if [[ $branchname =~ $regex ]]
then
ticket_id="${BASH_REMATCH[1]}"
message=`cat $1`
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE FlexibleInstances #-}
module Traversals where
import Control.Applicative (Applicative(..))
import Data.Bool (Bool(True))