Last active
February 2, 2016 22:24
-
-
Save ImaginaryDevelopment/e63ba3e4029798579bc3 to your computer and use it in GitHub Desktop.
Helpful F# snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module BReusable | |
open System | |
open System.Text | |
let trim (s:string) = if isNull s then null else s.Trim() | |
let after (delimiter:string) (s:string) = s.Substring(s.IndexOf delimiter + delimiter.Length) | |
let before (delimiter:string) (s:string) = s.Substring(0,s.IndexOf delimiter) | |
let beforeI (delimiter:string) (s:string) = s.Substring(0,s.IndexOf(delimiter, StringComparison.CurrentCultureIgnoreCase)) | |
let capture (pattern:string) (s:string) = Regex.Match(s, pattern).Groups.[1].Value | |
let isNullOrEmptyToOpt s = if String.IsNullOrEmpty s then None else Some s | |
let delimit delimiter (values:#seq<string>) = String.Join(delimiter, Array.ofSeq values) | |
let (|StartsWithI|_|) s1 (toMatch:string) = if toMatch <> null && toMatch.StartsWith(s1, StringComparison.InvariantCultureIgnoreCase) then Some () else None | |
let (|InvariantEqualI|_|) (str:string) arg = | |
if String.Compare(str, arg, StringComparison.InvariantCultureIgnoreCase) = 0 | |
then Some() else None | |
let (|OrdinalEqualI|_|) (str:string) arg = | |
if String.Compare(str, arg, StringComparison.OrdinalIgnoreCase) = 0 | |
then Some() else None | |
// |Null|Value| already in use by Nullable active pattern | |
let (|NullString|Empty|WhiteSpace|ValueString|) (s:string) = | |
match s with | |
| null -> NullString | |
| "" -> Empty | |
| _ when String.IsNullOrWhiteSpace s -> WhiteSpace | |
| _ -> ValueString | |
type System.Convert with | |
static member ToGuid(o:obj) = o :?> Guid | |
static member ToBinaryData(o:obj) = o :?> byte[] // http://stackoverflow.com/a/5371281/57883 | |
// taken from SO http://stackoverflow.com/a/1595311/57883 | |
type System.DateTime with | |
member x.getAge(now:DateTime) = | |
let age = now.Year - x.Year | |
if (now.Month < x.Month || (now.Month = x.Month && now.Day < x.Day)) then | |
age - 1 | |
else | |
age | |
type System.String with | |
member x.containsI(s:string) = if x = null then false else x.IndexOf(s,System.StringComparison.InvariantCultureIgnoreCase) >= 0 | |
member x.before(delimiter:string) = x.Substring(0,x.IndexOf(delimiter)) | |
member x.after(delimiter:string) = x.Substring(x.IndexOf(delimiter) + delimiter.Length) | |
member x.splitLines() = x.Split([| "\r\n";"\n"|], System.StringSplitOptions.None) | |
static member Null:String = null | |
static member emptyToNull (s:string) = if String.IsNullOrEmpty s then null else s | |
static member replace (target:string) (replacement) (str:string) = str.Replace(target,replacement) | |
member x.beforeAnyOf(delimiters:string list) = | |
let index,_ = | |
delimiters | |
|> Seq.map ( fun delimiter -> x.IndexOf(delimiter),delimiter) | |
|> Seq.filter( fun (index,_) -> index >= 0 ) | |
|> Seq.minBy (fun (index, _) -> index) | |
x.Substring(0,index) | |
module Rop = | |
// Railway Oriented Programming | |
type Error = {Property : string; Message : string} | |
type Result<'a> = | |
| Success of 'a | |
| Fail of Error | |
let bind f x = | |
match x with Success x -> f x |Fail err -> Fail err | |
let bind' f1 f2 x = | |
match f1 x with | |
| Success x -> f2 x | |
| Fail err -> Fail err | |
let inline (>>=) f1 f2 = bind' f1 f2 | |
let overrideFail default' r = match r with |Success x -> x | Fail(_) -> default' | |
let overrideFail' f r = match r with |Success x -> x | Fail(_) -> f() | |
[<AutoOpen>] | |
module StaticReflection = | |
open Microsoft.FSharp.Quotations | |
open Microsoft.FSharp.Quotations.Patterns | |
let rec private nameOf = function | |
| Patterns.Call(None, methodInfo, _) -> methodInfo.Name | |
| Patterns.Lambda(_, expr) -> nameOf expr | |
| Patterns.PropertyGet(_,pi,_) -> pi.Name | |
| Patterns.FieldGet(_,fi) -> fi.Name | |
| x -> failwithf "Didn't cover type %A" x | |
let rec nameof<'t> (expression:Quotations.Expr<'t -> _>) = nameOf expression |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module FSharp.NullHelpers | |
open System | |
open Microsoft.FSharp.Math | |
module Option = | |
// created by me, rest of file is from someone else's helpers | |
let getOrDefault (default': 'a) (n: 'a option) = match n with| Some x -> x | None -> default' | |
let getOrDefault' (default': 'a Lazy) (n: 'a option) = match n with| Some x -> x | None -> default'.Force() | |
let fromNullable (n: _ Nullable) = | |
if n.HasValue | |
then Some n.Value | |
else None | |
let toNullable = | |
function | |
| None -> Nullable() | |
| Some x -> Nullable x | |
let toNull = | |
function | |
| None -> null | |
| Some x -> x | |
let (|Null|NullableValue|) (x: _ Nullable) = | |
if x.HasValue then NullableValue x.Value else Null | |
module Nullable = //http://bugsquash.blogspot.com/2010/09/nullable-in-f.html also https://gist.github.com/mausch/571158 | |
//let create x = System.Nullable x (* just use Nullable in and of itself, create is unnecessary. perhaps this is because of F# 4? *) | |
let getOrDefault n v = match n with NullableValue x -> x | _ -> v | |
let getValueOrDefault n = match n with NullableValue x -> x | Null -> n.GetValueOrDefault() | |
let getOrElse (n: 'a Nullable) (v: 'a Lazy) = match n with NullableValue x -> x | _ -> v.Force() | |
let get (x: _ Nullable) = x.Value | |
let fromOption = Option.toNullable | |
let toOption = Option.fromNullable | |
let bind f x = | |
match x with | |
| Null -> Nullable() | |
| NullableValue v -> f v | |
let hasValue (x: _ Nullable) = x.HasValue | |
let isNull (x: _ Nullable) = not x.HasValue | |
let count (x: _ Nullable) = if x.HasValue then 1 else 0 | |
let fold f state x = | |
match x with | |
| Null -> state | |
| NullableValue v -> f state v | |
let foldBack f x state = | |
match x with | |
| Null -> state | |
| NullableValue _ -> f x state | |
let exists p x = | |
match x with | |
| Null -> false | |
| NullableValue _ -> p x | |
let forall p x = | |
match x with | |
| Null -> true | |
| NullableValue _ -> p x | |
let iter f x = | |
match x with | |
| Null -> () | |
| NullableValue v -> f v | |
let map f x = | |
match x with | |
| Null -> Nullable() | |
| NullableValue v -> Nullable(f v) | |
let toArray x = | |
match x with | |
| Null -> [||] | |
| NullableValue v -> [| v |] | |
let toList x = | |
match x with | |
| Null -> [] | |
| NullableValue v -> [v] | |
let liftNullable op (a: _ Nullable) (b: _ Nullable) = | |
if a.HasValue && b.HasValue | |
then Nullable(op a.Value b.Value) | |
else Nullable() | |
let mapBoolOp op a b = | |
match a,b with | |
| NullableValue x, NullableValue y -> op x y | |
| _ -> false | |
let bindf (n: _ Nullable) f ``default`` = if n.HasValue then f n.Value else ``default`` | |
// helpful in eliminating parenthesis | |
type DateTime with | |
//member x.nullable = Nullable x | |
static member n = Nullable<DateTime>() | |
type Int32 with | |
static member n = Nullable<Int32>() | |
type Guid with | |
static member n = Nullable<Guid>() | |
type Microsoft.FSharp.Core.int< 'Measure > with | |
member x.n = Nullable (int x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment