Skip to content

Instantly share code, notes, and snippets.

View dmitry-a-morozov's full-sized avatar

Dmitry Morozov dmitry-a-morozov

View GitHub Profile
open System.IO
open System.Net
let wc = new WebClient()
let trainingSetFile = Path.Combine( __SOURCE_DIRECTORY__, "trainingSet.csv")
File.WriteAllText(trainingSetFile,
contents = wc.DownloadString("http://brandewinder.blob.core.windows.net/public/trainingsample.csv")
)
@dmitry-a-morozov
dmitry-a-morozov / gist:6370649
Created August 28, 2013 20:13
Euler project. Problem #54. F#. More Clarity Of Intent.
//TYPES
//In the card game poker...
type Card = Face * Suit
and Suit = Spades | Diamonds | Clubs | Hearts
//The cards are valued in the order:
@dmitry-a-morozov
dmitry-a-morozov / gist:6379684
Created August 29, 2013 15:36
SqlCommandTypeProvider usage example.
#r "bin/Debug/SqlCommandTypeProvider.dll"
open FSharp.NYC.Tutorial
[<Literal>]
let connectionString="Data Source=.\SQLExpress;Initial Catalog=AdventureWorks2012;Integrated Security=True"
type QueryPersonInfo = SqlCommand<"SELECT * FROM dbo.ufnGetContactInformation(@PersonId)", connectionString>
@dmitry-a-morozov
dmitry-a-morozov / gist:6701202
Last active December 23, 2015 22:09
Using Single-Case Active Patterns in real application. Parsing CSV files with possible formatting errors and bogus data. Pay attention to implementation of (|OrderStatusLine|_|), (|AccountPnlLine|_|) and (|PositionPnlLine|_|).
module Parse =
let inline tryParse<'T when ^T : (static member TryParse: string * ^T byref -> bool)> s =
let mutable x = Unchecked.defaultof<_>
if (^T: (static member TryParse: string * ^T byref -> bool) (s, &x)) then Some x else None
let (|Int|_|) = tryParse<int>
let (|Int64|_|) = tryParse<int64>
let (|Decimal|_|) = tryParse<decimal>
let (|DateTime|_|) = tryParse<DateTime>
@dmitry-a-morozov
dmitry-a-morozov / gist:7679567
Created November 27, 2013 17:22
Why out ref will be an issue
namespace Test
type MyType =
static member AsyncWithRefParams(i : int byref) =
async {
i <- 5
}
module Program =
[<EntryPoint>]
@dmitry-a-morozov
dmitry-a-morozov / gist:7994192
Created December 16, 2013 20:52
WinForms + Async
open System
open System.Net
open Microsoft.FSharp.Control.WebExtensions
open System.Windows.Forms
open System.Threading
let form = new Form()
let text = new Label()
let button = new Button()
@dmitry-a-morozov
dmitry-a-morozov / gist:afd85a24579163bc0464
Last active August 29, 2015 14:03
F# attach interface
open System
let getComparableUri uri = {
new Uri( uri)
interface IComparable<string> with
member this.CompareTo other =
compare (string this) other
}
let url = "http://news.google.com/"
module Symbology
open System
let yahoo symbol =
use wc = new Net.WebClient()
let yahoo = "http://download.finance.yahoo.com/d"
let uri = sprintf "%s/quotes.csv?s&f=nl1" yahoo symbol
wc.DownloadString(uri)
.Split([| "\n\r" |], StringSplitOptions.RemoveEmptyEntries)
@dmitry-a-morozov
dmitry-a-morozov / gist:04db553a31b5cddf8849
Created February 26, 2015 19:23
SqlCommandProvider static factory
open FSharp.Data
type Get42 = SqlCommandProvider<"SELECT 42", "Data Source=.;Integrated Security=True;", SingleRow = true>
Get42.Create().Execute()
@dmitry-a-morozov
dmitry-a-morozov / gist:140e1e37d7998eadcece
Created April 1, 2015 23:10
McCaffrey’s column in MSD
open System
let sigmoid z = 1.0 / (1.0 + exp (- z))
let logistic (weights:float[]) (features:float[]) =
let sumProd = (weights, features) ||> Array.map2 (*) |> Array.sum
sigmoid sumProd
let makeAllData (numFeatures, numRows, seed) =