Skip to content

Instantly share code, notes, and snippets.

View t0yv0's full-sized avatar

Anton Tayanovskyy t0yv0

View GitHub Profile
@t0yv0
t0yv0 / gist:192353
Created September 23, 2009 22:39
another take on F# monads
module Monad =
type M<'T,'B when 'B :> Sig<'B>> =
interface end
and Sig<'B when 'B :> Sig<'B>> =
abstract member Return<'X> : 'X -> M<'X,'B>
abstract member Bind<'X,'Y> :
M<'X,'B> -> ('X -> M<'Y,'B>) -> M<'Y,'B>
class functional_point y =
object
val x = y
method get_x = x
method move d = {< x = x + d >}
end
#let p = new functional_point 7;;
val p : functional_point = <obj>
let MapReduce (map: 'T1 -> 'T2)
(reduce: 'T2 -> 'T2 -> 'T2)
(zero: 'T2)
(inputs: seq<'T1>) =
let ( >>= ) x f = async.Bind(x, f)
Async.FromContinuations <| fun (ok, no, _) ->
let n = Seq.length inputs
let reducer =
MailboxProcessor.Start <| fun inbox ->
let rec loop n s =
type View<'T1,'T2> =
{
Get : 'T1 -> option<'T2>
Set : 'T2 -> 'T1 -> 'T1
}
static member Find() =
let t1 = typeof<'T1>
let t2 = typeof<'T2>
if R.IsRecord t1 then
open System.Collections.Generic
[<Sealed>]
type Channel<'T>() =
let sync = obj ()
let readers = Queue<Future<_>>()
let writers = Queue<_>()
member this.CanRead : bool =
lock sync <| fun () -> writers.Count > 0
# Downloads a file from a given URL to the current directory.
function wget($url)
{
$path = $url.Substring($url.LastIndexOf('/') + 1)
$dir = pwd
$location = [System.IO.Path]::Combine($dir, $path)
$client = new-object System.Net.WebClient
$client.DownloadFile($url, $location);
}
module Specialization =
type ITyped<'T1,'T2> =
abstract member Run<'T3> : 'T1 -> 'T2
type private Function<'T1,'T2> = delegate of 'T1 -> 'T2
let Specialize<'T1,'T2> (algorithm: ITyped<'T1,'T2>) =
let aT = typeof<ITyped<'T1,'T2>>
let def = aT.GetMethod("Run").GetGenericMethodDefinition()
let dT = typeof<Function<'T1,'T2>>
let rec fac = function
| 0 -> 1
| n -> fac (n - 1) * n
def readFile(path):
f = open(path, 'r')
r = map(lambda x: x.strip(), f.readlines())
f.close()
return r
animals = readFile('animals.txt')
@t0yv0
t0yv0 / List.v
Created October 24, 2010 15:00
The only list of length zero is the empty list.
Require Import Coq.Lists.List.
(** The only list of length zero is the empty list. *)
Lemma length_zero : forall (a : Type) (x : list a),
length x = 0 <-> x = nil.
Proof.
intros; split; intro.
induction x; auto; discriminate.
rewrite H; compute; auto.
Qed.