f# for c# devs - ndc oslo 2015

37
F# FOR C# DEVS Phil Trelford, @ptrelford #ndcoslo, 2015

Upload: phillip-trelford

Post on 05-Aug-2015

225 views

Category:

Software


2 download

TRANSCRIPT

Page 1: F# for C# devs - NDC Oslo 2015

F# FOR C# DEVSPhil Trelford, @ptrelford

#ndcoslo, 2015

Page 2: F# for C# devs - NDC Oslo 2015

F#UNCTIONAL LONDONERS

Founded Feb 2010

950+ Members

Meets every 2 weeks

Topics include Machine Learning Finance Games Web

http://meetup.com/fsharplondon

Page 3: F# for C# devs - NDC Oslo 2015

F# COMMUNITY WORLDWIDE

Page 4: F# for C# devs - NDC Oslo 2015

VISUAL F#

Page 5: F# for C# devs - NDC Oslo 2015

THE F IN F# IS FOR FUN!

Page 6: F# for C# devs - NDC Oslo 2015

HALO 3 WITH F# SKILLS

Page 7: F# for C# devs - NDC Oslo 2015

XBLA: PATH TO GO – F# AI

Page 8: F# for C# devs - NDC Oslo 2015

F#

Statically Typed

Functional First

Object Orientated

Open Source

.Net language

In Xamarin & Visual Studio

Page 9: F# for C# devs - NDC Oslo 2015

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/

Page 10: F# for C# devs - NDC Oslo 2015

F# FOR PROFIT

Phil Trelford, @ptrelford

#ndcoslo, 2015

Page 11: F# for C# devs - NDC Oslo 2015

WHY F#?

Time to Market

Efficiency

Correctness

Complexity

Page 12: F# for C# devs - NDC Oslo 2015

TIME TO MARKET

speed development by 50 percent or more,

European IB

order of magnitude increase in productivity,

GameSys

Page 13: F# for C# devs - NDC Oslo 2015

EFFICIENCY

processes that used to require hours now take just minutes

Grange Insurance

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

Aviva

Page 14: F# for C# devs - NDC Oslo 2015

CORRECTNESS

leads 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 - NDC Oslo 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 - NDC Oslo 2015

COMPLEXITY

everything becomes simple and clear when expressed in F#,

Byron Cook, Microsoft Research

Page 17: F# for C# devs - NDC Oslo 2015

COMPLEXITY: CYCLES IN SPECFLOW

Page 18: F# for C# devs - NDC Oslo 2015

COMPLEXITY: CYCLES IN TICKSPEC

Page 19: F# for C# devs - NDC Oslo 2015

LIVE DEMOS

Phil Trelford, @ptrelford

#ndcoslo, 2015

Page 20: F# for C# devs - NDC Oslo 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; } }}

Page 21: F# for C# devs - NDC Oslo 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 - NDC Oslo 2015

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)

Page 23: F# for C# devs - NDC Oslo 2015

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

Page 24: F# for C# devs - NDC Oslo 2015

CODE: FIZZBUZZ

Page 25: F# for C# devs - NDC Oslo 2015

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

Page 26: F# for C# devs - NDC Oslo 2015

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

Page 27: F# for C# devs - NDC Oslo 2015

TICKSPEC: DEBUGGING TEXT FILES

Page 28: F# for C# devs - NDC Oslo 2015

TYPE PROVIDERS: JSON

open FSharp.Data

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

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

person.Age

Page 29: F# for C# devs - NDC Oslo 2015

R – TYPE PROVIDER

Page 30: F# for C# devs - NDC Oslo 2015

WORLD BANK DATA WITH FUNSCRIPT

Page 31: F# for C# devs - NDC Oslo 2015

RESOURCES

Phil Trelford, @ptrelford

#ndcoslo, 2015

Page 32: F# for C# devs - NDC Oslo 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

Page 33: F# for C# devs - NDC Oslo 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 34: F# for C# devs - NDC Oslo 2015

TRYFSHARP.ORG

Page 35: F# for C# devs - NDC Oslo 2015

BUY THE BOOK

Page 36: F# for C# devs - NDC Oslo 2015

QUESTIONS?

Twitter @ptrelford

Blog http://trelford.com/blog

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

Page 37: F# for C# devs - NDC Oslo 2015

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!)