Skip to content

Instantly share code, notes, and snippets.

View jcmrva's full-sized avatar

Josh M jcmrva

View GitHub Profile
@bohdanszymanik
bohdanszymanik / activePatternDateMatch.fsx
Created March 27, 2018 22:20
Active pattern date matching
// I like this active pattern stuff
open System.Globalization
let (|ShortDate|_|) s =
match DateTime.TryParseExact(s, "ddMMMyyyy", CultureInfo.InvariantCulture, DateTimeStyles.None) with
| true, d -> Some d
| _ -> None
let (|LongDate|_|) s =
@porglezomp
porglezomp / Leftpad.idr
Last active August 13, 2024 22:22
Taking on Hillel's challenge to formally verify leftpad (https://twitter.com/Hillelogram/status/987432181889994759)
-- Note: There is a more complete explanation at https://github.com/hwayne/lets-prove-leftpad/tree/master/idris
import Data.Vect
-- `minus` is saturating subtraction, so this works like we want it to
eq_max : (n, k : Nat) -> maximum k n = plus (n `minus` k) k
eq_max n Z = rewrite minusZeroRight n in rewrite plusZeroRightNeutral n in Refl
eq_max Z (S _) = Refl
eq_max (S n) (S k) = rewrite sym $ plusSuccRightSucc (n `minus` k) k in rewrite eq_max n k in Refl
-- The type here says "the result is" padded to (maximum k n), and is padding plus the original
@realvictorprm
realvictorprm / PaketDependencyManagementMadeEasy.fsx
Last active June 12, 2018 19:41
A must have for your script file to ease spreading a small proof of concept with dependencies!
open System
open System.IO
open System.Diagnostics
let downloadDependencies deps =
Environment.CurrentDirectory <- __SOURCE_DIRECTORY__
if not (File.Exists "paket.exe") then
async {
let url = "http://fsprojects.github.io/Paket/stable"
@jfrank-summit
jfrank-summit / Constrained.Types.fs
Last active April 15, 2019 17:35
F# Signature File - Constrained Types
module StringConstraints
open System
let stringPattern str pattern =
if String.IsNullOrEmpty(str) then
let msg = sprintf "%s: Must not be null or empty" str
Error msg
elif System.Text.RegularExpressions.Regex.IsMatch(str, pattern) then
Ok (str)
else
let msg = sprintf "'%s' must match the pattern '%s'" str pattern
@isaacabraham
isaacabraham / io-monad.fsx
Last active November 1, 2022 23:58
F# port of the first half of John De Goes "FP to the max" (https://www.youtube.com/watch?v=sxudIMiOo68)
#load @".paket\load\net452\FSharpPlus.fsx"
open FSharpPlus
open System
[<AutoOpen>]
module rec IO =
let run (IO computation) = computation()
type IO<'T> =
@jdh30
jdh30 / Q_rsqrt.fs
Last active October 13, 2018 14:01
Fast inverse sqrt in F#
// The famous fast inverse square root approximation from
// Quake ported to F#.
// Note: this algorithm is not fast on modern computers
// and this implementation of it is extremely inefficient:
// for educational purposes only!
open System
let isqrt y =
let x2 = y * 0.5f
@isobel-cullen
isobel-cullen / morse.fs
Last active August 18, 2021 08:56
I got distracted by someone's morse code ringtone at work
[<Measure>] type ms
let dotLength = 70<ms>
let dot = async { Console.Beep(1000, int dotLength) }
let dash = async { Console.Beep(1000, int dotLength * 3) }
let blipSpace = async { do! Async.Sleep (int dotLength) }
// * 2 since there will be a blip space
let charSpace = async { do! Async.Sleep (int dotLength * 6) }
// * 4 as there will be a blip (1) and a char (2)
@fnky
fnky / ANSI.md
Last active May 28, 2025 21:53
ANSI Escape Codes

ANSI Escape Sequences

Standard escape codes are prefixed with Escape:

  • Ctrl-Key: ^[
  • Octal: \033
  • Unicode: \u001b
  • Hexadecimal: \x1B
  • Decimal: 27
@gregberns
gregberns / the-algebra-of-algebraic-data-types.md
Created December 13, 2018 05:57
The Algebra of Algebraic Data Types, Part 1, by Chris Taylor
// This is am example of an immediate write / random access cursor for Excel with basic formatting options.
// Implementation is based on a concrete, non generic writer monad with no payload ("do!"" only) (only state).
// Instead of directl writing to excel, an alternatives would be a random acces to a
// copy-on-write list (or even a mutable array) and then bulk-write the result to excel in one shot.
// When only forward access would have been required, a simple seq expression with yields would have been enough.
// Anyway, it is a demonstration on how to "hide" pseudo-mutable state that is passed through a computation.
//
// I personally use it for generating reports based on various data sources.