f# for c# devs - copenhagen .net 2015

31
F# FOR C# DEVS Phil Trelford, @ptrelford Copenhagen .Net @SimCorp, 2015

Upload: phillip-trelford

Post on 12-Apr-2017

547 views

Category:

Software


0 download

TRANSCRIPT

Page 1: F# for C# devs - Copenhagen .Net 2015

F# FOR C# DEVSPhil Trelford, @ptrelfordCopenhagen .Net @SimCorp, 2015

Page 2: F# for C# devs - Copenhagen .Net 2015

F#UNCTIONAL LONDONERS Founded Feb 2010 1050+ Members Meets every 2 weeks Topics include

Machine Learning Finance Games Web

http://meetup.com/fsharplondon

Page 3: F# for C# devs - Copenhagen .Net 2015

F# COMMUNITY WORLDWIDE

Page 4: F# for C# devs - Copenhagen .Net 2015

VISUAL F#

Page 5: F# for C# devs - Copenhagen .Net 2015

THE F IN F# IS FOR FUN!

Page 6: F# for C# devs - Copenhagen .Net 2015

HALO 3 WITH F# SKILLS

Page 7: F# for C# devs - Copenhagen .Net 2015

XBLA: PATH TO GO – F# AI

Page 8: F# for C# devs - Copenhagen .Net 2015

F# Statically Typed Functional First Object Oriented Open Source .Net language In Xamarin & Visual

Studio

Page 9: F# for C# devs - Copenhagen .Net 2015

C# + F# = BEST FRIENDSKaggle Testimonial“we have a large existing code base in C#, …getting started with F# was an easy decision.”“The F# code is consistently shorter, easier to read, easier to refactor and contains far fewer bugs.…we’ve become more productive.”

Source: http://fsharp.org/testimonials/

Page 10: F# for C# devs - Copenhagen .Net 2015

F# FOR PROFIT

Phil Trelford, @ptrelfordCopenhagen .Net @SimCorp, 2015

Page 11: F# for C# devs - Copenhagen .Net 2015

WHY F#? Time to Market Efficiency Correctness Complexity

Page 12: F# for C# devs - Copenhagen .Net 2015

TIME TO MARKETspeed development by 50 percent or more,

European IB

order of magnitude increase in productivity, GameSys

Page 13: F# for C# devs - Copenhagen .Net 2015

EFFICIENCYprocesses that used to require hours now take just

minutesGrange Insurance

performance is 10× better than the C++ that it replacesAviva

Page 14: F# for C# devs - Copenhagen .Net 2015

CORRECTNESSleads to virtually bug-free code,

Fixed Income

I am still waiting for the first bug to come in, E-On

Page 15: F# for C# devs - Copenhagen .Net 2015

Billion-dollar mistake

I call it my billion-dollar mistake. It was the invention of the null reference in 1965. […] I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.

Tony Hoare

I call it my billion-dollar mistake. It was the invention of the null reference in 1965. […] I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.

Tony Hoare

Page 16: F# for C# devs - Copenhagen .Net 2015

COMPLEXITY

everything becomes simple and clear when expressed in F#,

Byron Cook, Microsoft Research

Page 17: F# for C# devs - Copenhagen .Net 2015

COMPLEXITY: CYCLES IN SPECFLOW

Page 18: F# for C# devs - Copenhagen .Net 2015

COMPLEXITY: CYCLES IN TICKSPEC

Page 19: F# for C# devs - Copenhagen .Net 2015

LIVE DEMOS

Phil Trelford, @ptrelfordCopenhagen .Net @SimCorp, 2015

Page 20: F# for C# devs - Copenhagen .Net 2015

TYPES: LIGHT SYNTAXF#type Person(name:string,age:int) = /// Full name member person.Name = name /// Age in years member person.Age = age

C#public class Person{ public Person(string name, int age) { _name = name; _age = age; }

private readonly string _name; private readonly int _age;

/// <summary> /// Full name /// </summary> public string Name { get { return _name; } }

/// <summary> /// Age in years /// </summary> public int Age { get { return _age; } }}

Page 21: F# for C# devs - Copenhagen .Net 2015

DEPENDENCY INJECTION: LIGHT SYNTAXF#

type VerySimpleStockTrader

(analysisService:IStockAnalysisService,

brokerageService:IOnlineBrokerageService) =

member this.ExecuteTrades() =

() // ...

C#public class VerySimpleStockTrader { private readonly IStockAnalysisService analysisService; private readonly IOnlineBrokerageService brokerageService;

public VerySimpleStockTrader( IStockAnalysisService analysisService, IOnlineBrokerageService brokerageService) { this.analysisService = analysisService; this.brokerageService = brokerageService; }

public void ExecuteTrades() { // ... }}

Page 22: F# for C# devs - Copenhagen .Net 2015

UNIT TESTINGF# NUnitmodule MathTest =

open NUnit.Framework

let [<Test>] ``2 + 2 should equal 4``() = Assert.AreEqual(2 + 2, 4)

C# NUnitusing NUnit.Framework;

[TestFixture]public class MathTest{ [Test] public void TwoPlusTwoShouldEqualFour() { Assert.AreEqual(2 + 2, 4); }}

Page 23: F# for C# devs - Copenhagen .Net 2015

MOCKINGF# Foqlet ``order sends mail if unfilled``() = // setup data let order = Order("TALISKER", 51) let mailer = mock() order.SetMailer(mailer) // exercise order.Fill(mock()) // verify verify <@ mailer.Send(any()) @> once

C# Moqpublic void OrderSendsMailIfUnfilled(){ // setup data var order = new Order("TALISKER", 51); var mailer = new Mock<MailService>(); order.SetMailer(mailer.Object); // exercise order.Fill(Mock.Of<Warehouse>()); // verify mailer.Verify(mock => mock.Send(It.IsAny<string>()), Times.Once());}

Page 24: F# for C# devs - Copenhagen .Net 2015

TYPE PROVIDERS: JSONopen FSharp.Data

type Person = JsonProvider<""" { "name":"Name", "age":64 } """>

let thomas = Person.Parse(""" { "name":"Thomas", "age":12 } """)

person.Age

Page 25: F# for C# devs - Copenhagen .Net 2015

R – TYPE PROVIDER

Page 26: F# for C# devs - Copenhagen .Net 2015

WORLD BANK DATA WITH FUNSCRIPT

Page 27: F# for C# devs - Copenhagen .Net 2015

RESOURCES

Phil Trelford, @ptrelfordCopenhagen .Net @SimCorp, 2015

Page 28: F# for C# devs - Copenhagen .Net 2015

F# Software Foundation

http://www.fsharp.org

software stackstrainings teaching F# user groups snippets

mac and linux cross-platform books and tutorials

F# community open-source MonoDevelop

contributions research support consultancy mailing list

Page 29: F# for C# devs - Copenhagen .Net 2015

F# KOANS//---------------------------------------------------------------// About Let//// The let keyword is one of the most fundamental parts of F#.// You'll use it in almost every line of F# code you write, so// let's get to know it well! (no pun intended)//---------------------------------------------------------------[<Koan(Sort = 2)>]module ``about let`` =

[<Koan>] let LetBindsANameToAValue() = let x = 50 AssertEquality x __

Page 30: F# for C# devs - Copenhagen .Net 2015

BUY THE BOOK

Page 31: F# for C# devs - Copenhagen .Net 2015

QUESTIONS? Twitter

@ptrelford Blog

http://trelford.com/blog F# Koans:

http://tinyurl.com/fsharpkoans