Skip to content

Instantly share code, notes, and snippets.

@miklund
Created January 4, 2016 21:43
Show Gist options
  • Save miklund/384428ba302876603e69 to your computer and use it in GitHub Desktop.
Save miklund/384428ba302876603e69 to your computer and use it in GitHub Desktop.
2011-03-22 F-sharpen your saw
# Title: F-sharpen your saw
# Author: Mikael Lundin
# Link: http://blog.mikaellundin.name/2011/03/22/f-sharpen-your-saw.html
open Xunit
// SUT
let add a1 a2 = a1 + a2
[<Fact>]
let ShouldAddTwoNumbersTogether() =
let result = add 8 4
Assert.Equal(12, result)
let rec dequeue (q : Queue) =
match q.Empty with
| true -> []
| false -> q.Pop.Value :: dequeue q
dequeue queue
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);
}
}
}
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)
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
let hasPrime l =
match findPrime l with
| None -> false
| Some(x) -> true
[4; 6; 8; 9; 11] |> hasPrime
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
// 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") }
[<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
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));
}
}
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))
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)
[<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)
let primes n =
let rec sieve = function
| [] -> []
| head :: tail -> head ::
sieve (tail |> List.filter (fun x -> x % head <> 0))
sieve [2..n]
let sum max =
let result = 0
for i = 0 to max do
result <- result + i
result
sum 100000
let rec sum max =
if max = 0 then
0
else
max + sum (max - 1)
let rec sum max =
match max with
| 0 -> 0
| _ -> max + sum (max - 1)
let rec sum = function
| 0 -> 0
| n -> n + sum (n - 1)
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