130 likes | 388 Views
Introduction to F#. Kit Eason - @ kitlovesfsharp – www.kiteason.com – kit.eason@gmail.com. F# in a nutshell. Microsoft first class supported language for .NET Supports OO and functional paradigms Compiles to CLI like C#, VB.Net First class citizen in VS2010 through VS2013
E N D
Introduction to F# Kit Eason - @kitlovesfsharp – www.kiteason.com – kit.eason@gmail.com
F# in a nutshell • Microsoft first class supported language for .NET • Supports OO and functional paradigms • Compiles to CLI like C#, VB.Net • First class citizen in VS2010 through VS2013 • Open source, runs on Mono • Strongly typed
let it be • Use ‘let’ to declare functions and values let a =1 letAdd x y = x +y letCircleArea r =System.Math.PI* r **2.
Point of no return (-statements) • The return value of the function is the last value calculated (no ‘return’ statement) letStrToDoubleDefaultstrdef= let ok, result = System.Double.TryParse str if ok then result else def
Yeah, it’s magic • Types are inferred – at design time
The bonfire of the parentheses • Lexical scope is determined by indentation • Argument lists don’t have brackets (normally) letStrToDoubleDefaultstrdef= let ok, result = System.Double.TryParse str if ok then result else def
|> operator makes you :) • “Forward pipe” • Takes output from previous operation… • …and feeds it as input to the next letAbsSquareRoot (n : double) = n |> abs |>sqrt // sqrt(abs(n))
F# Interactive (FSI) • Use F# Interactive to define and try out functions > let add x y = x + y;; val add : x:int -> y:int -> int > add 3 4;; val it : int = 7 >
Array module • Provides operations that work on whole arrays • Similar to LINQ letCircleArea r =System.Math.PI* r **2. let circles = [|3.; 5.2; 9.9|] letTotalArea radii = radii |> Array.sumBy (fun r -> CircleArea r)
Array.filter • Another operation from the Array module • Returns an array with just the elements that pass the test let transactions = [|310.99; -52.80; 99.99; -128.30; 58.25|] letTotalCreditstxns= txns |>Array.filter (fun t -> t >0.) |>Array.sum