際際滷

際際滷Share a Scribd company logo
[F#] DEV LIFES LITTLE PLEASURES
Natallie Baikevich,
F# enthusiast and financial dev
@lu_a_jalla
WE ARE EVERYWHERE!
THINGS TO CARE ABOUT:

 Correctness

 Efficiency
 Safety

 Conciseness
 Simplicity
Based on true stories

WHEN YOU MEET THE REAL WORLD
#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, isnt it?
#1. THE CODE
let 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
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)
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
#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>.
The copy-paste is still around, so

GO FOR GENERICS!
WHAT MAKES A PROGRAM WRITE ITSELF?
#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);
#4. LOADING STOCK DATA FROM YAHOO
type Price = {

Oct 10, 2013

Date: DateTime

parse DateTime

33.31

parse float

33.38

parse float



Open: float



High: float
Low: float
Close: float
Volume: float

AdjClose: float option
}
MEET GENERIC RESTRICTIONS
let 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;

}
Good and Evil

MUTABILITY / STATE / MORE
[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.
WHEN CHOOSE WHAT
Immutable

Mutable

 Safe
 Simple to read

 Performance optimizations
(e.g. Dictionary vs Map)

 Efficient data structures:

 Interop

 Check fsharpx
 F# Core extensions (ExtCore)
HOW
 let for values
 types: tuples, records, DUs
 core & community collections and data structures (list, map, set
and more)
Scala

F#

 val

 let

 var

 let mutable
The billion dollar mistake and co

MAKE ILLEGAL STATES UNREPRESENTABLE
(NULL)

We have an Option to avoid nulls!
#8. SEND A MESSAGE
match status with
| Pending ->
| Verified -> doSomething()

Pattern matching &
Discriminated Unions FTW!

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 ->
Summing up

LET THE COMPILER HELP YOU!
WHATS 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
QUESTIONS?

More Related Content

[F#] Dev Life's Little Pleasures

  • 1. [F#] DEV LIFES LITTLE PLEASURES Natallie Baikevich, F# enthusiast and financial dev @lu_a_jalla
  • 3. THINGS TO CARE ABOUT: Correctness Efficiency Safety Conciseness Simplicity
  • 4. Based on true stories WHEN YOU MEET THE REAL WORLD
  • 5. #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, isnt it?
  • 6. #1. THE CODE let 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
  • 7. 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)
  • 8. 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
  • 9. #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>.
  • 10. The copy-paste is still around, so GO FOR GENERICS!
  • 11. WHAT MAKES A PROGRAM WRITE ITSELF?
  • 12. #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);
  • 13. #4. LOADING STOCK DATA FROM YAHOO type Price = { Oct 10, 2013 Date: DateTime parse DateTime 33.31 parse float 33.38 parse float Open: float High: float Low: float Close: float Volume: float AdjClose: float option }
  • 14. MEET GENERIC RESTRICTIONS let 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; }
  • 15. Good and Evil MUTABILITY / STATE / MORE
  • 17. #6. JUST CHECKING UPDATES .NET List [ResizeArray] foreach(var update in updatesList) { doSomethingWith(update); } System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
  • 18. WHEN CHOOSE WHAT Immutable Mutable Safe Simple to read Performance optimizations (e.g. Dictionary vs Map) Efficient data structures: Interop Check fsharpx F# Core extensions (ExtCore)
  • 19. HOW let for values types: tuples, records, DUs core & community collections and data structures (list, map, set and more) Scala F# val let var let mutable
  • 20. The billion dollar mistake and co MAKE ILLEGAL STATES UNREPRESENTABLE
  • 21. (NULL) We have an Option to avoid nulls!
  • 22. #8. SEND A MESSAGE match status with | Pending -> | Verified -> doSomething() Pattern matching & Discriminated Unions FTW! 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 ->
  • 23. Summing up LET THE COMPILER HELP YOU!
  • 24. WHATS 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