+ All Categories
Home > Technology > F# Presentation

F# Presentation

Date post: 13-Dec-2014
Category:
Upload: mrkurt
View: 1,637 times
Download: 2 times
Share this document with a friend
Description:
 
22
What the… F# A language you can brag about
Transcript
Page 1: F# Presentation

What the… F#A language you can brag about

Page 2: F# Presentation

Why you should care

Functional programming The new hotness

Next “big” .NET language CTP this summer

Page 3: F# Presentation

F# Background

OCaml offspring Created by MS Research Mixed mode language

Functional OO Imperative (if you must)

Statically typed

Page 4: F# Presentation

Variables… sort of

let x = 7let s = "kurt"let pi = 3.142

Yay! Type inference ...also, immutable So, really just

“values”

Page 5: F# Presentation

Lists are cool

let l = ['a';'b';'c';]// char list or list<char>

let digits = [0..9]// int list

Singly linked list Still immutable!

Page 6: F# Presentation

Sequences

let s = {1..10}// seq[1;2;3;...]

IEnumerable<T> Useful for

computing unbounded lists

Page 7: F# Presentation

Tuples – cliques for values

let me = ("kurt", 6.0)// string * float let point3d = (0,0,0)// int * int * int

Typed groups of values

Can “hold” anything

Basically awesome

Page 8: F# Presentation

Function types

let add x y = x + y// int -> int -> int

let eight = add 3 5// eight = 8

Like any other value

More type inference

Returns last expression

Page 9: F# Presentation

Even More Type Inference

let group x y = (x,y)// 'a -> 'b -> 'a * 'b

Look ma! Generics! Auto-generalization

Page 10: F# Presentation

Recursive Functions

let rec fib n = if n < 2 then n else (fib (n - 1)) + (fib (n - 2))

Explicitly recursive Tail recursion

Page 11: F# Presentation

Option types

type name = stringtype number = inttype date = System.DateTimetype meeting = | Personal of name * date | Phone of number * date let review = Personal("Jasmine",System.DateTime.Now)let call = Phone(8675309, System.DateTime.Now)

Page 12: F# Presentation

Pattern Matching

let what_to_do (m : meeting) = match m with | Personal(name,date) -> printfn "Meeting with %s at %A" name date | Phone(phone, date) -> printfn "Call %A at %A" phone date

Page 13: F# Presentation

The “fun” in functional

Functions are basic units of a program

We can mix them up in interesting ways Less OO design patterns!

“Cheap” functions are good Code is more expressive

Page 14: F# Presentation

Partial function application

let add3 = add 3// int -> int

let add x y = x + y// int -> int -> int

Page 15: F# Presentation

Pipelining operator

let (|>) x f = f x// 'a -> ('a -> 'b) -> 'b

let even x = x % 2 = 0// int -> boollet numbers = [1..20]// int listlet evens = numbers |> List.filter even// int list

Page 16: F# Presentation

Useful List functions

List.map// ('a -> 'b) -> // 'a list -> // 'b list[1..10] |> List.map add3// [4;5;6;..;13]

List.fold_left// ('b -> 'a -> 'b) ->// 'b ->// 'a list ->// 'b

[1..10] |> List.fold_left (fun acc x -> acc + x) 0// 55

Page 17: F# Presentation

Solving a problem

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?

- Project Eulerprojecteuler.net

Page 18: F# Presentation

Imperative Programming

“Mutable” keyword For real variables

There are imperative loops Try not to use it too much

Page 19: F# Presentation

OO Programming

F# can be OO Classes Interfaces Inheritance

Cross language work “Hide” implementation

Page 20: F# Presentation

Creating a Class!

type OrderLine(n:string, q:int, p:float) = let mutable currName = n let mutable currQuantity = q let mutable currPrice = p new (name, price) = OrderLine(name, 1, price) member x.Name with get() = currName and set name = currName <- name member x.SubTotal with get() = (Float.of_int quantity) * price member x.OneMore() = currQuantity <- currQuantity + 1 currQuantity

Page 21: F# Presentation

When to do what

Why functional programming? Easy to build incrementally Simplified testing Concurrency!

Imperative can be good too At times, it’s wickedly fast It’s what we know

Page 22: F# Presentation

The end


Recommended