Skip to content

Instantly share code, notes, and snippets.

View mattpodwysocki's full-sized avatar

Matthew Podwysocki mattpodwysocki

View GitHub Profile
#light
namespace Microsoft.MapReduce
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
[<CompilationMapping(SourceConstructFlags.Module)>]
module String =
open System.Text.RegularExpressions
let words (s:string) : string list =
#light
type ICustomerRepository =
abstract GetCustomersWithFirstNameOf : string -> seq<Customer>
let test_lookup() =
let repository = { new ICustomerRepository with
member this.GetCustomersWithFirstNameOf(name) =
[{ CustomerName = name; Address = "1 Microsoft Way"; City = "Redmond"; State = "WA" }]
}
#light
namespace CodeBetter.Extensions
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
[<CompilationMapping(SourceConstructFlags.Module)>]
module List =
// Insert
// The non-overloaded version of 'insert'
#light
open System
module List =
let to_string (xs:char list) : string =
new string(Array.of_list xs)
module Char =
#light
module Char =
open System
let isAlpha = Char.IsLetter
let isDigit = Char.IsDigit
let isHexDigit c =
public static IEnumerable<T> Filter<T>(this IEnumerable<T> items, Func<T, bool> pred)
{
return items.Aggregate<T, Func<IEnumerable<T>, IEnumerable<T>>>(
x => x, (f, c) => x => f(pred(c) ? (new [] { c }).Concat(x) : x))(Enumerable.Empty<T>());
}
public static IEnumerable<R> Map<T, R>(this IEnumerable<T> items, Func<T, R> proj)
{
return items.Aggregate<T, Func<IEnumerable<R>, IEnumerable<R>>>(
x => x, (f, c) => x => f((new[] { proj(c) }).Concat(x)))(Enumerable.Empty<R>());
[Fact]
public void TestSpecificationCollection()
{
var minCredit = 3;
var product = new Product() { MinimumCreditRating = minCredit, Price = 50 };
var spec = new EligibleForDiscountSpecification(product);
var customers = new List<Customer>()
{
new Customer() {CreditRating = minCredit-2}, // bad credit
-- Haskell
identity :: [a] -> [a]
identity xs = foldr (:) [] xs
// F#
let identity (xs:'a list) : 'a list = List.fold_right (::) [] xs;;
-- Haskell
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
// F#
let rec fibs : LazyList<int> =
LazyList.cons 0 (LazyList.cons 1 (fun() -> (LazyList.map2 (+) fibs (LazyList.tl fibs))))
-- Haskell
(.) :: (b -> c) -> (a -> b) -> a -> c
// F#
val (<<) : ('a -> 'b) -> ('c -> 'a) -> 'c -> 'b
val (>>) : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c
val (|>) : 'a -> ('a -> 'b) -> 'b
val (<|) : ('a -> 'b) -> 'a -> 'b