This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE FunctionalDependencies #-} | |
{-# LANGUAGE MultiParamTypeClasses #-} | |
{-# LANGUAGE ScopedTypeVariables #-} | |
{-# LANGUAGE TypeFamilies #-} | |
{-# LANGUAGE TypeOperators #-} | |
{-# LANGUAGE UndecidableInstances #-} | |
import Data.Proxy (Proxy(..)) | |
import DBus (IsVariant(..), Variant) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE GADTs #-} | |
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE KindSignatures #-} | |
{-# LANGUAGE PolyKinds #-} | |
{-# LANGUAGE RankNTypes #-} | |
{-# LANGUAGE TypeFamilies #-} | |
{-# LANGUAGE TypeOperators #-} | |
{-# LANGUAGE ScopedTypeVariables #-} | |
import Data.Promotion.Prelude | |
import Data.Singletons.Prelude |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)); |
NewerOlder