26
F# Eye 4 the C# Guy Phillip Trelford @ptrelford http://trelford.com/blog

F# Eye For The C# Guy - Seattle 2013

Embed Size (px)

Citation preview

Page 1: F# Eye For The C# Guy - Seattle 2013

F# Eye 4 the C# GuyPhillip Trelford

@ptrelford

http://trelford.com/blog

Page 2: F# Eye For The C# Guy - Seattle 2013

Thoughtworks Tech RadarMarch 2012 - Languages

Page 3: F# Eye For The C# Guy - Seattle 2013

Thoughtworks Tech RadarMarch 2012 - Quotes

“F# is excellent at concisely expressing

business and domain logic.”

“Developers trying to achieve explicit business logic within an application may opt to

express their domain in F#

with the majority of plumbing code in C#.”

Page 4: F# Eye For The C# Guy - Seattle 2013

Visual F#

Page 5: F# Eye For The C# Guy - Seattle 2013

The F in F# is for FUN!

Page 6: F# Eye For The C# Guy - Seattle 2013

Halo 3 with F# Skills

Page 7: F# Eye For The C# Guy - Seattle 2013

XBLA: Path to Go – F# AI

Page 8: F# Eye For The C# Guy - Seattle 2013

Facebook: Monopoly

Page 9: F# Eye For The C# Guy - Seattle 2013

F#

• Statically Typed• Functional• Object Orientated• Open Source• .Net language• In Visual Studio

Page 10: F# Eye For The C# Guy - Seattle 2013

Functional Programming

• Idempotent Functions• Higher Order Functions• Pattern Matching

Page 11: F# Eye For The C# Guy - Seattle 2013

Functions - Excel

Page 12: F# Eye For The C# Guy - Seattle 2013

Higher Order Functions

F# Map/Reducelet map f xs = seq { for x in xs do yield f x }

let reduce f init items = let mutable current = init for item in items do current <- f current item current

C# Map/Reducepublic static IEnumerable<R> Map<T, R> (this IEnumerable<T> xs, Func<T, R> f){ foreach (var x in xs) yield return f(x);}

public static R Reduce<T, R> (this IEnumerable<T> xs, R init, Func<R, T, R> f){ var current = init; foreach (var x in xs) current = f(current, x); return current;}

Page 13: F# Eye For The C# Guy - Seattle 2013

Pattern Matching

F#match day with| 0 -> "Sunday"| 1 -> "Monday"| 2 -> "Tuesday"| 3 -> "Wednesday"| 4 -> "Thursday"| 5 -> "Friday"| 6 -> "Saturday"| _ –> invalidArg "Invalid day"

C#switch (day) { case 0: return "Sunday"; case 1: return "Monday"; case 2: return "Tuesday"; case 3: return "Wednesday"; case 4: return "Thursday"; case 5: return "Friday"; case 6: return "Saturday"; default: throw new ArgumentException("day"); }

Page 14: F# Eye For The C# Guy - Seattle 2013

DEMOS

Page 15: F# Eye For The C# Guy - Seattle 2013

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 16: F# Eye For The C# Guy - Seattle 2013

Light Syntax: DI

C#public class VerySimpleStockTraderImpl : IAutomatedStockTrader{ private readonly IStockAnalysisService analysisService; private readonly IOnlineBrokerageService brokerageService;

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

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

F#type VerySimpleStockTraderImpl (analysisService:IStockAnalysisService, brokerageService:IOnlineBrokerageService) = member this.ExecuteTrades() = () // ...

Page 17: F# Eye For The C# Guy - Seattle 2013

Unit Testing

F# 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 18: F# Eye For The C# Guy - Seattle 2013

BDD

Page 19: F# Eye For The C# Guy - Seattle 2013

Units of Measure

Page 20: F# Eye For The C# Guy - Seattle 2013

Units in Cells

type 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 21: F# Eye For The C# Guy - Seattle 2013

F# 3.0Type Providers

Page 22: F# Eye For The C# Guy - Seattle 2013

SUMMARY

Page 23: F# Eye For The C# Guy - Seattle 2013

F# Books

Page 24: F# Eye For The C# Guy - Seattle 2013

Meet the F#ers Twitter #fsharp

@rickasaurus

@tomaspetricek

@jonharrop

Page 25: F# Eye For The C# Guy - Seattle 2013

@jbandi

#chtd Quote from "future of .net" Have a look at F# for two days and you will find yourself two years ahead of your peer .net devs. #fsharp

Page 26: F# Eye For The C# Guy - Seattle 2013

Q & A

Links

• http://fsharp.org• http://fssnip.net• http://tryfsharp.org

Connect

• @ptrelford• [email protected]• http://trelford.com/blog