26
F# EYE 4 THE C# GUY Phil Trelford, @ptrelford DDD Cambridge Nights, 2014

F# Eye 4 the C# Guy - DDD Cambridge Nights 2014

Embed Size (px)

Citation preview

Page 1: F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014

F# EYE 4 THE C# GUYPhil Trelford, @ptrelford

DDD Cambridge Nights, 2014

Page 2: F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014

VISUAL F#

Page 3: F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014

THE F IN F# IS FOR FUN!

Page 4: F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014

HALO 3 WITH F# SKILLS

Page 5: F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014

XBLA: PATH TO GO – F# AI

Page 6: F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014

MONOPOLY ON FACEBOOK

Page 7: F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014

F#

Statically Typed

Functional First

Object Orientated

Open Source

.Net language

In Visual Studio

& Xamarin Studio

Page 8: F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014

C# & F# = BEST FRIENDS

Kaggle

The fact that F# targets the CLR was also critical

we have a large existing code base in C#,

getting started with F# was an easy decision.

Page 9: F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014

F# FOR PROFIT: KAGGLE

The F# code is

consistently shorter,

easier to read,

easier to refactor and contains far fewer bugs.

…we’ve become

more productive.

Page 10: F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014

LIVE DEMOS

Phil Trelford, @ptrelford

DDD Cambridge, 2014

Page 11: F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014

LIGHT SYNTAX: POCOS

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 12: F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014

LIGHT SYNTAX: DI

F#

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 13: F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014

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 14: F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014

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 15: F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014

TICKSPEC: DEBUGGING

Page 16: F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014

UNITS IN CELLStype formula =

| Neg of formula

| Exp of formula * formula

| ArithmeticOp of

formula * arithmetic * formula

| LogicalOp of

formula * logical * formula

| Num of UnitValue

| Ref of int * int

| Range of int * int * int * int

| Fun of string * formula list

Page 17: F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014

TYPE PROVIDERS: JSON

open FSharp.Data

type Simple = JsonProvider<""" { "name":"John", "age":94 } """>

let simple = Simple.Parse(""" { "name":"Tomas", "age":4 } """)

Simple.Age

Page 19: F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014

RESOURCES

Phil Trelford, @ptrelford

DDD Cambridge, 2014

Page 20: F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014

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 21: F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014

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 22: F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014

TRYFSHARP.ORG

Page 23: F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014

BUY THE BOOK

Page 24: F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014

GET THE T-SHIRT

Page 25: F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014

SHOW ME THE MONEY!

Page 26: F# Eye 4 the C# Guy -  DDD Cambridge Nights 2014

QUESTIONS?

Phil Trelford, @ptrelford

DDD Cambridge, 2014