From "F-What ?" to F#


Language intro + discovering powerful yet accessible features


birdmod (GitHub)

Presentation

CLI language of created by Microsoft Research

Maintained and improved by the F# Software Foundation

"F# is a mature, open source, cross-platform, functional-first programming language.
It empowers users and organizations to tackle complex computing problems with simple, maintainable and robust code"

mature : released in 2005, latest version is F# 4.1. MSDN docs available F#. Microsoft supports it as C#. Available since VS 2012 (at least)

functional-first : not limited to functional paradigm. Possible to mix to object oriented or procedural code

Syntax Basics

              
          (* binding *)
          let amount = 10
          
          (* declare function and conditional *)
          let incrementWithLimit number limit = 
            if number < limit then
              number + 1
            else
              limit
              
            
              
          (* reference & array & loop *)
          open System.IO
          
          let folders = Directory.GetDirectories @"C:\"
          for folder in folders do
            printfn "Folder found in C:\\ : %s" folder
            
          (* n-tuples *)
          let filmAndScore = ("inception", 8.8)
          let filmScoreYear = ("interstellar", 8.6, 2014)
              
            

Type inference

Compiler deduces types.
This information is not hidden. IDEs integrating F# provide it

              
          let isAwesome = true (* bool *)
          let increment n = n + 1 (* (int -> int) *)
          let increment n = n + 1. (* (float -> float) *)
              
            

Compiler notifies of ambiguous cases. We fix this by clarifying the expected type : type annotations

              
          (* ('a -> 'b) *)
          let getLength x = x.Length 
          
          (* (string -> int) *)
          let getLength (x:string) = x.Length 
          
          (* (float[] -> int) *)
          let getLength (x:float []) = x.Length 
              
            

FP: Immutability

Value is bound to the label, forever

Limits surprise effects by working with copies, thread safety

BTW System.String is immutable... and you're still alive

Do not see it as a negative point

FP : higher order

functions accepts or return other functions as parameters

              
          let sendEmail recipient recipientCheck = 
              recipientCheck recipient
              
            

FP : partial application

The power to build "pre configured" functions

              
          (* t_credentials -> t_query -> t_result *)
          let connectToDbThenExecute credentials query = 
            DB.connect credentials
            DB.exec query
          
          connectToDbThenExecute creds query1
          connectToDbThenExecute creds query2
          
          let executeQuery = connectToDbThenExecute creds 
          executeQuery query1
          executeQuery query2
              
            

Designing powerful types to model your domain

How do you represent a set of exclusive cases ?

                
            /// booleans
            var isUserAvailable = true

            /// 0 stands for rock, 1 for paper, 
            /// 2 for scissors.
            var player1Choice = 1 
            ///... until the days someone adds a dynamite case 
            /// and "explodes" in your code 
            /// Dynamite case introduced. 
            /// Effective against scissors and paper
            var player2Choice = 4 
                
              
                
            var isScissors = true;
            var isRock = false;
            var isPaper = false;
            var isDynamite = true;
                
              
                
            public enum TurnChoice 
            {
              Player1,
              Player2
            }
            /// ... has to evolve into ...
            public enum AdvancedTurnChoice
            {
              Player1Day,
              Player1Night,
              Player2Day,
              Player2Night,
            }
                
              

Sometimes, we have constraints or we change the design as if nothing happened and we hold limited data

Discriminated unions

~ Union in C

  • Exclusive set of cases
  • Holds data
  • Meaningful, understandable, IEasilyDebuggable
                
            type UserChoice = 
              | Paper
              | Scissors
              | Rock
            let user1Choice = Paper 

            (* so enums ? no because I did not write: *)
            type UserChoice = 
              | Paper = 0
              | Scissors = 1
              | Rock = 2
                
              
                
            type FamousCaseOfShapeModelization = 
              | Circle of uint32 (* ( ͡° ͜ʖ ͡°) *)
              | Square of Edge : uint32
              | Rectangle of uint32 * uint32
            let TimesSquare = Square(12)
                
              

Multiple DU usage

                
            type PlayingCardKind = 
              | Clubs | Spades | Diamonds | Hearts
            type PlayingCardNum = 
              | Ace | King | Queen | Jack | Two | Three | Four 
              | Five | Six | Seven | Eight | Nine | Ten
            type PlayingCard = 
              | PlayingCardNum * PlayingCardKind
            type Deck = PlayingCard []
                
              

We could use enums ... Now imagine card values as floats...

wrong/illegal states are not possible by design

Recursive DUs

                
            type BinaryTree = 
              | Node of 'NodeType * Tree * Tree
              | Leaf of 'NodeType
                
              

Pattern matching

if then else : conditional
This construct lets you branch between multiple expressions to execute with the ability to decompose or match input data

                  
              match data with
              | case 1 -> expression 1
              | ...
              | case N -> expression N
                  
                

One benefit is that missing cases are detected by the compiler.
DUs are supported so you will know at compile time that your design is broken

              
          match cardKind with
            | Clubs -> "♣"
            | Spades  -> "♠"
            | Diamonds -> "♦" (* no hearts, won't compile  *)
              
            

Data extraction is possible by creating a "pattern"

                
      let todayActivities = ["curling"; "swimming"; "rock climbing"]
      match todayActivities with
        | "curling"::remainingElements ->  "Excellent activity !"
        | ...
        
      type Meal = Breakfast | Lunch | Dinner
      let lunchAndPriceTuple = (Breakfast, 15) (* Meal*int *)

      let mealGossip tpl = 
      match mealTuple with
        | (Breakfast, price) when price > 8 -> "omg so pricey!" 
        | (Breakfast, price) -> "where did you buy it ?"
        | (Lunch, price) ... 
                
              

Another level than if/then/else (we could even replace them)

Improved readability

Type providers

What is the workflow when working with data of an external source ?

=>Read the doc, analyse response, create types, play with properties

How to work with data from external source in short cycles (schema changes ?) and with a need to be operational quickly ?

=>By using the same workflow ?

  • Read the doc
  • analyse response : documentation defines it
  • create type : if we know the structure, we know how to navigate inside

The concept is to generate on the fly the structures and properties to explore and use them.
We may miss information in the doc. Which tool could let us discover it ?

If the demo fails...

                
        [<LiteralAttribute>] (* compile time assignment *)
        let url = "https://jsonplaceholder.typicode.com/users"
        type LectureDataProvider = JsonProvider<url>
        let allUsers = LectureDataProvider.Load(url)
                
              

Conclusion

Wrap up

The difficulty is to set the cursor between FP and F# so some stuff is left

Not covered in this presentation because of time:

  • options
  • Measures
  • RECORDS
  • list comprehensions
  • composition & pipe operator
  • communication with C#
  • ...

Questions ?