25
Natallie Baikevich, F# enthusiast and financial dev @lu_a_jalla [F#] DEV LIFE’S LITTLE PLEASURES

[F#] Dev Life's Little Pleasures

Embed Size (px)

Citation preview

Page 1: [F#] Dev Life's Little Pleasures

Natallie Baikevich,

F# enthusiast and financial dev

@lu_a_jalla

[F#] DEV LIFE’S LITTLE PLEASURES

Page 2: [F#] Dev Life's Little Pleasures

WE ARE EVERYWHERE!

Page 3: [F#] Dev Life's Little Pleasures

THINGS TO CARE ABOUT:

• Correctness• Efficiency• Safety• Conciseness • Simplicity

Page 4: [F#] Dev Life's Little Pleasures

WHEN YOU MEET THE REAL WORLD…Based on true stories

Page 5: [F#] Dev Life's Little Pleasures

#1. THE SPREADSHEET

How to gain by selling £17mln and buying $10mln?• Buy $10mln

• Sell £16mln

• Spot Rate = 1.6

• GBP/USD =1.5945

• RGL = 88000

Pretty nice, isn’t it?

[Something you don’t believe can happen until you see it ]

RGL = amount * (spot rate – exch rate)

Page 6: [F#] Dev Life's Little Pleasures

#1. THE CODElet buyAmount, sellAmount = 10000000.0, 16000000.0

let spotRate = sellAmount / buyAmount // 1.6

let exchRate = 1.5945

let rgl amount spotRate exchRate =

amount * (spotRate - exchRate)

rgl sellAmount spotRate exchRate |> printfn "%A"

// 88000.0

Page 7: [F#] Dev Life's Little Pleasures

UNITS OF MEASURE[<Measure>] type USD

[<Measure>] type GBP

let buyAmount, sellAmount = 10000000.0<USD>, 16000000.0<GBP>

let spotRate = sellAmount / buyAmount

let exchRate = 1.5945<USD/GBP>

let rgl amount spotRate exchRate =

amount * (spotRate – exchRate)

Page 8: [F#] Dev Life's Little Pleasures

UNITS OF MEASURE - FIXED[<Measure>] type USD

[<Measure>] type GBP

let buyAmount, sellAmount = 10000000.0<USD>, 16000000.0<GBP>

let spotRate = buyAmount / sellAmount

let exchRate = 1.5945<USD/GBP>

let rgl amount spotRate exchRate =

amount * (spotRate – exchRate)

// -15512000.0

Page 9: [F#] Dev Life's Little Pleasures

#2. THE OPPORTUNITIES

The same trick works for yield quotes! Get a 100 times greater coupon. Or smaller. Whatever.

• 1 = 100%

• 1 % = 100 bp

… Or add <pct> and <bp>.

Page 10: [F#] Dev Life's Little Pleasures

GO FOR GENERICS!The copy-paste is still around, so

Page 11: [F#] Dev Life's Little Pleasures

WHAT MAKES A PROGRAM WRITE ITSELF?

Page 12: [F#] Dev Life's Little Pleasures

#3. THE BOILERPLATE

DevExpress layouts: Save/Restore• DockManager:

public void SaveLayoutToStream(Stream stream);

public void RestoreLayoutFromStream(Stream stream);

• Grid:

public void SaveLayoutToStream(Stream stream);

public void RestoreLayoutFromStream(Stream stream);

[No common ancestor or interface! How would you save the layouts?]

Page 13: [F#] Dev Life's Little Pleasures

#4. LOADING STOCK DATA FROM YAHOO

type Price = {

   Date: DateTime

   Open: float

    High: float

    Low: float

    Close: float

    Volume: float

  AdjClose: float option

}

parse DateTime

parse float

parse float

“Oct 10, 2013“

“33.31”

“33.38”

let ok, res = {Type here}.TryParse str

Page 14: [F#] Dev Life's Little Pleasures

MEET GENERIC RESTRICTIONSlet inline tryParse str =

    let mutable res = Unchecked.defaultof<_>

    let ok = (^a: (static member TryParse: string * byref< ^a > -> bool) (str, &res))

    res

match s.Split ',' with

| [| date; o; h; l; c; v; adj |] ->

Some { Date = tryParse date;

           Open = tryParse o;

High = tryParse h;

}

Page 15: [F#] Dev Life's Little Pleasures

MUTABILITY / STATE / MOREGood and Evil

Page 16: [F#] Dev Life's Little Pleasures
Page 17: [F#] Dev Life's Little Pleasures

#6. JUST CHECKING UPDATES

.NET List [ResizeArray]

foreach(var update in updatesList)

{

doSomethingWith(update);

}

System.InvalidOperationException: Collection was modified; enumeration operation may not execute.

Page 18: [F#] Dev Life's Little Pleasures

• Performance optimizations (e.g. Dictionary vs Map)

• Interop

• Safe

• Simple to read

• Efficient data structures:

• Check fsharpx

• F# Core extensions (ExtCore)

WHEN CHOOSE WHAT

Immutable Mutable

Page 19: [F#] Dev Life's Little Pleasures

Scala

• val

• var

F#

• let

• let mutable

[TRUST ME IT’S NOT SOMETHING YOU WANT TO TYPE OFTEN]

HOW

• let for values• types: tuples, records, DUs• core & community collections and data structures (list, map, set

and more)

Page 20: [F#] Dev Life's Little Pleasures

MAKE ILLEGAL STATES UNREPRESENTABLEThe billion dollar mistake and co

Page 21: [F#] Dev Life's Little Pleasures

(NULL)

We have an Option to avoid nulls!

Page 22: [F#] Dev Life's Little Pleasures

#8. SEND A MESSAGE

match status with| Pending ->| Verified -> doSomething()

FS0025: Incomplete pattern matches on this expression. For example, the value '(_,Released)’

type Status =| Pending| Verified of User| Released of User * DateTime

match msg with | text, Verified user ->

Pattern matching & Discriminated Unions FTW!

Page 23: [F#] Dev Life's Little Pleasures

LET THE COMPILER HELP YOU!Summing up

Page 24: [F#] Dev Life's Little Pleasures

WHAT’S NEXT?

• The big ones: Type Providers

• For those who is crazy about types: F*

Getting Started:

• The F# Software Foundation: http://fsharp.org/

• Try F# in your browser: http://www.tryfsharp.org/

• Snippets: http://www.fssnip.net/

• Join Twitter discussions: #fsharp

Page 25: [F#] Dev Life's Little Pleasures

QUESTIONS?