Skip to content

Instantly share code, notes, and snippets.

@artur-s
artur-s / GitAliases.txt
Last active December 16, 2025 06:14
My Git aliases
git config alias.browse '!start `git config remote.origin.url`'
git config --global alias.df diff
git config --global alias.dft difftool
git config --global alias.mgt mergetool
git config --global alias.logg 'log --abbrev-commit --decorate --oneline --graph -30'
git config --global alias.logga 'log --abbrev-commit --decorate --oneline --graph -30 --all'
git config --global alias.ss 'status -s'
git config --global alias.co checkout
@artur-s
artur-s / OptionHelpers.fs
Last active August 29, 2015 14:07
Option helper functions
module Option
let Else second = function
| Some _ as first -> first
| None -> second
let ofNullable = function
| null -> None
| i -> Some i
@artur-s
artur-s / WebJobShutdownWatcher.fs
Last active August 29, 2015 14:15
Allows to handle an event that fires when a WebJob process is about to close
module WebJobShutdownWatcher
open System
open System.IO
/// Allows to handle an event that fires when a WebJob process is about to close.
/// Based on a blog-post: http://blog.amitapple.com/post/2014/05/webjobs-graceful-shutdown
let subscribe handler =
let shutdownFile = Environment.GetEnvironmentVariable "WEBJOBS_SHUTDOWN_FILE"
@artur-s
artur-s / tryParse.fsx
Created April 19, 2016 05:09
A function with constrained return type that implements static TryParse method
// simplified version a snipped found here: http://fssnip.net/sT
let inline tryParse text : ^a option =
let mutable result = Unchecked.defaultof<_>
if (^a : (static member TryParse: string * ^a byref -> bool) (text, &result))
then Some result
else None
// usage
@artur-s
artur-s / versionETags.fsx
Last active August 12, 2016 17:12
Encoding version numbers (integers) as ETags using SHA-1
open System
open System.Security.Cryptography
let hasher = SHA1.Create()
let intToSHA1 (n:int) =
let hexString bytes =
[for b:byte in bytes -> b.ToString("x2")] |> String.concat ""
n |> (BitConverter.GetBytes >> hasher.ComputeHash >> hexString)
@artur-s
artur-s / operators.fs
Last active November 13, 2016 04:03
Operators for matching option and nullable
module Option =
let (|??) = defaultArg
let (|?) nullable d = if isNull nullable then d else nullable
// usage
open Option
[] |> List.tryHead |?? 0
@artur-s
artur-s / KleisliComposition.fsx
Created November 12, 2016 23:23
An example of Kleisli composition
// An example of Kleisli composition
let (>>=) m f = Option.bind f m
let (>=>) mf mg = fun x -> mf x >>= mg
let tryParseInt s =
match System.Int32.TryParse s with
| true, v -> Some v
| _ -> None
@artur-s
artur-s / FsCheckMoneyGen.fsx
Created November 13, 2016 03:58
FsCheck generator for positive decimals constrained to more realistic money amounts
open System
open FsCheck
let positiveDecimal = gen {
let! low = Gen.elements [0..1000]
let! mid = Gen.elements [0..100]
let! scale = Gen.elements [0..5]
return!
Gen.elements [
Decimal(low, mid, 0, false, byte scale)
@artur-s
artur-s / functorsComposition.fsx
Created March 19, 2017 17:59
Composing functors is for free
let optionListMap f = (Option.map >> List.map) f
let square x = x * x
let squareLifted = optionListMap square
squareLifted [Some 2; None; Some 3]
@artur-s
artur-s / ListDiffWithDups.fsx
Last active November 4, 2017 16:13
Diffs two lists preserving duplicates
module List
let rec diffDups minuend subtrahend =
let rec removeFirst i = function
| h::t -> if h = i then t else h::(removeFirst i t)
| [] -> []
match subtrahend with
| h::t -> diffDups (removeFirst h minuend) t