Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to F# Kit Eason – –

Similar presentations


Presentation on theme: "Introduction to F# Kit Eason – –"— Presentation transcript:

1 Introduction to F# Kit Eason - @kitlovesfsharp – www.kiteason.com – kit.eason@gmail.com

2 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  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

3 let it be  Use ‘let’ to declare functions and values let a = 1 let Add x y = x + y let CircleArea r = System.Math.PI * r ** 2.  Use ‘let’ to declare functions and values let a = 1 let Add x y = x + y let CircleArea r = System.Math.PI * r ** 2.

4 Point of no return (-statements)  The return value of the function is the last value calculated (no ‘return’ statement) let StrToDoubleDefault str def = let ok, result = System.Double.TryParse str if ok then result else def  The return value of the function is the last value calculated (no ‘return’ statement) let StrToDoubleDefault str def = let ok, result = System.Double.TryParse str if ok then result else def

5 Yeah, it’s magic  Types are inferred – at design time

6 The bonfire of the parentheses  Lexical scope is determined by indentation  Argument lists don’t have brackets (normally) let StrToDoubleDefault str def = let ok, result = System.Double.TryParse str if ok then result else def  Lexical scope is determined by indentation  Argument lists don’t have brackets (normally) let StrToDoubleDefault str def = let ok, result = System.Double.TryParse str if ok then result else def

7 |> operator makes you :)  “Forward pipe”  Takes output from previous operation…  …and feeds it as input to the next  “Forward pipe”  Takes output from previous operation…  …and feeds it as input to the next let AbsSquareRoot (n : double) = n |> abs |> sqrt // sqrt(abs(n))

8 F# Interactive (FSI) > let add x y = x + y;; val add : x:int -> y:int -> int > add 3 4;; val it : int = 7 > > let add x y = x + y;; val add : x:int -> y:int -> int > add 3 4;; val it : int = 7 >  Use F# Interactive to define and try out functions

9 Array module  Provides operations that work on whole arrays  Similar to LINQ  Provides operations that work on whole arrays  Similar to LINQ let CircleArea r = System.Math.PI * r ** 2. let circles = [|3.; 5.2; 9.9|] let TotalArea radii = radii |> Array.sumBy (fun r -> CircleArea r)

10 Array.filter  Another operation from the Array module  Returns an array with just the elements that pass the test  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|] let TotalCredits txns = txns |> Array.filter (fun t -> t > 0.) |> Array.sum


Download ppt "Introduction to F# Kit Eason – –"

Similar presentations


Ads by Google