F# for Functional Enthusiasts Jack Fox jackfoxy.com craftyThoughts @foxyjackfox Bibliography jackfoxy.com/ Fsharp_functional_enthusiasts_bibliography Sample Code https://gist.github.com/jackfoxy/6679238 https:// github.com/jackfoxy/CrockSolution https://github.com/jackfoxy/TestSandbox 2012 annular eclipse observed by the author from Sugarloaf Peak, Lake Shasta
Slides for my talk to the Bay Area Clojure User Group. Unlike most "entry level" F# talks, this presentation assumes some familiarity with functional programming concepts, although I think even folks unfamiliar with functional programming will find the content and demonstrations interesting. The talk includes a survey of the state of the F# community and community-driven (open source) projects.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
let one' = one + zero let x' = one <<< (bitSize - 1) x' ||| one'
bitSize:int -> zero: ^a -> one: ^a -> ^a when ^a : (static member ( + ) : ^a * ^a -> ^a) and ^a : (static member ( <<< ) : ^a * int32 -> ^a) and ^a : (static member ( ||| ) : ^a * ^a -> ^a)
// Base Type Constraint
type Class1<'T when 'T :> System.Exception> …
// Interface Type Constraint
type Class2<'T when 'T :> System.IComparable> …
// Comparison constraint
type Class10<'T when 'T : comparison> …
// Member constraint with property
type Class6<'T when 'T : (member Property1 : int)> …
Constraints on Type Definitions
[<Measure>] type m // Distance, meters.
[<Measure>] type mile // Distance, miles.
[<Measure>] type s // Time, seconds.
[<Measure>] type h // Time, hours.
let distance1 = 100.0<m>let distance2 = 250.0<mile>
let speedMetersPerSec = distance1 / 10.0<s> // float<m/s>
let speedMPH = distance2 / 5.0<h> // float<mile/h>
let x = distance1 + distance2 //will not compile
Units of MeasureDesign Time Type Checking
So far…Type Providers: strongly typed external data
Strong typing and type inference
Type constraints
Units of Measure
Next up…Nuts and bolts programming
Vector
Eager Rose Tree
Vector
54321
Construct Deconstruct
Initial
Last
[ ]
empty
;;
type JsonValue = | String of string | Number of decimal | Float of float | Object of Map<string, JsonValue> | Array of JsonValue[] | Boolean of bool | Null
type 'a t = Node of color * 'a * 'a t * 'a t | Leaf
let balance = function | Black, z, Node (Red, y, Node (Red, x, a, b), c), d | Black, z, Node (Red, x, a, Node (Red, y, b, c)), d | Black, x, a, Node (Red, z, Node (Red, y, b, c), d) | Black, x, a, Node (Red, y, b, Node (Red, z, c, d)) ->
Node (Red, y, Node (Black, x, a, b), Node (Black, z, c, d))
| x -> Node x Source: http://fsharpnews.blogspot.com/2010/07/f-vs-mathematica-red-black-trees.html
Computational ExpressionsOne syntactic sugar to rule them all
( monad, monoid, applicative functor )
seq {for n in 1..10 do yield n yield n * 2} seq {for n in 1..10 do yield (n, n * 2)}
Sequence Generators
Computational ExpressionsRecursive Generators
let rec listFiles dir = seq { yield! Directory.GetFiles(dir)
let htmlPage = query { for content in db.tbContentLib do
where (content.companyID.Value > 0 and content.isHtml) join template in db.tbTemplate on (content.templateID.Value = template.templateID) select (content.styleID, template.title, template.body) }