F# for C# devs - NDC Oslo 2015

Post on 05-Aug-2015

226 views 2 download

Transcript of F# for C# devs - NDC Oslo 2015

F# FOR C# DEVSPhil Trelford, @ptrelford

#ndcoslo, 2015

F#UNCTIONAL LONDONERS

Founded Feb 2010

950+ 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 Orientated

Open Source

.Net language

In Xamarin & Visual Studio

C# + F# = BEST FRIENDS

Kaggle 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, @ptrelford

#ndcoslo, 2015

WHY F#?

Time to Market

Efficiency

Correctness

Complexity

TIME TO MARKET

speed development by 50 percent or more,

European IB

order of magnitude increase in productivity,

GameSys

EFFICIENCY

processes that used to require hours now take just minutes

Grange Insurance

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

Aviva

CORRECTNESS

leads 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, @ptrelford

#ndcoslo, 2015

TYPES: LIGHT SYNTAX

F#

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

CODE: FIZZBUZZ WITH IF/THEN/ELSEfor i = 1 to 100 do

let text =

if i % 3 = 0 && i % 5 = 0 then "FizzBuzz"

elif i % 3 = 0 then "Fizz"

elif i % 5 = 0 then "Buzz"

else i.ToString()

Console.WriteLine(text)

CODE: FIZZBUZZ WITH PATTERN MATCHINGfor i = 1 to 100 do

match i%3, i%5 with

| 0, 0 -> "FizzBuzz"

| 0, _ -> "Fizz"

| _, 0 -> "Buzz"

| _, _ -> i.ToString()

|> Console.WriteLine

CODE: FIZZBUZZ

UNIT TESTING

F# NUnit

module MathTest =

open NUnit.Framework

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

C# NUnit

using NUnit.Framework;

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

MOCKING

F# Foq

let ``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# Moq

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

TICKSPEC: DEBUGGING TEXT FILES

TYPE PROVIDERS: JSON

open 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, @ptrelford

#ndcoslo, 2015

F# Software Foundation

http://www.fsharp.org

software stacks

trainings 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 __

TRYFSHARP.ORG

BUY THE BOOK

QUESTIONS?

Twitter @ptrelford

Blog http://trelford.com/blog

F# Koans: http://tinyurl.com/fsharpkoans

NDC Oslo Functional Track

Room 2 All day every day!

Making Functional

Programming

a safe choice for business(so we can do it as our day job!)