Skip to content

Instantly share code, notes, and snippets.

View FunctionalFirst's full-sized avatar

Daniel Robinson FunctionalFirst

View GitHub Profile
let rec AsyncAcceptMessages(client : WebSocket) =
async {
let! message = client.AsyncReadMessage
if(not(isNull message)) then
let s =
use reader = new StreamReader(message)
reader.ReadToEnd()
do
use writer = new StreamWriter(client.CreateMessageWriter(WebSocketMessageType.Text), Encoding.UTF8)
writer.Write s
let binarySearch value (array: 'T[]) =
let rec loop lo hi =
if lo <= hi then
let mid = lo + ((hi - lo) >>> 1)
match array.[mid] with
| x when x = value -> Some mid
| x when x < value -> loop (mid + 1) hi
| _ -> loop lo (mid - 1)
else None
loop 0 (array.Length - 1)
@FunctionalFirst
FunctionalFirst / gist:3132740
Created July 17, 2012 23:05
Escape from Zurg puzzle
open System
let MAX_TIME = 60
type Toy =
| Toy of string * int
type Direction =
| Start
| End
let groupsOf n (items: seq<_>) =
use e = items.GetEnumerator()
let rec loop i acc =
seq {
match i, acc, e.MoveNext() with
| 0, [], _ | _, [], false -> ()
| _, _, false ->
yield seq (List.rev acc)
| 0, _, true ->
yield seq (List.rev acc)
@FunctionalFirst
FunctionalFirst / gist:1781723
Created February 9, 2012 18:14
SQL Search Condition Parser
module SqlAst =
open System
type BinaryOp =
| Add
| Sub
| Mul
| Div
| Mod
let inline cosine n (x:^a) : ^a =
let one = LanguagePrimitives.GenericOne
let two = one + one
Seq.unfold (fun (twoIp1, t) -> Some(t, (twoIp1+two, -t*x*x/(twoIp1*(twoIp1+one))))) (one,one)
|> Seq.take n
|> Seq.sum