Created
January 4, 2016 21:43
-
-
Save miklund/384428ba302876603e69 to your computer and use it in GitHub Desktop.
2011-03-22 F-sharpen your saw
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Title: F-sharpen your saw | |
# Author: Mikael Lundin | |
# Link: http://blog.mikaellundin.name/2011/03/22/f-sharpen-your-saw.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Xunit | |
// SUT | |
let add a1 a2 = a1 + a2 | |
[<Fact>] | |
let ShouldAddTwoNumbersTogether() = | |
let result = add 8 4 | |
Assert.Equal(12, result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let rec dequeue (q : Queue) = | |
match q.Empty with | |
| true -> [] | |
| false -> q.Pop.Value :: dequeue q | |
dequeue queue |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace LiteMedia.CSharpLecture | |
{ | |
public class Example1 | |
{ | |
const int Max = 100000; | |
public void Aggregate() | |
{ | |
var result = 0; | |
for (int i = 1; i < Max; i++) | |
result += i; | |
System.Console.WriteLine(result); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let fibonacci = Seq.initInfinite (fun i -> | |
let rec fibonacci_inner = function | |
| 0 -> 0 | |
| 1 -> 1 | |
| n -> fibonacci_inner (n - 1) + fibonacci_inner (n - 2) | |
fibonacci_inner i) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let rec findPrime l = | |
let isPrime n = [2..(n/2)] | |
|> List.exists (fun x -> n % x = 0) |> not | |
match l with | |
| [] -> None | |
| head :: tail when head |> isPrime -> Some(head) | |
| head :: tail -> findPrime tail |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let hasPrime l = | |
match findPrime l with | |
| None -> false | |
| Some(x) -> true | |
[4; 6; 8; 9; 11] |> hasPrime |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace LiteMedia.Web | |
open System.Web | |
open System.Web.Mvc | |
[<HandleError>] | |
type HomeController() = | |
inherit Controller() | |
member x.Index () : ActionResult = | |
x.ViewData.["Message"] <- "Welcome to ASP.NET MVC!" | |
x.View() :> ActionResult | |
member x.About () = | |
x.View() :> ActionResult |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Identity monad | |
type Identity<'a> = | |
| Identity of 'a | |
type IdentityBuilder<'a>(v : 'a) = | |
let value = v | |
member self.Bind ((Identity v), f) = f(v) | |
member self.Return v = Identity v | |
let identity = new IdentityBuilder<int>(0) | |
let answerToEverything = | |
identity { return System.Int32.Parse("42") } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[<ServiceContract(ConfigurationName = "IsItFriday", | |
Namespace = "http://litemedia.se/IsItFriday")>] | |
[<ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)>] | |
type public IsItFriday() = | |
class | |
[<OperationContract>] | |
member public x.Query() = | |
DateTime.Today.DayOfWeek = DayOfWeek.Friday | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoginController | |
{ | |
private readonly IRepository<User> userRepository; | |
public LoginController(IRepository<User> userRepository) | |
{ | |
this.userRepository = userRepository; | |
} | |
public bool Login(string username, string password) | |
{ | |
var users = userRepository.GetAll(); | |
return users.Any( user => | |
user.UserName.Equals(username, StringComparison.InvariantCultureIgnoreCase) && | |
user.Password.Equals(password)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Point = { x : int; y : int } | |
let graph fn width height = | |
// Is point y between y1 and y2 | |
// int -> int -> Point -> bool | |
let yBetween y1 y2 point = point.y > y1 && point.y < y2 | |
// For all x, -100 to 100 | |
[-(width / 2)..(width / 2)] | |
|> List.map (fun x -> { x = x; y = fn x}) | |
|> List.filter (yBetween -(height / 2) (height / 2)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Queue() = | |
let mutable queue = [] | |
member this.Empty = queue = [] | |
member this.Push x = queue <- queue @ [x] | |
member this.Pop = | |
match queue with | |
| [] -> None | |
| head :: tail -> | |
queue <- tail | |
Some(head) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[<Fact>] | |
let ShouldSuccessfullyLoginToController() = | |
// Setup | |
let user = new User("Mikael", "Hello") | |
let repository = { | |
new IRepository<User> with | |
member this.GetAll() = [|user|] :> seq<User> } | |
let controller = new LoginController(repository) | |
// Test | |
let result = controller.Login(user.UserName, user.Password) | |
// Assert | |
Assert.True(result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let primes n = | |
let rec sieve = function | |
| [] -> [] | |
| head :: tail -> head :: | |
sieve (tail |> List.filter (fun x -> x % head <> 0)) | |
sieve [2..n] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let sum max = | |
let result = 0 | |
for i = 0 to max do | |
result <- result + i | |
result | |
sum 100000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let rec sum max = | |
if max = 0 then | |
0 | |
else | |
max + sum (max - 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let rec sum max = | |
match max with | |
| 0 -> 0 | |
| _ -> max + sum (max - 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let rec sum = function | |
| 0 -> 0 | |
| n -> n + sum (n - 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System.Net | |
open Microsoft.FSharp.Control.WebExtensions | |
let webGet (name, url) = | |
async { | |
let uri = new System.Uri(url) | |
let webClient = new WebClient() | |
let! html = webClient.AsyncDownloadString(uri) | |
return name, html.Length | |
} | |
let addresses = [ "Valtech", "http://www.valtech.se"; | |
"LiteMedia", "http://litemedia.se" ] | |
let contentLengths = | |
addresses | |
|> Seq.map webGet | |
|> Async.Parallel | |
|> Async.RunSynchronously |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment