4. F# WAS SO EASY TO PICK UP WE
WENT FROM COMPLETE NOVICES
TO HAVING OUR CODE IN
PRODUCTION IN LESS THAN A
WEEK.
O’Connor’s Online - Jack Mott
WE ALSO FOUND THAT IT WAS STRAIGHTFORWARD TO USE OUR NEW F# MODULE
FROM WITHIN OUR EXISTING C# CODE
5. THE F# CODE IS CONSISTENTLY
SHORTER, EASIER TO READ,
EASIER TO REFACTOR AND
CONTAINS FAR FEWER BUGS.
Kaggle
6. THE F# SOLUTION OFFERS US AN
ORDER OF MAGNITUDE
INCREASE IN PRODUCTIVTY
GameSys - Yan Cui, Lead Server Engineer
7. PROGRAMMING IN F# FEELS
LIKE WRITING OUT IDEAS
RATHER THAN CODE
Maria Gorinova
10. ▸Accepting functions as arguments (High order function)
▸Returning functions inside functions
▸Storing functions inside data structures
▸Shouldn’t cause mutation or side-effects (pure functions)
▸Behaviour based on arguments
▸Functions in FP are equivalent to objects in OO
FUNCTIONS AS FIRST CLASS CITIZEN
11. ▸States or values cannot be altered
▸Brings thread-safety
▸No mutable states shared amongst threads
▸No huge penalties when updating internal states
▸Creating new states from previous states
IMMUTABILITY
12. ▸Combination of 1+ of
▸values, functions, operators, constants
▸Expressions are evaluated
▸Produces
▸return value (e.g numerical)
▸an expression (e.g a function)
EXPRESSIONS
13. ▸When functions are calling themselves
▸Tail recursion when it’s the last expression of the function
▸No state required for recursion
▸Only read-only function arguments
▸Write-only return value
▸Function “iterates” in it’s own stack
▸Reduce memory usage on the stack
RECURSION
14. ▸Functions can be broken down in arguments
▸Currying uses a series of functions from a “root” function
▸Curried functions can be passed to high order functions
▸Example:
▸add x y = x + y
▸incrementBy10 y = add 10 + y
PARTIAL FUNCTION APPLICATION (CURRYING)
16. ▸Top-down & left-right
▸The location of functions and types matters
▸The location of source file in the project matters
▸Indentation is key in source files
FILE SYSTEM HIERARCHY
17. ▸An allocation followed by an assignment
▸When allocating values or functions to same symbol (variable)
▸It becomes shadowed - lost in space
▸Compiler infers type to value
LET BINDING
18. ▸Grouping of code
▸Can contain : modules, type definitions, values, constants
▸ Implemented as a static CLR class with static members.
▸Module can be defined as
▸Top level : Whole file (Don’t need to indent)
▸Locally (requires indentation)
MODULES
19. ▸Discriminated unions: Heterogenous data
▸Similar to the union type in C++ but each option as an case identifier
▸Equivalent to enumerations when cases have no value
▸type Shape = | Rectangle | Circle | Square
▸Records: Set of named values
▸type Point3D = { x : float; y : float; z : float }
▸A record can be updated with keyword with
▸Tuples: Set of unnamed and ordered values
▸int * int
CORE DATA STRUCTURES
20. ▸A pattern is a rule for transforming data
▸Data is decomposed when it’s being matched on a pattern to find a match
▸Possible to match on Tuple, Discriminated union, Records, collections, etc
▸match [something] with | pattern1 -> expression1
▸Compiler can see when you forget cases
▸_ is a wildcard pattern to ignore the remaining cases
PATTERN MATCHING
21. ▸If in F# are expressions, not statements like in C#
▸Each branch must return an expression
▸The else branch is optional when if returns no value (unit)
▸Expressions must be of the same type
▸Cannot return int in if and bool in else for instance
IF EXPRESSIONS
22. ▸Common collections in F# : List ( [] ) , Array ( [| |] ), Seq ( ses { } )
▸They basically have the same API
▸You can find functions such as
▸tryFind
▸length
▸map
▸Filter
▸min/max
▸sum
▸average
HIGH ORDER FUNCTIONS ON COLLECTIONS
23. ▸For loop
▸for I = 0 to 10 do …
▸for i = 10 downto 0 do ….
▸Foreach loop
▸for i in [0..100] do …
▸for i in [2..2..50] do …
LOOPING
24. ▸The pipe operators are syntactic sugar for chained method calls
▸Pipe-forward ( |> )
▸Lets you input an intermediate result onto the next function
▸[1..10] |> List.filter (fun integer -> integer % 2 = 0) (Result : [2;4;6;8;10])
▸ Pipe-backward ( <| )
▸Takes a function from the left and applies its return value/expression to the right
▸printfn “The value of 3 when tripled %d” <| tripleValue 3 vs
▸Printfn “The value of 3 when tripled %d” (tripleValue 3)
PIPELINE OPERATORS
25. ▸Bound values to a type
▸Operations can be made on values of same type
▸A unit of measure is a type
▸They can be given as arguments
▸Type inference
▸Values can be multiplied/divided and even converted
▸Not part of the .NET type system
▸Can’t be accessed by C#/VB
UNIT OF MEASURE
26. ▸Match a specific interface
▸Loaded types by compiler at design time
▸Set of of System.Type instances
▸Each type has a corresponding type in .NET
▸+ Compile-time check on data source
▸+ Provider can recognize change in data source and adapt
▸+ Uniform mechanism for data access
▸Examples: Entity Framework, JSON, XML, Yaml, CSV, HTML, R, Powershell
TYPE PROVIDERS
27.
28. REFERENCES
▸ Book : F# for C# developers
▸ Book : F# deep dives
▸ http://fsharpforfunandprofit.com
▸ https://github.com/ChrisMarinos/FSharpKoans#functional-koans---f
▸ http://foundation.fsharp.org/join
▸ https://en.wikibooks.org/wiki/F_Sharp_Programming