F# for C# devs - Copenhagen .Net 2015

Preview:

Citation preview

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

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

Machine Learning Finance Games Web

http://meetup.com/fsharplondon

F# COMMUNITY WORLDWIDE

VISUAL F#

THE F IN F# IS FOR FUN!

HALO 3 WITH F# SKILLS

XBLA: PATH TO GO – F# AI

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

Studio

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/

F# FOR PROFIT

Phil Trelford, @ptrelfordCopenhagen .Net @SimCorp, 2015

WHY F#? Time to Market Efficiency Correctness Complexity

TIME TO MARKETspeed development by 50 percent or more,

European IB

order of magnitude increase in productivity, GameSys

EFFICIENCYprocesses that used to require hours now take just

minutesGrange Insurance

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

CORRECTNESSleads to virtually bug-free code,

Fixed Income

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

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

COMPLEXITY

everything becomes simple and clear when expressed in F#,

Byron Cook, Microsoft Research

COMPLEXITY: CYCLES IN SPECFLOW

COMPLEXITY: CYCLES IN TICKSPEC

LIVE DEMOS

Phil Trelford, @ptrelfordCopenhagen .Net @SimCorp, 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; } }}

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() { // ... }}

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); }}

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());}

TYPE PROVIDERS: JSONopen FSharp.Data

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

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

person.Age

R – TYPE PROVIDER

WORLD BANK DATA WITH FUNSCRIPT

RESOURCES

Phil Trelford, @ptrelfordCopenhagen .Net @SimCorp, 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

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 __

BUY THE BOOK

QUESTIONS? Twitter

@ptrelford Blog

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

http://tinyurl.com/fsharpkoans

Recommended