Skip to content

Instantly share code, notes, and snippets.

View dgfitch's full-sized avatar

Dan Fitch dgfitch

View GitHub Profile
open System.IO
type Walker(p) =
let root = new DirectoryInfo(p)
let root_path = root.FullName
let link (f:FileInfo) =
let relative_path = f.FullName.Replace(root_path, "").TrimStart('\\')
printfn "<p><a href='%s'>%s</a></p>" relative_path f.Name
(* First, a few dumb extensions on IEnumerable *)
type System.Collections.Generic.IEnumerable<'a> with
member xs.ChopHead = (Seq.head xs, Seq.skip 1 xs)
member xs.Cut f = (Seq.takeWhile f xs, Seq.skipWhile f xs)
(* Given a sequence, I want to partition it according to some fairly complex rules.
I'm using a pair of sequence expressions depending on what's at the head of the sequence.
Unfortunately, this code is prohibitively slow for the large inputs I'm sending it.
let FReplace (remove:string) (replace:string) (input:string) = input.Replace(remove, replace)
let Insert sql s1 s2 =
sql
|> FReplace "step1" s1
|> FReplace "step2" s2
(* OR, possibly more concise: *)
let PipeInsert s1 s2 =
let awesome (x:string) =
if x.Contains "awesome" then
5
else
0
(* everything is fine so far... *)
let mutable guyFuckedUp = false
(* In this weird case I need to write a stream but also keep track of how long ago a linefeed was written *)
member s.WriteStream (from:Stream) =
use reader = new StreamReader(from)
let mutable buffer = Array.zeroCreate<char>(BUFFER_LENGTH)
let mutable len = 0
while not reader.EndOfStream do
len <- reader.ReadBlock(buffer, 0, buffer.Length)
s.OutputWriter.Write(buffer, 0, len)
(* Now, walk backwards through the buffer looking for linefeeds *)
let set = Set.ofList
let map = Map.ofList
let w = set [1; 2; 2; 5]
let x = map [("foo", 1); ("bar", 2)]
let (~~) = set
let (!!) = map
let y = ~~ [1; 2; 2; 5]
xUnit.net console test runner (32-bit .NET 4.0.30319.1)
Copyright (C) 2007-9 Microsoft Corporation.
xunit.dll: Version 1.5.0.1479
Test assembly: e:\svn\dtc\Tests\UnitTests\bin\Local\UnitTests.dll
..................................................................................................................................................................FFFFFFF......F..FF.FFF.F...................................................................................................................................................................................................................................
Total tests: 413, Failures: 14, Skipped: 0, Time: 5.457 seconds
let f x = sprintf "[%A]" x
let g x = sprintf "{%A}" x
(* So you have two functions (string -> string)
Now, you want to do let result = f g "hi"
However, f doesn't take a function string -> string, it takes a string.
So you need to tell the compiler about your precedence.
Normal "idiomatic" ways: *)
(*
A parser needs to compress input as it receives it,
but it may receive the characters in small chunks.
How does the .NET System.IO.Compression namespace
deal with small chunks? Let's see the worst case!
*)
let streamToString (x:MemoryStream) =
let x_X = (+) "ouch"
let (---) x y = x + " don't" + y
let (!^-^) x = " punch" + x
let Q = " face"
let O = " please"
let emoticon = (x_X) O---(!^-^Q)