Skip to content

Instantly share code, notes, and snippets.

type Async<'T> = ('T -> unit) -> unit
[<Sealed>]
type Async() =
static let self = Async()
member inline this.Return(x: 'T) : Async<'T> =
fun f -> f x
member inline this.ReturnFrom(x: Async<'T>) = x
member inline this.Bind
@t0yv0
t0yv0 / ManualInversion.v
Created December 7, 2011 19:34
A simple Coq proof exercise.
(* Consider lists indexed by length. *)
Inductive List (t : Type) : nat -> Type :=
| Nil : List t 0
| Cons : forall n, t -> List t n -> List t (S n).
(* Every non-empty list has a head element. *)
Definition Head : forall t n, List t (S n) -> t.
intros t n list. inversion list. assumption. Defined.
type IList<'T> =
abstract member Tail : #IListConsumer<'T,'R> -> 'R
abstract member Head : 'T
abstract member IsEmpty : bool
and IListConsumer<'T,'R> =
abstract member Consume : #IList<'T> -> 'R
[<Struct>]
type Nil<'T> =
let rec loop : Async<unit> =
async {
let! req = HttpRequestParser.Parse buf read
let! resp = app.Handle req
let kA = isKeepAlive req
do! respond socket buf (setKeepAlive kA resp)
if isKeepAlive req then
return! loop
else
return ()
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain',
'Connection': 'keep-alive'});
res.end('Hello World\n');
}).listen(8080, "192.168.56.1");
console.log('Server running');
@t0yv0
t0yv0 / AsyncSockets.fs
Last active September 7, 2023 11:45
Asynchronous socket programming with F# async.
open System.Net.Sockets
type A = System.Net.Sockets.SocketAsyncEventArgs
type B = System.ArraySegment<byte>
exception SocketIssue of SocketError with
override this.ToString() =
string this.Data0
/// Wraps the Socket.xxxAsync logic into F# async logic.
(*
We start designing a representation for some functional language.
Keeping it simple, we use strings to represent identifiers (to be
changed later by something more fancy), define a few primitives,
constants, and finally expressions.
*)
type Id = string
[<JavaScript>]
let Factorial (n: int) =
let rec fac n acc =
match n with
| 0 | 1 -> acc
| n -> fac (n - 1) (acc * n)
fac n 1
@t0yv0
t0yv0 / generated.js
Created September 26, 2011 21:42
Illustrates a Firefox JavaScript engine bug - failure to process nested closures.
/// generated JavaScript for k=16
function bind(x,f) {return f(x)}
function test(x0) {
var k0;
k0 = function (x1) {
var k1;
k1 = function (x2) {
var k2;
k2 = function (x3) {
{-# OPTIONS -XDoRec -XDeriveDataTypeable #-}
module Example where
import Text.Earley
import Data.Dynamic
import Control.Applicative
data E = Nat Int
| Add E E deriving (Show, Typeable)