Skip to content

Instantly share code, notes, and snippets.

@YoEight
YoEight / output
Last active August 29, 2015 14:25
WTF rustc ??? (Builder pattern)
$ cargo build
Compiling project v0.1.0 (file:///home/yoeight/code/rust/project-driver)
src/project/internal/types.rs:48:30: 48:34 error: cannot move out of borrowed content
src/project/internal/types.rs:48 connection_type: self.connection_type,
^~~~
src/project/internal/types.rs:50:20: 50:24 error: cannot move out of borrowed content
src/project/internal/types.rs:50 creds: self.creds
^~~~
error: aborting due to 2 previous errors
Could not compile `project`.
benchmarking map/machines
time 49.16 ms (49.08 ms .. 49.24 ms)
1.000 R² (1.000 R² .. 1.000 R²)
mean 49.21 ms (49.17 ms .. 49.28 ms)
std dev 91.09 μs (50.76 μs .. 146.6 μs)
benchmarking map/pipes
time 32.91 ms (32.86 ms .. 32.95 ms)
1.000 R² (1.000 R² .. 1.000 R²)
mean 32.94 ms (32.92 ms .. 32.98 ms)
@YoEight
YoEight / code.hs
Created February 13, 2015 07:23
Bidirectional communication between 2 processes using machines library
{-# LANGUAGE RankNTypes, GADTs #-}
module Bidi where
import Control.Monad.Trans
import Data.Machine
data Pipe a' a b' b c where
Request :: a' -> Pipe a' a b' b a
{-# LANGUAGE RankNTypes #-}
newtype State s a = State { runState :: forall r. s -> (a -> s -> r) -> r }
stateReturn :: a -> State s a
stateReturn a = State $ \s k -> k a s
stateMap :: (a -> b) -> State s a -> State s b
stateMap f (State sa) = State $ \s kb -> sa s (kb . f)
@YoEight
YoEight / State.hs
Last active August 29, 2015 14:07
A different State encoding (solution here https://gist.github.com/YoEight/0541aa293effbfff8d02)
{-# LANGUAGE RankNTypes #-}
newtype State s a = State { runState :: forall r. s -> (a -> s -> r) -> r }
stateReturn :: a -> State s a
stateReturn = undefined
stateMap :: (a -> b) -> State s a -> State s b
stateMap = undefined
@YoEight
YoEight / bug.hs
Last active August 29, 2015 14:04
Is that a bug ? (GHC 7.8.2)
{-# LANGUAGE GADTs, ExistentialQuantification, RankNTypes #-}
data A
data B
data C
data Foo a where
FooA :: Foo A
FooB :: Foo B
FooC :: Foo C
@YoEight
YoEight / exo.hs
Created June 21, 2014 16:43
Cycle function expressed as anamorphism
import Prelude hiding (cycle)
newtype Mu f = Mu (f (Mu f))
data Cons a b
= Nil
| Cons a b
instance Functor (Cons a) where
fmap _ Nil = Nil
@YoEight
YoEight / Unix.hs
Created May 9, 2014 22:17
Unix tail using pipes library
module Unix where
import Data.Foldable (traverse_)
import System.IO
import Control.Monad.Trans
import Pipes
import Pipes.Parse
import qualified Pipes.Prelude as P
@YoEight
YoEight / example.hs
Last active August 29, 2015 14:01
Free Monad vs Direct approach with existential type
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE RankNTypes #-}
import Control.Applicative
import Control.Monad
import Control.Monad.State
import Control.Monad.Trans
--------------------------------------------------------------------------------
-- | Free encoding
--------------------------------------------------------------------------------
GHC: 7.8.2
Cabal: 1.18.0.3