Skip to content

Instantly share code, notes, and snippets.

View davidgrenier's full-sized avatar

David Grenier davidgrenier

View GitHub Profile
let asyncDownload1 (url : string) =
let req = HttpWebRequest.Create url
req.BeginGetResponse(fun ar ->
use resp = req.EndGetResponse(ar)
use stream = resp.GetResponseStream()
let result = ref [||]
let buffer = Array.zeroCreate 8192 : byte []
let rec readAll () =
stream.BeginRead(buffer, 0, buffer.Length, fun ar2 ->
match stream.EndRead ar2 with
@davidgrenier
davidgrenier / seqYield vs functions.fs
Created July 4, 2012 17:39
seqYield vs functions
seq {
for x in elements do
if x % 3 = 0 then
yield x + 12
}
// instead do:
elements
|> Seq.filter (fun x -> x % 3 = 0)
@davidgrenier
davidgrenier / BlockQuote.fs
Created October 8, 2012 15:27
Recursive descent parser using F# Active Patterns compiled to JavaScript with WebSharper
open IntelliFactory.WebSharper
open IntelliFactory.WebSharper.Html
open Server.Tonys
[<JavaScript>]
let string chars = System.String (chars |> List.rev |> List.toArray)
[<JavaScript>]
let rec (|Eval|) = function
| Normal (c :: rest) ->
open System
type Message=
| GetToken of AsyncReplyChannel<IDisposable>
| Release
let start count =
let agent =
MailboxProcessor.Start(fun inbox ->
let token =
open System.Text.RegularExpressions
type Clip = System.Windows.Forms.Clipboard
[<System.STAThread>]
do
Regex.Matches(Clip.GetText(), @"\d+", RegexOptions.Singleline)
|> Seq.cast<Match>
|> Seq.map (fun x -> x.Value)
|> Seq.distinct
|> String.concat ","
#r "System.Data.Linq"
#r "FSharp.Data.TypeProviders"
open Microsoft.FSharp.Linq
open Microsoft.FSharp.Data.TypeProviders
type Company = SqlDataConnection<ConnectionString = @"Server=(localdb)\Projects;Database=Company;Integrated Security=SSPI">
let db = Company.GetDataContext()
let datum = "toalla"
let width = 99
System.String(datum |> Seq.truncate width |> Seq.toArray)
|> sprintf "%-*s" width
let trySwap (source: obj ref) replacement =
let before = !source
let result = System.Threading.Interlocked.CompareExchange(source, replacement, before)
System.Object.ReferenceEquals(before, result)
@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 add3times2minus4 =
let add3 x = x + 3
let times2 x = x * 2
let minus4 x = x - 4
[
add3
times2
minus4
] |> Seq.reduce (>>)