Introduction to Railway Oriented Programming
Ed Courtenay@edcourtenay
Ed Courtenay
Senior Engineer, Application Reliability Team
Inspiration for this
talk
• Scott Wlaschin
• F# for Fun and Profit -https://fsharpforfunandprofit.com/rop
Agenda
• So what are we talking about?
• F# demos
• How can these principles be applied to non-functional languages?
• C# demo
Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live.
http://wiki.c2.com/?CodeForTheMaintainer
What is Railway Oriented Programming?• A functional approach to error handling
• A technique for creating pipelines
• Push error handling to where it belongs
• Control flow over exceptions
Func<Doctor, Doctor>
Regenerate
F# Demo
'TInput Happy Path
'TOutput
Single Track Function
'TA 'TB 'TC 'TD
'TInput Happy Path
Sad Path
Result<'TSuccess, 'TFailure>
Switch Function
('a -> Result<'b, 'c>) -> Result<'a, 'b> -> Result<'b, 'c>
switchFunction twoTrackInput output
MapR
esu
lt<T
A, T
C>
Resu
lt<TA, TC
>
Success path
Failure path
TA TB
TC TC
MapR
esu
lt<l
on
g, s
trin
g>R
esult<
DateTim
e, strin
g>
Success path
Failure path
long DateTime
string string
Bind
Success path
Failure path
TA TB
TC TC
C# Demo
Code for this talk
• https://github.com/ecourtenay/ROP
Any Questions?