Skip to content

Instantly share code, notes, and snippets.

View davidgrenier's full-sized avatar

David Grenier davidgrenier

View GitHub Profile
@davidgrenier
davidgrenier / tolerance.fsx
Created February 5, 2014 22:46
Extensible Pattern Matching
//Hi Julie, I liked your article and couldn't resist offering some possible improvements. I don't know
//what all the formulae mean but F#'s pattern matching is pretty powerful but also extensible and can
//be used to greatly improve readability and your chances of getting the code right in the first go.
//
//Below you can see the definition of two Active Pattern to extend F#'s pattern matching with a way
//to extract information from simple values, in this case floats.
let (|GreaterThan|_|) num = function
| value when value > num -> Some ()
| _ -> None
@davidgrenier
davidgrenier / Erlang-Rx.fs
Created March 7, 2014 15:50
Erlang Ring Rx new
#r @"C:\Projects\Lib\System.Reactive.Interfaces.dll"
#r @"C:\Projects\Lib\System.Reactive.Core.dll"
#r @"C:\Projects\Lib\System.Reactive.Linq.dll"
open System
open System.Linq
open System.Reactive.Subjects
open System.Reactive.Linq
let run n m =
let inline parse str = (^a: (static member Parse: string -> ^a) (str))
let inline tryParse str =
let mutable value = Unchecked.defaultof<_>
let result = (^a: (static member TryParse: string * byref< ^a > -> bool) (str, &value))
result, value
(tryParse "3": bool * int)
(tryParse "2.3": bool * float)
(tryParse "2.3": bool * decimal)
let cell init =
MailboxProcessor.Start(fun inbox ->
let rec loop n =
async {
let! result = inbox.Receive()
printfn "Received %d" result
return! loop result
}
loop init
#r "System.Management"
open System.Management
do
use ma = new ManagementObjectSearcher("Select * from Win32_Processor")
for item in ma.Get() do
try
printfn "%A" item.["NumberOfCores"]
with _ -> ()
#if INTERACTIVE
let (++) a b = System.IO.Path.Combine(a, b)
let source = __SOURCE_DIRECTORY__ ++ __SOURCE_FILE__
let args = sprintf "--platform:x86 %s -o:%s" source <| source.Replace("fs", "exe")
System.Diagnostics.Process.Start(System.AppDomain.CurrentDomain.BaseDirectory ++ "fsc", args)
#endif
let time f x =
let ts = System.Diagnostics.Stopwatch.StartNew()
let result = f x
[<Inline "$v.toString(16)">]
let toHex (v: int) = X<_>
let guid() =
let rx = EcmaScript.RegExp("[xy]", "g")
EcmaScript.String("xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx").Replace(rx, As (fun c ->
let r = EcmaScript.Math.Random() * 16.0 |> int
let v =
type T = T with
static member (<@>) (x, y: int) = x * y
static member (<@>) (x, y: float) = x * int y
let inline helper (_: ^o) (left: ^t) (right: ^u) = ((^o or ^t): (static member (<@>): ^t * ^u -> int) (left, right))
let inline (<@>) left right = helper T left right
1 <@> 2
1 <@> 2.3
#r @"C:\Projects\Streams\src\Streams.Core\bin\Release\Streams.Core.dll"
open Nessos.Streams.Core
let data =
[|
let rnd = System.Random().NextDouble >> (*) 1e3
for x in -10.0 .. 10.0 do
yield System.DateTime.Now.AddDays (rnd())
|]
@davidgrenier
davidgrenier / NTBreakpoint.js
Created September 16, 2014 18:31
Conditional Breakpoint for javascript
var objUserInfo = new ActiveXObject("WScript.network");
if (objUserInfo.UserName == "dgrenier")
debugger;