Skip to content

Instantly share code, notes, and snippets.

View chamook's full-sized avatar
🍫
Would you risk it for a chocolate biscuit?

Adam Guest chamook

🍫
Would you risk it for a chocolate biscuit?
View GitHub Profile
@chamook
chamook / InterfaceAndClass.fs
Last active January 19, 2016 21:31
A super basic example of using interfaces and classes in F#
type ISomeInterface =
abstract DoAnInterfaceyThing : string -> string
type SomeClassThatImplementsAnInterface () =
interface ISomeInterface with
member x.DoAnInterfaceyThing msg =
"Interface!" + msg
member x.DoAClassThing a = a + 1
@chamook
chamook / StaticClass.fs
Created January 19, 2016 21:39
Static classes are just figments of your imagination
[<Sealed>]
type StaticishClass private () =
static member DoThing a = a + 1
@chamook
chamook / ConstructorsAreFunctionsToo.fs
Last active January 20, 2016 18:26
You can make .net objects in F# (in case you were in doubt)
open System
let makeUri s = "http://" + s |> Uri
@chamook
chamook / ClassThatWantsAFunc.fs
Created January 20, 2016 20:50
A class defined in F# can expect an FSharpFunc as a dependency
type SomeClassWithADependency (funcThing : string -> string) =
member x.DoThing(s) = funcThing s
public static class FunctionContainer
{
public static string SomeStringFunction(string input) => "So, gosh-darned creative:" + input;
}
public static class Injector
{
public static SomeClassWithADependency GetTheThing() =>
new SomeClassWithADependency(FunctionContainer.SomeStringFunction.ToFSharpFunc());
}
@chamook
chamook / AsyncAndTasks.fs
Created January 20, 2016 21:17
F# makes it easy to go from Task to Async and back again
let anAsyncFunction thing =
async {
//a task becomes an async
return! Task.FromResult(thing) |> Async.AwaitTask
}
//and back again!
let nowItsATask =
"yo" |> anAsyncFunction |> Async.StartAsTask
@chamook
chamook / FuncHelper.fs
Created January 21, 2016 06:10
Convert a C# Func or Action into an F# function
open System.Runtime.CompilerServices
[<Extension>]
type public FSharpFuncUtil =
[<Extension>]
static member ToFSharpFunc<'a> (func:System.Func<'a>) = fun () -> func.Invoke()
[<Extension>]
static member ToFSharpFunc<'a,'b> (func:System.Converter<'a,'b>) = fun x -> func.Invoke(x)
namespace Rop
type Result<'TSuccess, 'TFailure> =
| Success of 'TSuccess
| Failure of 'TFailure
module Result =
let map f x =
match x with
module AsyncResult =
let map f x =
async {
let! x' = x
return x' |> Result.map f
}
let bind f x =
async {
public class ThingFactory
{
public string ThingName { get; }
public string ThingValue { get; }
public ThingFactory(string thingName, int thingValue)
{
if (thingName == null)
throw new ArgumentNullException(nameof(thingName));