Skip to content

Instantly share code, notes, and snippets.

View kazu-yamamoto's full-sized avatar
💭
QUIC and TLS 1.3

Kazu Yamamoto kazu-yamamoto

💭
QUIC and TLS 1.3
View GitHub Profile
@kazu-yamamoto
kazu-yamamoto / gist:6265119
Last active December 21, 2015 06:38
Unravelling greedy algorithms
module Main where
-- Pearls of Functional Algorithm Design
-- Section 8: Unravelling greedy algorithms
import Control.Exception
import Control.Applicative
main :: IO ()
main = handle handler $ do
@kazu-yamamoto
kazu-yamamoto / gist:6237308
Created August 15, 2013 00:51
Monad.Reader 22: GADT
{-# LANGUAGE GADTs #-}
module Gadt where
import Control.Arrow (first)
import Data.Char
----------------------------------------------------------------
data Dynamic where
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP, ForeignFunctionInterface #-}
-- ghc --make -O2 -funbox-strict-fields -threaded -rtsopts SimpleServer.hs
import Network.Socket
import System.Environment (getArgs)
import Control.Concurrent
import Control.Monad
import Foreign
@kazu-yamamoto
kazu-yamamoto / gist:5882488
Created June 28, 2013 04:40
An example for Operational Monad with two interpreters.
{-# LANGUAGE GADTs #-}
module Main where
import Control.Monad
import Control.Monad.RWS.Strict
import Control.Monad.Trans.Error
import Control.Monad.Operational.Simple
import Prelude hiding (putChar, getChar)
import qualified System.IO as IO
@kazu-yamamoto
kazu-yamamoto / gist:5873204
Created June 27, 2013 01:09
Free Operational example to be interpreted to IO and pure testing.
{-# LANGUAGE GADTs #-}
module Main where
import Control.Monad
import Control.Monad.Operational.Simple
import Prelude hiding (putChar, getChar)
import qualified System.IO as IO
import Test.QuickCheck
@kazu-yamamoto
kazu-yamamoto / gist:5597749
Last active December 17, 2015 10:49
Implementing Haskell's IO in OCaml
type world = unit
type 'a io = world -> 'a
(* write : string -> unit io *)
let write str = fun () -> print_string str
(* read : string io *)
let read = read_line
(* run : a' io -> 'a *)
@kazu-yamamoto
kazu-yamamoto / gist:5582673
Created May 15, 2013 09:15
Reading a password in Haskell
import System.Posix.IO
import System.Posix.Terminal
readPassword :: IO String
readPassword = do
echoOn <- getTerminalAttributes stdInput
let echoOff = withoutMode echoOn EnableEcho
fdWrite stdOutput "Password: "
setTerminalAttributes stdInput echoOff Immediately
pass <- getLine
@kazu-yamamoto
kazu-yamamoto / gist:5218431
Last active July 22, 2022 13:44
Purely functional Dijkstra algorithm using priority search queue to find shortest paths
-- % cabal install PSQueue
-- % ghci Dijkstra.hs
-- > dijkstra sample A
-- [(A,0,A),(D,4,A),(E,7,D),(C,8,E),(B,9,E)]
module Dijkstra where
import Control.Applicative hiding (empty)
import Data.List (unfoldr)
import Data.Maybe (fromJust)
@kazu-yamamoto
kazu-yamamoto / gist:5054423
Created February 28, 2013 05:24
Storable example
{-# LANGUAGE CPP
, ForeignFunctionInterface
, BangPatterns
, GeneralizedNewtypeDeriving #-}
#include <poll.h>
import Foreign.C.Types (CInt(..), CShort(..), CChar(..))
import Foreign.Marshal.Alloc
import Foreign.Ptr (Ptr, castPtr, plusPtr)
@kazu-yamamoto
kazu-yamamoto / gist:4711284
Last active December 12, 2015 03:59
Free Monad example with DeriveFunctor
{-# LANGUAGE DeriveFunctor #-}
module Main where
import Control.Monad
import Control.Monad.Free
import Prelude hiding (putChar, getChar)
import qualified System.IO as IO
import Test.QuickCheck