This document discusses F# and compares it to C#. It notes that F# is statically typed, functional first, and object oriented. It is an open source .NET language that can be used in Visual Studio and Xamarin Studio. The document discusses how F# code is shorter, easier to read, easier to refactor, and contains fewer bugs than equivalent C# code. It provides examples comparing F# and C# code for defining classes, unit tests, mocks, and using JSON.
7. Statically Typed
Functional First
Object Orientated
Open Source
.Net language
In Visual Studio
& Xamarin Studio
8. 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 because we
knew we could use new
modules right away.
9. The F# code is
consistently shorter,
easier to read,
easier to refactor and
contains far fewer bugs.
…we’ve become
more productive.
11. F#
C#
type Person(name:string,age:int) =
/// Full name
member person.Name = name
/// Age in years
member person.Age = age
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
12. 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()
{
// ...
}
}
13. F# NUnit
C# NUnit
module MathTest =
using NUnit.Framework;
open NUnit.Framework
[TestFixture]
public class MathTest
{
[Test]
public void TwoPlusTwoShouldEqualFour()
{
Assert.AreEqual(2 + 2, 4);
}
}
let [<Test>] ``2 + 2 should equal 4``() =
Assert.AreEqual(2 + 2, 4)
14. F# Foq
C# Moq
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
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());
}
16. open FSharp.Data
type Simple = JsonProvider<""" { "name":"John", "age":94 } """>
let simple = Simple.Parse(""" { "name":"Tomas", "age":4 } """)
Simple.Age
19. //--------------------------------------------------------------// 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 __