Skip to content

Instantly share code, notes, and snippets.

Isaac Abraham isaacabraham

View GitHub Profile
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
@isaacabraham
isaacabraham / gist:882236bc83a35a02fdd9f7984ad0ccf8
Last active Aug 30, 2018
Test if the behaviour of two functions are the same.
View gist:882236bc83a35a02fdd9f7984ad0ccf8
// Some dummy data types
type Thing = Red | Yellow | Blue
type Foo = { Number : PositiveInt; Other : Thing }
/// First way
let groupByThenSortAndHead data =
data
|> Array.groupBy(fun x -> x.Other)
|> Array.map (
snd
View nullability.md
BCL Source Member Access Null comparison
Oblivious Allowed. Allowed.
Explicitly Nullable Compiler error, must pattern match. Allowed.
Explicitly non-Nullable Allowed Compiler error.
@isaacabraham
isaacabraham / io-monad.fsx
Last active Aug 17, 2018
F# port of the first half of John De Goes "FP to the max" (https://www.youtube.com/watch?v=sxudIMiOo68)
View io-monad.fsx
#load @".paket\load\net452\FSharpPlus.fsx"
open FSharpPlus
open System
[<AutoOpen>]
module rec IO =
let run (IO computation) = computation()
type IO<'T> =
@isaacabraham
isaacabraham / 16_exercise.fsx
Created Jul 2, 2018 — forked from Schueh/16_exercise.fsx
Sort subfolders by folder size in F#
View 16_exercise.fsx
open System.IO
type FolderStatistic = { Name : string
Size : int64 }
let calculateFolderSize (folder : string) =
folder
|> Directory.GetFiles
|> Array.sumBy (fun file -> (FileInfo file).Length)
You can’t perform that action at this time.