Upload
phillip-trelford
View
1.003
Download
3
Embed Size (px)
Citation preview
F# Eye 4 the C# GuyPhillip Trelford
@ptrelford
http://trelford.com/blog
Thoughtworks Tech RadarMarch 2012 - Languages
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#.”
Visual F#
The F in F# is for FUN!
Halo 3 with F# Skills
XBLA: Path to Go – F# AI
Facebook: Monopoly
F#
• Statically Typed• Functional• Object Orientated• Open Source• .Net language• In Visual Studio
Functional Programming
• Idempotent Functions• Higher Order Functions• Pattern Matching
Functions - Excel
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;}
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"); }
DEMOS
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; } }}
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() = () // ...
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); }}
BDD
Units of Measure
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
F# 3.0Type Providers
SUMMARY
F# Books
Meet the F#ers Twitter #fsharp
@rickasaurus
@tomaspetricek
@jonharrop
@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
Q & A
Links
• http://fsharp.org• http://fssnip.net• http://tryfsharp.org
Connect
• @ptrelford• [email protected]• http://trelford.com/blog