Upload
natallie-baikevich
View
3.145
Download
5
Tags:
Embed Size (px)
Citation preview
Natallie Baikevich,
F# enthusiast and financial dev
@lu_a_jalla
[F#] DEV LIFE’S LITTLE PLEASURES
WE ARE EVERYWHERE!
THINGS TO CARE ABOUT:
• Correctness• Efficiency• Safety• Conciseness • Simplicity
WHEN YOU MEET THE REAL WORLD…Based on true stories
#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)
#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
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>.
GO FOR GENERICS!The copy-paste is still around, so
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);
[No common ancestor or interface! How would you save the layouts?]
#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
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;
}
MUTABILITY / STATE / MOREGood and Evil
#6. JUST CHECKING UPDATES
.NET List [ResizeArray]
foreach(var update in updatesList)
{
doSomethingWith(update);
}
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
• 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
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)
MAKE ILLEGAL STATES UNREPRESENTABLEThe billion dollar mistake and co
(NULL)
We have an Option to avoid nulls!
#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!
LET THE COMPILER HELP YOU!Summing up
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
QUESTIONS?