Skip to content

Instantly share code, notes, and snippets.

@sarkologist
sarkologist / Foci.hs
Created September 27, 2017 03:32
composable bundles of traversals
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE RecordWildCards #-}
module Control.Lens.Foci where
import qualified Control.Category as Cat
import Control.Applicative
import Control.Monad
@sarkologist
sarkologist / fold.py
Last active February 8, 2018 03:54
composable folds in python
from functools import partial, wraps, reduce
class Fold:
def __init__(self, zero, update, out):
self.zero = zero
self.update = update
self.out = out
def __add__(self, other):
zero = (self.zero, other.zero)
@sarkologist
sarkologist / AwsConfig.hs
Created April 11, 2018 12:47
free applicative parsing of environment variables
s3AccessKey :: EnvVar Aws.AccessKey
s3AccessKey = mkEnvVar "S3_ACCESS_KEY" (Just . Aws.AccessKey . convertString) Nothing
s3SecretKey :: EnvVar Aws.SecretKey
s3SecretKey = mkEnvVar "S3_SECRET_KEY" (Just . Aws.SecretKey . convertString) Nothing
s3FromKeys :: EnvVar Aws.Credentials
s3FromKeys = iI Aws.FromKeys s3AccessKey s3SecretKey Ii
@sarkologist
sarkologist / FreeApplicativeTypesafeConfig.scala
Last active April 26, 2018 12:15
first sketch of free applicative config in scala
import com.typesafe.config.{Config, ConfigFactory}
object Configured extends App {
val config = ConfigFactory.load()
import TypesafeConfig._
import FreeConfig._
import scalaz.syntax.apply._
val kafkaProducerBackoff: BackoffConfig[KafkaProducerBackoff] =
@sarkologist
sarkologist / RSI.hs
Created June 13, 2018 14:26
RSI with composable folds
import qualified Control.Foldl as Fold
import Control.Applicative
import Data.Profunctor
type Price = Double
type Prices = [ Price ]
type Period = Int
changes :: Prices -> Prices
changes ps = zipWith (-) (tail ps) ps
@sarkologist
sarkologist / convert.hs
Last active August 9, 2018 14:28
convert ScanM to Pipe
import Control.Scanl
import Pipes
import Pipes.Lift
scanMToPipe :: (Monad m) => ScanM m a b -> Pipe a b m ()
scanMToPipe (ScanM step initial) =
let pipe = do
a <- await
b <- lift $ step a
yield b
λ: let a = stagger [] (arr (1:)) (arr (0:)) in mapM_ print $ scan a (Prelude.replicate 5 ())
([1],[0,1])
([1,0,1],[0,1,0,1])
([1,0,1,0,1],[0,1,0,1,0,1])
([1,0,1,0,1,0,1],[0,1,0,1,0,1,0,1])
([1,0,1,0,1,0,1,0,1],[0,1,0,1,0,1,0,1,0,1])
@sarkologist
sarkologist / gist:7ba54f286bca9b349f8df315a79f7634
Created July 31, 2019 08:12
dynamic typing in haskell (from sandy maguire's thinking with types)
data Dynamic where
Dynamic :: Typeable t => t -> Dynamic
elimDynamic :: (forall a. Typeable a => a -> r) -> Dynamic -> r
elimDynamic f (Dynamic a) = f a
fromDynamic :: Typeable a => Dynamic -> Maybe a
fromDynamic = elimDynamic cast
liftD2 :: forall a b r. ( Typeable a , Typeable b , Typeable r )
@sarkologist
sarkologist / ValueProviderPimp.scala
Last active October 28, 2019 04:07
ValueProvider[ConnectionConfiguration]
package utils
import org.apache.beam.sdk.options.ValueProvider
import org.apache.beam.sdk.options.ValueProvider.{
NestedValueProvider,
StaticValueProvider
}
import scalaz.{Applicative, Functor}
object ValueProviderPimp {
@sarkologist
sarkologist / CoGroupByKeyUtil.scala
Last active December 5, 2019 02:49
CoGroupByKeyUtil
object CoGroupByKeyUtil {
// a workaround for scala's lack of rank-2 polymorphism
// so that we can allow the called function to decide how to instantiate F[_]
trait Rank2[F[_]] {
def apply[A]: F[A]
}
def eitherOf[K: Coder, A: Coder, B: Coder](
left: PCollection[KV[K, A]],
right: PCollection[KV[K, B]],