Skip to content

Instantly share code, notes, and snippets.

View davidgrenier's full-sized avatar

David Grenier davidgrenier

View GitHub Profile
#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 _ -> ()
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
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)
@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 =
@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 / PatternMatchingQuery.fsx
Created January 2, 2014 14:44
Pattern matching query
query {
for order in db.Orders do
let status =
match order.Status with
| 0 | 1 -> "Cancelled/Returned"
| _ -> "Valid"
where (status = "Valid")
select order
}
module Order =
type T = Order of int
let get (db: DataContext) id =
query {
for order in db.Orders do
where (order.Id = id)
select (Order id)
exactlyOne
}
type FileChange =
| Changed of string
| Deleted of string
| Created of string
| Renamed of string * string
let watch folder =
async {
use fsw = new System.IO.FileSystemWatcher(folder, "*.fsx", EnableRaisingEvents = true)
let (|Empty|_|) = function
| xs when Set.isEmpty xs -> Some ()
| _ -> None
[
"rule1", ["192"; "193"]
"rule2", ["193"; "194"]
"rule3", ["194"; "195"]
]
|> Seq.fold (fun data (rule, dst) ->
let data =
query {
for packet in communicator.ReceivePackets() do
// where (not packet.IsValid)
select packet
}