Skip to content

Instantly share code, notes, and snippets.

@pqwy
pqwy / delimcc.scm
Created April 25, 2017 13:24
call/cc -> delimited continuations
(define-syntax let/cc
(syntax-rules () [(_ cc e ...) (call/cc (lambda (cc) e ...))]))
(define prompt
(make-parameter (lambda _ (raise 'undelimited-shift))))
(define (call-with-reset f)
(let/cc c-frame
(parameterize
@pqwy
pqwy / multimap.ml
Created March 13, 2016 17:15
Multimap
module MultiMap (T : Map.OrderedType) : sig
type 'a t
val of_list : (T.t list * 'a) list -> 'a t
val find : T.t -> 'a t -> 'a option
val find_update : T.t -> ('a -> 'a) -> 'a t -> ('a * 'a t) option
end =
struct
module TMap = Map.Make (T)
module IMap = Map.Make (struct type t = int let compare a b = compare a b end)
@pqwy
pqwy / lru.ml
Last active December 7, 2015 18:32
lru!
module Option = struct
let (>>=) a fb = match a with Some x -> fb x | _ -> None
let (>|=) a f = match a with Some x -> Some (f x) | _ -> None
end
type ('k, 'v) tree =
| Tip
| Node of 'k * 'v * int * ('k, 'v) tree * ('k, 'v) tree
type ('k, 'v) t = { gen : int ; size : int ; tree : ('k, 'v) tree }
@pqwy
pqwy / dumper.ml
Last active November 4, 2015 20:59
dumper
let encodev ?(encoding=`UTF_8) xs =
let b = Buffer.create 16 in
let e = Uutf.encoder encoding (`Buffer b) in
List.iter (fun x -> Uutf.encode e (`Uchar x) |> ignore) xs;
Uutf.encode e `End |> ignore;
Buffer.contents b
module S = struct
@pqwy
pqwy / build.sh
Last active September 24, 2015 09:04
GPG me.
#!/bin/sh
ocamlfind ocamlopt -linkpkg -package lwt,lwt.unix,lwt.syntax,nocrypto,nocrypto.unix -syntax camlp4o gpg.ml -o gpg
A safe-prime Diffie-Hellman modulus, now with 170985 more bits!
p = 5402161229914426767924245696512848424216642600277301421852665098462321252358160289695158322139396298898114241481624851759410350491442565136633390598064091112606611545640411668399837120163255638920708873829467481796881790459026743305967234516857486260852430427155182772897935766821624666331175088532789357194386350745269542352851867894105353000097433263811496321609182397660588268252384366066130372542475460662119218809439851161749110724836426149223127067729554521742353731995648277137696519684248147031255896594754305095200318596313907167369711868278479227611088476138554403349648435181592894279334345817425321039557813711634226085073314759977332019277887660028404939129724636141684030217480107685966218013362314933067922641942913196064504480487180075664272877336018079244317720345228452822508677898086987652051812876685400696701889028307036069964610800845458882168175466152432528035105783114155167462803487857885282262001408034217228333910159756102527263
@pqwy
pqwy / dsa.ml
Created October 22, 2014 17:55
DSA, with fries and modules
module Dsa = struct
open Nocrypto
open Nocrypto.Uncommon
type priv = { p : Z.t ; q : Z.t ; gg : Z.t ; x : Z.t ; y : Z.t }
type pub = { p : Z.t ; q : Z.t ; gg : Z.t ; y : Z.t }
let pub_of_priv ({ p; q; gg; y }: priv) : pub = { p; q; gg; y}
@pqwy
pqwy / words.hs
Last active August 29, 2015 14:04
import Data.List
import Data.Function
import Control.Arrow
import qualified Data.HashMap.Strict as M
import qualified Data.ByteString.Lazy.Char8 as BL
import qualified Data.ByteString.Char8 as BS
import Text.Printf
histo = M.toList . foldl' (\m x -> M.insertWith (+) x (1 :: Int) m) M.empty
@pqwy
pqwy / words.ml
Last active August 29, 2015 14:04
open Lwt
open Lwt_io
let adjust ht f k v =
Hashtbl.(replace ht k @@ try f (find ht k) v with Not_found -> v)
let histo stream =
let ht = Hashtbl.create 16 in
Lwt_stream.iter (fun x -> adjust ht (+) x 1) stream
>|= fun () -> Hashtbl.fold (fun k v xs -> (k, v)::xs) ht []
@pqwy
pqwy / entropy.ml
Created July 6, 2014 15:42
Ideal `ENTROPY` from the perspective of Fortuna
(*
The ideal interface for an entropy-device from the perspective of Fortuna.
*)
module type ENTROPY = sig
(* With a pull-based design, there is the problem of wasted work. Since the
* client of the entropy does not know how often there is something worth
* checking, it might do it too frequently / infrequently. If it stalls, there