Skip to content

Instantly share code, notes, and snippets.

@lessismore1
lessismore1 / 01_SayHello.fsx
Created March 9, 2018 16:27 — forked from akimboyko/01_SayHello.fsx
Samples from "Actor-based Concurrency with F# and Akka.NET" http://bit.ly/FSharpAkkaNET
#time "on"
#load "Bootstrap.fsx"
open System
open Akka.Actor
open Akka.Configuration
open Akka.FSharp
open Akka.TestKit
// #Using Actor
namespace MyNamespace
type IMyInterface =
abstract GetValue: unit -> string
type MyRecord =
{ MyField1: int
MyField2: string }
interface IMyInterface with
member x.GetValue() = x.MyField2
// Rather than use [<DefaultValue>], define a default record.
type MyRecord = {
field1 : int
field2 : int
}
let defaultRecord1 = { field1 = 0; field2 = 0 }
let defaultRecord2 = { field1 = 1; field2 = 25 }
// Use the with keyword to populate only a few chosen fields
@lessismore1
lessismore1 / FuncHelper.fs
Created March 24, 2018 10:47 — forked from chamook/FuncHelper.fs
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)
open Microsoft.FSharp.Reflection
let toString (x:'a) =
match FSharpValue.GetUnionFields(x, typeof<'a>) with
| case, _ -> case.Name
let fromString<'a> (s:string) =
match FSharpType.GetUnionCases typeof<'a> |> Array.filter (fun case -> case.Name = s) with
|[|case|] -> Some(FSharpValue.MakeUnion(case,[||]) :?> 'a)
|_ -> None
@lessismore1
lessismore1 / SendGridEmail
Created March 26, 2018 18:15 — forked from jamessdixon/SendGridEmail
Send Grid Email in F#
#r "~/packages/Sendgrid.5.1.0/lib/SendGridMail.dll"
#r "~/packages/SendGrid.SmtpApi.1.2.1/lib/net40/SendGrid.SmtpApi.dll"
open System
open System.Collections.Generic
open System.Net
open System.Net.Mail
open SendGrid
@lessismore1
lessismore1 / gist:10d0005baa6cad4ff2873435df008c21
Created March 27, 2018 06:23 — forked from mausch/gist:3188428
Async exception handling in F#
open System
open System.Net
// exception handling in async using Async.Catch
let fetchAsync (name, url:string) =
async {
let uri = new System.Uri(url)
let webClient = new WebClient()
let! html = Async.Catch (webClient.AsyncDownloadString(uri))
match html with
@lessismore1
lessismore1 / gist:169c41b8c4ff7ed0372b07adc496098d
Created March 30, 2018 07:08 — forked from theburningmonk/gist:3921623
F# - helper functions to Start and Wait for a plain Task (not Task<T>)
open System.Threading.Tasks
[<AutoOpen>]
module Async =
let inline awaitPlainTask (task: Task) =
// rethrow exception from preceding task if it fauled
let continuation (t : Task) : unit =
match t.IsFaulted with
| true -> raise t.Exception
| arg -> ()
@lessismore1
lessismore1 / RResult.md
Created April 1, 2018 22:08 — forked from mrange/RResult.md
Railway Oriented Programming and F# Result

Railway Oriented Programming and F# Result

Full source: https://gist.github.com/mrange/aa9e0898492b6d384dd839bc4a2f96a1

Option<_> is great for ROP (Railway Oriented Programming) but we get no info on what went wrong (the failure value is None which carries no info).

With the introduction F# 4.1 we got Result<_, _> a "smarter" Option<_> as it allows us to pass a failure value.

However, when one inspects the signature of Result.bind one sees a potential issue for ROP:

@lessismore1
lessismore1 / ApiModel.hs
Created April 2, 2018 10:07 — forked from ploeh/ApiModel.hs
Handling a reservation request in Haskell. Proof of concept
module ApiModel where
import Data.Time (ZonedTime(..), parseTimeM, defaultTimeLocale, iso8601DateFormat)
data ReservationRendition = ReservationRendition
{ rDate :: String
, rName :: String
, rEmail :: String
, rQuantity :: Int }
deriving (Eq, Show, Read)