Skip to content

Instantly share code, notes, and snippets.

@rgrinberg
rgrinberg / bag_vs_hash_set.ml
Created June 7, 2013 16:08
Bag vs Hash_set when used as a hashtable key. bag acts strange?
open Core.Std
module type Collection = sig
type 'a t
val create : unit -> 'a t
val add : 'a t -> 'a -> unit
val of_list : 'a list -> 'a t
val iter : 'a t -> f:('a -> unit) -> unit
end
@rgrinberg
rgrinberg / tinycache.ml
Last active October 25, 2016 15:36
simple cache server example in core
open Core.Std
open Core_extended.Std
open Async.Std
module Protocol = struct
module Client_message = struct
type t =
| Get of string
| Set of string * string
| Quit with sexp
@rgrinberg
rgrinberg / mvar.ml
Created June 8, 2013 00:16
sweek's implementation of an mvar in async
open Core.Std
open Async.Std
module Mvar : sig
type 'a t with sexp_of
include Invariant.S1 with type 'a t := 'a t
val create : unit -> _ t
val put : 'a t -> 'a -> unit Deferred.t
val take : 'a t -> 'a Deferred.t
end = struct
@rgrinberg
rgrinberg / rx.rb
Created July 15, 2013 15:20
rxruby
# Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root
require 'thread.rb'
# Disposables
class Disposable
def initialize(&disposable_action)
@disposable_action = disposable_action
@rgrinberg
rgrinberg / Inisec.hs
Created August 17, 2013 06:02
simple ini parser using attoparsec
{-# LANGUAGE OverloadedStrings #-}
module Inisec where
import Prelude as P
import Data.Attoparsec.Text
import Data.Text.IO as T
import Data.Text
import Control.Applicative
data Section = Section
{ entries :: [(Text, Text)]
@rgrinberg
rgrinberg / fiber_hangman.rb
Created August 25, 2013 18:22
hangman using ruby fibers
#!/usr/bin/env ruby
require 'fiber'
require 'set'
def state full_word, revealed
all_letters = full_word.split('') - [' ']
hidden = all_letters.to_set - revealed
hidden.inject(full_word) {|acc, e| acc.gsub e, '_' }
end
@rgrinberg
rgrinberg / webapp2_handler.py
Last active December 21, 2015 20:18
webapp2 handler as a decorator. when defining a full class seems "heavy"
def webapp2_handler(method):
allowed_methods = set(['GET', 'POST', 'DELETE', 'PUT'])
if method not in allowed_methods:
raise ValueError("%s is an invalid HTTP method" % method)
def wrap(f):
return type('', (webapp2.RequestHandler,), {
method.lower(): f
})
return wrap
@rgrinberg
rgrinberg / option_parse_gadt.ml
Created September 14, 2013 16:36
no need for objects
type 'a option_parser = string list -> 'a list
type 'a handler = 'a list -> unit
type subcommand =
| Subcommand : 'a option_parser * 'a handler -> subcommand
let handle_run options =
let refresh = ref false in
let wrapper = ref None in
@rgrinberg
rgrinberg / bencode.hs
Created October 3, 2013 22:09
bencode parser in attoparsec
module Bencode where
import Data.Maybe (fromJust)
import Control.Monad (liftM)
import Control.Applicative
import Data.ByteString.Char8 as BS
import Data.Attoparsec.ByteString.Char8 as A hiding (string)
import qualified Text.Show.Pretty as Pr
data Bencode = String ByteString
@rgrinberg
rgrinberg / async_fiber.ml
Created October 24, 2013 01:36
delimcc/async fiber
open Core.Std
open Async.Std
let print_endline = Caml.print_endline
module Fiber : sig
val start : (unit -> 'a) -> 'a Deferred.t
val await : 'a Deferred.t -> 'a
end = struct
let active_prompt = ref None