Skip to content

Instantly share code, notes, and snippets.

View davidgrenier's full-sized avatar

David Grenier davidgrenier

View GitHub Profile
@davidgrenier
davidgrenier / StructuralTyping.fsx
Created August 4, 2013 18:10
StructuralTyping
let inline itemsProp arg = (^a : (member Items : ItemCollection) arg)
let inline withBackground brush e = (^a : (member set_Background : Brush -> unit) (e, brush))
let genString =
let rand = System.Random()
let chars = [|'A'..'Z'|] |> Array.append [|'a'..'z'|]
fun n -> System.String (Array.init n (fun _ -> chars.[rand.Next(0, chars.Length)]))
genString 10
let transpose3 = function
| [||] -> [||]
| a ->
Array.maxBy Array.length a
|> Array.Parallel.mapi (fun i _ ->
Array.filter (fun js -> Array.length js > i) a
|> Array.map (fun sub -> sub.[i])
)
#load @"C:\Bib\Projects\FSharpChart\F#\scripts\FSharpChart.fsx"
#r @"C:\Bib\Projects\FSharp.Data\src\bin\Release\FSharp.Data.dll"
open FSharp.Data
open MSDN.FSharp.Charting
type Types = CsvProvider<"""C:\Users\dgrenier\Desktop\race_data.csv""">
let data = Types.Load """C:\Users\dgrenier\Desktop\race_data.csv"""
@davidgrenier
davidgrenier / ReadersVsPopulation.fsx
Created June 2, 2013 20:43
Readers of Sergey Tihon's Blog vs Country population using FSharp.Data WorldBank type provider.
#r @"C:\Bib\Projects\FSharp.Data\src\bin\Release\FSharp.Data.dll"
open FSharp.Data
let data = WorldBankData.GetDataContext()
let ``Sergey's data`` =
[
"United States", 11823
"United Kingdom", 4256
@davidgrenier
davidgrenier / Run.fsx
Created June 2, 2013 19:50
Identify a run of consecutive characters.
let rec (|Run|_|) n c =
let rec identicals n = function
| rest when n = 0 -> Some rest
| a :: rest when a = c && n > 0 -> identicals (n - 1) rest
| _ -> None
identicals n
let (|ModZero|_|) d v =
match v % d with
| 0 -> Some ()
| _ -> None
let fizzBuzz = function
| ModZero 15 -> printfn "fizzbuzz"
| ModZero 3 -> printfn "fizz"
| ModZero 5 -> printfn "buzz"
| i -> printfn "%i" i
let add3times2minus4 =
let add3 x = x + 3
let times2 x = x * 2
let minus4 x = x - 4
[
add3
times2
minus4
] |> Seq.reduce (>>)
@davidgrenier
davidgrenier / Plane vs Helicopter.fsx
Last active December 17, 2015 21:29
(a) A plane at velocity 100m/s in still air is flying from station P due North to station Q. A helicopter at velocity 50m/s in still air is flying from P due East to station C. If wind at velocity 20m/s blows due East when the plane and helicopter are flying, calculate the: (i) Resultant velocities of the plane and helicopter (ii) Velocity of th…
[<Measure>]
type m
[<Measure>]
type s
type Vector<[<Measure>] 't> =
{ X: float<'t>; Y: float<'t> }
with
let trySwap (source: obj ref) replacement =
let before = !source
let result = System.Threading.Interlocked.CompareExchange(source, replacement, before)
System.Object.ReferenceEquals(before, result)