260 likes | 376 Views
F# Eye 4 the C# Guy. Phillip Trelford @ ptrelford http://trelford.com/blog. Thoughtworks Tech Radar March 2012 - Languages. “F# is excellent at concisely expressing business and domain logic.” “Developers trying to achieve explicit business logic within an application may opt to
E N D
F# Eye 4 the C# Guy Phillip Trelford @ptrelford http://trelford.com/blog
“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#.” Thoughtworks Tech RadarMarch 2012 - Quotes
F# Statically Typed Functional Object Orientated Open Source .Net language In Visual Studio
Idempotent Functions • Higher Order Functions • Pattern Matching Functional Programming
F# Map/Reduce C# Map/Reduce publicstatic IEnumerable<R> Map<T, R> (thisIEnumerable<T> xs, Func<T, R> f) { foreach (var x in xs) yieldreturn f(x); } publicstatic R Reduce<T, R> (thisIEnumerable<T> xs, R init, Func<R, T, R> f) { varcurrent = init; foreach (var x in xs) current = f(current, x); return current; } letmapf xs = seq { for x inxsdo yield f x } letreducefinititems = letmutablecurrent = init for item in items do current <- f current item current Higher Order Functions
F# C# switch (day) { case0: return"Sunday"; case1: return"Monday"; case2: return"Tuesday"; case3: return"Wednesday"; case4: return"Thursday"; case5: return"Friday"; case6: return"Saturday"; default: thrownew ArgumentException("day"); } match day with | 0 ->"Sunday" | 1 ->"Monday" | 2 ->"Tuesday" | 3 ->"Wednesday" | 4 ->"Thursday" | 5 ->"Friday" | 6 ->"Saturday" | _ –> invalidArg"Invalid day" Pattern Matching
F# C# publicclassPerson { public Person(string name, int age) { _name = name; _age = age; } privatereadonlystring _name; privatereadonlyint _age; ///<summary> /// Full name ///</summary> publicstring Name { get { return _name; } } ///<summary> /// Age in years ///</summary> publicint Age { get { return _age; } } } type Person(name:string,age:int) = /// Full name memberperson.Name = name /// Age in years memberperson.Age = age Light Syntax: POCOs
C# F# typeVerySimpleStockTraderImpl (analysisService:IStockAnalysisService, brokerageService:IOnlineBrokerageService) = memberthis.ExecuteTrades() = () // ... publicclassVerySimpleStockTraderImpl : IAutomatedStockTrader { privatereadonlyIStockAnalysisServiceanalysisService; privatereadonlyIOnlineBrokerageServicebrokerageService; publicVerySimpleStockTraderImpl( IStockAnalysisServiceanalysisService, IOnlineBrokerageServicebrokerageService) { this.analysisService = analysisService; this.brokerageService = brokerageService; } publicvoidExecuteTrades() { // ... } } Light Syntax: DI
F# NUnit C# NUnit usingNUnit.Framework; [TestFixture] publicclassMathTest { [Test] publicvoid TwoPlusTwoShouldEqualFour() { Assert.AreEqual(2 + 2, 4); } } moduleMathTest = openNUnit.Framework let [<Test>] ``2 + 2 should equal 4``() = Assert.AreEqual(2 + 2, 4) Unit Testing
typeformula = | Negof formula | Expof formula * formula | ArithmeticOpof formula * arithmetic * formula | LogicalOpof formula * logical * formula | NumofUnitValue | Ref ofint * int | Range ofint * int * int * int | Fun of string * formula list Units in Cells
@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 .netdevs. #fsharp
Links Connect @ptrelford phil@trelford.com http://trelford.com/blog • http://fsharp.org • http://fssnip.net • http://tryfsharp.org Q & A