Skip to content

Instantly share code, notes, and snippets.

Isaac Abraham isaacabraham

Block or report user

Report or block isaacabraham

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@isaacabraham
isaacabraham / matching.fsx
Last active Oct 14, 2019
Some examples of pattern matching with explicit options
View matching.fsx
type SurfaceOrientation = Rotation270 = 0 | Rotation180 = 1 | Rotation90 = 2 | Rotation0 = 3
type DisplayOrientation = Rotation270 | Rotation180 | Rotation90 | Rotation0 | Unknown
type Display = { Rotation : SurfaceOrientation }
let getDefaultDisplay() : Display option = failwith ""
let display = getDefaultDisplay()
// 1. Does not compile! Can't access Rotation directly as the display value is optional.
match display.Rotation with
| SurfaceOrientation.Rotation270 -> DisplayOrientation.Rotation270
@isaacabraham
isaacabraham / 1. types.fsx
Last active Aug 27, 2019
Example of an F# DSL designed for building ARM Templates
View 1. types.fsx
open ARM
let template =
{ Parameters = [
"environment"
"location"
"pricingTier"
]
Variables = [
"environment", "[toLower(parameters('environment'))]"
View recursive-data-structure.fsx
type FileElement =
| Folder of Name : string * Children : FileElement list
| File of {| Name:string; Size : decimal |}
let rec processFileElement element =
match element with
| File details ->
printfn "Oh look, a file called %s of size %MKB!" details.Name details.Size
| Folder (name, children) ->
printfn "I'm entering %s. It has %d direct children." name children.Length
View 1-programsync.fs
open System.Threading.Tasks
let add a b = a + b
let generateAString() = "foo"
let oldMain argv =
let anInt = add 1 2
let aString = generateAString()
printfn "Number is %d and string is '%s'" anInt aString
0
View DbPatterns.fs
module DbExceptions
open System.Text.RegularExpressions
let (|GenericDbException|_|) (ex:exn) =
match ex with
| :? SqlException as ex -> Some(GenericDbException ex)
| _ -> None
[<AutoOpen>]
View span-slice.fsx
#I @"C:\Users\Isaac\.nuget\packages"
#r @"Microsoft.NETCore.App\2.2.1\ref\netcoreapp2.2\netstandard.dll"
#r @"system.memory\4.5.2\lib\netstandard2.0\System.Memory.dll"
#r @"System.Runtime.CompilerServices.Unsafe\4.5.2\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll"
#r "System.Runtime"
open System
let x = "Foobarbazbiz"
View pm.cs
using System;
namespace ConsoleApp1
{
abstract class Result {
public static Result Ok<T>(T value) => new Ok<T> { Value = value };
public static Result Error<T>(T value) => new Error<T> { Value = value };
}
View photo-merger-2.fs
let root = @"C:\Photos"
for srcDir in System.IO.Directory.GetDirectories root do
try
let srcDirName = (System.IO.Path.GetFileName srcDir).Split '-'
let year, month = int srcDirName.[0], int srcDirName.[1]
let targetDir = System.IO.Path.Combine(root, sprintf "%d-%d" year month)
System.IO.Directory.CreateDirectory targetDir |> ignore
for srcFile in System.IO.Directory.GetFiles srcDir do
System.IO.File.Move(srcFile, System.IO.Path.Combine(targetDir, System.IO.Path.GetFileName srcFile))
@isaacabraham
isaacabraham / Async.Sequential.fs
Created Nov 20, 2018
Example of a sequential Async operation in F#.
View Async.Sequential.fs
type Async with
static member Sequential workflows =
let rec sequential results (workflows:_ Async list) = async {
match workflows with
| [] ->
return results |> List.rev
| workflow :: workflows ->
let! result = workflow
return! sequential (result :: results) workflows }
sequential [] (List.ofSeq workflows)
View matching.fsx
type Person = { IsClever : bool }
let p = { IsClever = true }
/// Warning - missed "false"
let example1 =
match p with
| { IsClever = true } -> "Yes, this person is clever"
/// No warning - all cases matched
You can’t perform that action at this time.