-------------------- type List<'T> = | ( [] ) | ( :: ) of Head: 'T * Tail: 'T list interface IEnumerable interface IEnumerable<'T> member Head : 'T member IsEmpty : bool member Item : index:int -> 'T with get member Length : int member Tail : 'T list static member Cons : head:'T * tail:'T list -> 'T list static member Empty : 'T list
Full name: Microsoft.FSharp.Collections.List<_>
val sum : list:'T list -> 'T (requires member ( + ) and member get_Zero)
Full name: Microsoft.FSharp.Collections.List.sum
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val square : x:int -> int
Full name: input.square
val x : int
val sq : int
Full name: input.sq
type Person = {First: string; Last: string;}
Full name: input.Person
Person.First: string
Multiple items val string : value:'T -> string
Full name: Microsoft.FSharp.Core.Operators.string
-------------------- type string = System.String
Full name: Microsoft.FSharp.Core.string
Person.Last: string
type Employee = | Worker of Person | Manager of Employee list
Full name: input.Employee
union case Employee.Worker: Person -> Employee
union case Employee.Manager: Employee list -> Employee
type 'T list = List<'T>
Full name: Microsoft.FSharp.Collections.list<_>
val triple : x:int -> int
Full name: input.triple
val tripleSquared : (int -> int)
Full name: input.tripleSquared
val fibonacci : n:int -> int
Full name: input.fibonacci
val n : int
val unlockAchievement : gameObj:'a -> achivement:'b -> unit
Full name: input.unlockAchievement
val gameObj : 'a
val achivement : 'b
val character : obj
val not : value:bool -> bool
Full name: Microsoft.FSharp.Core.Operators.not
val keyboard : 'a
active recognizer SpaceKey: 'a -> 'b
Full name: input.( |SpaceKey| )
active recognizer Hold100ms: 'a -> 'b
Full name: input.( |Hold100ms| )
namespace System
Multiple items val int : value:'T -> int (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int
-------------------- type int = int32
Full name: Microsoft.FSharp.Core.int
-------------------- type int<'Measure> = int
Full name: Microsoft.FSharp.Core.int<_>
F# in Onikira
CodeMotion Milan 2015
Andrea Magnorsky
Digital Furnace Games ▀ BatCat Games ▀ GameCraft Foundation
[1..100] |>List.sum|>printfn"sum=%d"// no curly braces, semicolons or parenthesesletsquarex=x*xletsq=square42typePerson= {First:string; Last:string} // simple types in one linetypeEmployee=// complex types in a few lines
| WorkerofPerson
| ManagerofEmployeelistletsquarex=x*xlettriplex=x*3lettripleSquared=square>>triple
Visit F# for Fun and Profit for more examples and knowledge
let (|SpaceKey|) (keyboard:KeyboardInput) =keyboard.KeyPressed(Key.Space)
let (|Hold100ms|) (keyboard:KeyboardInput) =keyboard.KeyPressedFor(Key.I, 100)
matchDualityApp.Keyboardwith
| SpaceKeytrue&Hold100msfalse->playerGoJump
| SpaceKeytrue&Hold100mstrue->playerGoDoubleJump
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
let (|LeftKey|RightKey|OtherKey|) (keyboard:KeyboardInput) =ifkeyboard.KeyPressed(Key.Left) thenLeftKeyelifkeyboard.KeyPressed(Key.Right) thenRightKeyelseOtherKey"Hi, you pressed a key...well that is interesting :D"interfaceICmpUpdatablewithmemberthis.OnUpdate()=matchDualityApp.Keyboardwith
| LeftKey->playerGoLeft
| RightKey->playerGoRight
| OtherKeys-> ()
Interop
Check out the design guidelines
Use namespaces in F# or prefix with global::YourModuleName
C# consuming F# code
using System;
class Program
{
staticvoid Main(string[] args)
{
var s = Calculator.Calc.add("4 4", "+");
Console.WriteLine("The sum is {0}", s);
}
}
and the F# side
1: 2: 3: 4: 5: 6:
namespaceCalculatormoduleCalc=openSystemletaddnumbersdelimiter=// Do stuff to add numbers
F# consuming C# code
1: 2: 3: 4: 5: 6:
moduleMathTest=openNUnit.Frameworklet [<Test>] ``2 + 2 should equal 4``() =Assert.AreEqual(2+2, 4)
Superb article by Scott Wlaschin on Property based testing as part of the F# advent calendar.
Can be used from C#
Small library
Can run stand alone or integrates with NUnit and xUnit
1: 2: 3:
[<Property>]
let``When adding x to x then result is double x``(x:int)=x+x=2*x
1: 2: 3: 4: 5: 6: 7: 8: 9:
letpreconditionMaxHealthmaxHealth=maxHealth>0
[<Property(Verbose=true)>]
let``Health should never be higher than max`` (x:int)(maxHealth:int)=lethealthComponent=initialiseHealthhealthComponent.MaxHealth<-maxHealthhealthComponent.IncreaseHealthxpreconditionMaxHealthmaxHealth==>
(healthComponent.MaxHealth>=healthComponent.Health)
Fake
Use from any .net language
It's mature.
Builds for .net and mono, it's cross platform.
No need to know F# to use it.
Integrates with CI Server.
Hello FooBar
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
// include Fake lib#r@"tools\FAKE\tools\FakeLib.dll"openFakeTarget"Foo" (fun _ ->trace"Hello World from Foo"
)
Target"Bar" (fun _ ->trace"Hello World from Bar"
)
"Bar"==>"Foo"RunTargetOrDefault"Foo"