Skip to content

Instantly share code, notes, and snippets.

View edwardgeorge's full-sized avatar
🇵🇸

Edward George edwardgeorge

🇵🇸
  • AP Møller — Mærsk
  • Manchester
  • 23:23 (UTC +01:00)
View GitHub Profile
@edwardgeorge
edwardgeorge / topologicalsort.cue
Last active May 22, 2021 10:49
topological sort in cue
import "list"
// it's possible to do a topological sort of a DAG in cue without functions
// by using lazy evaluation to calculate the depth of all nodes
// (calculated as max of all children + 1)
// and then build a list using this for ordering
// (there may be multiple at a given level so we have to flatten).
// this works because we ensure that each node's level is always
// greater than all its children, therefore will always order
// after anything that it is dependent on.
{-# LANGUAGE GADTs, RankNTypes, TypeFamilies, DataKinds, PolyKinds, TypeOperators, ScopedTypeVariables #-}
-- Lots of ways you can phrase this, but this works for me
-- For folks who haven't seen it before, this is "the essence of the sum type" and sigma stands for sum.
-- You see it far more often in dependent types than elsewhere because it becomes a lot more pleasant to
-- work with there, but it's doable in other contexts too. Think of the first parameter to the data
-- constructor as a generalized "tag" as we talk about in "tagged union", and the second parameter is the
-- "payload". It turns out that you can represent any simple sum type you could write in Haskell this way
-- by using a finite and enumerable `f`, but things can get more unusual when `f` isn't either. In such
-- cases, it's often easier to think of this as the essence of existential types.
from collections import namedtuple
from functools import reduce
Scientific = namedtuple("Scientific", "coefficient exponent")
def decimal_to_scientific(dec):
sign, digits, exp = dec.as_tuple()
coeff = reduce(lambda x, y: (x * 10) + y, digits, 0)
return Scientific(-coeff if sign else coeff, exp)
@edwardgeorge
edwardgeorge / ghyloM.hs
Last active December 19, 2017 10:59
effectful generalised hylomorphism
ghyloM
:: forall w f m n a b. (Comonad w, Traversable f, Monad m, Monad n)
=> (forall x. f (w x) -> w (f x))
-> (forall x. m (f x) -> f (m x))
-> (forall x. w (n x) -> n (w x))
-> (forall x. m (n x) -> n (m x))
-> (f (w b) -> n b)
-> (a -> n (f (m a)))
-> a
-> n b
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, TypeFamilies, ScopedTypeVariables, RankNTypes,
ConstraintKinds, TypeOperators, UndecidableInstances #-}
module Lib2 where
import Data.Constraint
import Data.Constraint.Forall
class C1 a b
class C2 a b
from collections import deque
from functools import partial, singledispatch
# generic functions on a piece of data:
# > transform((1, (2, {'foo': 3, 'bar': 'hello'})), mktrans(int, lambda i: i * 2))
# (2, (4, {'foo': 6, 'bar': 'hello'}))
# > transform((1, (2, {'foo': 3, 'bar': 'hello'})), mktrans(str, lambda i: i * 2))
# (1, (2, {'bar': 'hellohello', 'foo': 3}))
# > descend((1, (2, {'foo': 3, 'bar': 'hello'})), mktrans(int, lambda i: i * 2))
# (2, (2, {'bar': 'hello', 'foo': 3}))
@edwardgeorge
edwardgeorge / URIToConnect.hs
Last active October 11, 2016 21:21
Convert URI (ie: `postgresql://scott:tiger@localhost:5432/mydatabase`) into postgresql-simple ConnectInfo (with prisms).
module URIToConnect (connectInfo,
connectInfoToURI,
uri,
uriToConnectInfo) where
import Control.Applicative ((<|>), empty)
import Control.Lens hiding (noneOf, uncons) -- from: lens
import Control.Monad (replicateM)
import Data.ByteString.Base16.Lazy (decode) -- from: base16-bytestring
import Data.ByteString.Builder as BSB -- from: bytestring
import Data.ByteString.Lazy (ByteString) -- from: bytestring
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
import Data.Proxy (Proxy(..))
import DBus (IsVariant(..), Variant)
@edwardgeorge
edwardgeorge / TSL.hs
Last active May 11, 2016 09:22
association list with type-level keys which prevents duplicates by construction
{-# LANGUAGE GADTs #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE ScopedTypeVariables #-}
import Data.Promotion.Prelude
import Data.Singletons.Prelude
// when I have to write some node for the first time ...
Promise.fromGen = function (gen, v) {
return new Promise(function (resolve) {
var cont = function (g, v) {
var x = g.next(v);
if (x.done) { return resolve(x.value); }
return x.value.then((v) => cont(g, v));
};
cont(gen(v));