Skip to content

Instantly share code, notes, and snippets.

@mathias-brandewinder
Last active June 9, 2016 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathias-brandewinder/c0dd4a96e5baf1d2a681706147d6c769 to your computer and use it in GitHub Desktop.
Save mathias-brandewinder/c0dd4a96e5baf1d2a681706147d6c769 to your computer and use it in GitHub Desktop.
NDC Oslo Lab Hour

let square (x:float) = x * x

square 10.0

// fsharp.formatting // fsharp project scaffold

// 1. promote script to unit test // 2. promote script to documentation // 3. discard :)

type Boolean = | True | False

let printBool (bool:Boolean) = match bool with | True -> "TRUE" | False -> "FALSE"

printBool False

type Result = | Success of float | Failure of string

let safeDivide x y = if y = 0.0 then Failure("Diviside by Zero") else Success(x/y)

let printDivide x y = match (safeDivide x y) with | Success(value) -> printfn "%f" value | Failure(msg) -> printfn "%s" msg

type Tree = | Leaf of int | Branch of Tree * Tree

let tree = Branch( Leaf(1), Branch( Leaf(2), Leaf(3)))

let rec sum acc (tree:Tree) = match tree with | Leaf(v) -> acc + v | Branch(left,right) -> let leftT = sum acc left let rightT = sum acc right leftT + rightT

sum 0 tree

// xunit, nunit // fsunit -> less now // unquote

#I "./packages/" #r "unquote/lib/net40/unquote.dll"

open Swensen.Unquote

test <@ square 2.0 = 4.0 @>

#r "fscheck/lib/net45/fscheck.dll" open FsCheck

let square should always be positive x = square x >= 0.0

Check.Verbose square should always be positive

// -infinity * (-infinity) > 0.0

https://fsharpforfunandprofit.com/posts/property-based-testing/ https://fsharpforfunandprofit.com/posts/roman-numeral-kata/ http://blog.ploeh.dk/2015/02/23/property-based-testing-without-a-property-based-testing-framework/ http://brandewinder.com/2014/03/22/fscheck-and-xunit-is-the-bomb/

let x = 42

let add x y = x + y

add 1 2

let add42 = add 42

2 |> add 1 |> add 3 |> add 7

let root = SOURCE_DIRECTORY

[ 1;2;3;4;5 ]

open System open System.IO

let filePath = @"C:\Users\Mathias\Desktop\day-1\trainingsample.csv"

let data0 = File.ReadAllLines filePath

let data1 = data0 |> Array.map (fun line -> line.Split ',')

let data2 = data1.[1..]

let data3 = data2 |> Array.map (fun line -> line |> Array.map (fun item -> int item))

type Example = { Label:int; Pixels:int[] }

let data4 = data3 |> Array.map (fun line -> { Label = line.[0]; Pixels = line.[1..] })

let dropHeaders (arr:_[]) = arr.[1..]

let loadData path = path |> File.ReadAllLines |> Array.map (fun line -> line.Split ',') |> dropHeaders |> Array.map (fun line -> line |> Array.map (fun item -> int item)) |> Array.map (fun line -> { Label = line.[0]; Pixels = line.[1..] })

loadData filePath

type Boolean = | True | False // | Maybe

let printBool (bool:Boolean) = match bool with | True -> "TRUE" | False -> "FALSE"

type Result = | Success of float | Failure of string

let safeDivide x y = if y = 0.0 then Failure("NOPE") else Success(x/y)

let consumer x y = match (safeDivide x y ) with | Success(value) -> printfn "%f" value | Failure(msg) -> printfn "%s" msg

consumer 1. 0.

type Tree = | Leaf of int | Branch of Tree * Tree

let tree = Branch( Leaf(1), Branch( Leaf(2), Leaf(3)))

let rec sum acc (tree:Tree) = match tree with | Leaf(x) -> acc + x | Branch(left,right) -> acc + sum 0 left + sum 0 right

sum 0 tree

// Discriminated Union

// fsharp.org // F# for fun and profit // #fsharp / twitter

// @brandewinder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment