Skip to content

Instantly share code, notes, and snippets.

View t0yv0's full-sized avatar
🎯
Focusing

Anton Tayanovskyy t0yv0

🎯
Focusing
View GitHub Profile
@t0yv0
t0yv0 / Regex.fs
Created July 15, 2011 17:31
F# translation of a Haskell regex combinator exercise.
(*
See the Haskell source: https://gist.github.com/1082416
There exist a few problems:
(0) How correct is this? Non-termination, stack overflows? The kind
of grammars that are recognized here are obviously not simply
regular expressions, because of the fixpoints. Try for instance:
@t0yv0
t0yv0 / Binary.fs
Created July 14, 2011 15:00
Binary encoder/decoder for F# types.
module Serialization.Binary
exception EncodingError
exception NoEncoding of System.Type with
override this.ToString() =
sprintf "Failed to derive a binary encoding for type: %O" this.Data0
type E = (string -> int) -> System.IO.BinaryWriter -> obj -> unit
type D = (int -> string) -> System.IO.BinaryReader -> obj
@t0yv0
t0yv0 / Regex.hs
Created July 14, 2011 13:10
A simple regex writeup in Haskell.
{-# OPTIONS -XGADTs #-}
-- This is an excercise in constructing a combinator-based regular
-- expression matcher for self-education.
--
-- Special thanks to Roman Cheplyaka (@shebang) for pointing out to me
-- the need for state reduction to eliminate exponential complexity on
-- certain benchmarks.
module Regex (Regex, SM, token, compile, run, char, string) where
@t0yv0
t0yv0 / MonadExamples.fs
Created July 2, 2011 23:29
Emulating higher kinds with F# inline functions.
(* F# lacks proper support for higher-kinded abstraction such as ML
functors or Haskell typeclasses. In particular it makes it seemingly
impossible to define functions that work for every type constructor.
The code below demonstrates that this functionality can be partially
recovered by using inline functions with static member constraints.
The approach requires explicitly passing typeclass instance values.
Error messages and derived types are horrible. Nonetheless, there is
some type safety - if the typeclass instances have been defined right,
type-unsafe code will be rejected by the typechecker, albeit with a
hairy message. Another disadvantage is the need to invent operators. *)
@t0yv0
t0yv0 / FSCGI.Structural.Converter.fs
Created June 30, 2011 01:07
An attempt to improve on OWIN<http://owin.org> in F#.
/// Converts structural encodings to proper FSCGI.* types.
module FSCGI.Structural.Converter
type private Encodings<'R,'W> =
('W -> Writer<'W>) *
('R -> Response<'R,'W>)
let private ConvertRequest (r : FSCGI.Request) : Request =
(
r.Headers,
function merge(a, b) {
if (typeof b == "object") {
for (var p in b) {
a[p] = b[p];
}
return a;
} else {
return b;
}
}
@t0yv0
t0yv0 / ClosureSerialization.hs
Created May 31, 2011 13:20
An attempt to figure out an easy way to serialize function types.
{-# OPTIONS -XExistentialQuantification -XMultiParamTypeClasses #-}
-- Serialization requires being able to marshall and unmarshall.
class Serializable a where
serialize :: a -> String
deserialize :: String -> (a, String)
-- Static assertion that `a` encodes `b -> c`.
class Encoding a b c where
decode :: a -> b -> c
type R =
{
A : int
B : int
C : int
}
let Test =
{
A = (printf "A"; 1)
@t0yv0
t0yv0 / Listing.tex
Created April 28, 2011 18:14
LaTeX command for typesetting pretty-printed code blocks.
% ``\Listing{...}'' command typesets its argument as a code block -
% with monospace font and whitespace preserved. It is an
% environment-like command accepting multiple paragraphs.
\def\Listing{\bigskip\begingroup%
\setlength{\parskip}{0pc}\addtolength{\parindent}{1pc}%
\tt\scriptsize\obeylines\obeyspaces\PreserveWhitespace\ListingBody}
% ``\ListingBody'' is an auxillary command need to make Listing work.
% It inserts the argument and closes the group opened by Listing.
\long\def\ListingBody#1{#1\endgroup\smallskip}
@parser::members {
private bool CanInsertSemicolon {
get {
var i = input.LA(1);
return (i == RCURL
|| i == EOF
|| input.LT(1).Line > input.LT(-1).Line);
}
}