Skip to content

Instantly share code, notes, and snippets.

View SchlenkR's full-sized avatar

SchlenkR SchlenkR

View GitHub Profile
@SchlenkR
SchlenkR / New-Paket.ps1
Last active February 14, 2019 20:01
Get the f# paket bootstrapper via a PowerShell Cmdlet
#
# from: https://devonburriss.me/up-and-running-with-paket
#
# If you find yourself needing to setup Paket often as can happen if you are using F# fsx scripting files often,
# you may want to create an easier to remember command. The easiest way to do this is to add a function call to
# your Powershell profile.
#
# Edit "C:\Users\<your username>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1" on Windows
# or ~/.config/powershell/profile.ps1 on Mac and add the following function:
// 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.
type RowPointer = int
type Writer = Writer of (RowPointer -> RowPointer)
let run (f:Writer) = match f with | Writer f -> f
type Cursor() =
member this.Bind(m: Writer, f: unit -> Writer) =
Writer(fun rp ->
let res = f() |> run
let newRp = rp |> run m
// extend a type from another assembly with 'combine'
type List<'a> with
static member combine (f, x: List<'a>) : List<'a> = x @ [f]
// extend another type from another assembly with 'combine'
type System.Int32 with
static member combine (f, x: System.Int32) = x + f
// extend a type from this assembly with 'combine'
open System.IO
let curry (f: (_ * _ -> _)) a b = f (a,b)
let curry2 = curry
let curry3 (f: (_ * _ * _ -> _)) a b c = f (a,b,c)
let curry4 (f: (_ * _ * _ * _ -> _)) a b c d = f (a,b,c,d)
let ( <+> ) a b = Path.Combine(a, b)
@SchlenkR
SchlenkR / alphaVantage.fsx
Created June 14, 2019 07:46
Some http calls for alphavantage
// paket FsHttp
open FSharp.Data
open FSharp.Data.JsonExtensions
open FsHttp
open FsHttp.Dsl
let call s =
open System
open System.Reflection
open System.Collections
open System.Collections.Generic
// TODO: Allow linebreaks in cells for strings / sequences?
module Config =
@SchlenkR
SchlenkR / cata.fsx
Last active August 15, 2019 18:07
cata
// Grenzen?
let toRoman arabic =
let repeatChar char num =
List.init num id
|> List.fold (fun curr _ -> sprintf "%s%s" char curr) ""
let zehnerUndHöher = arabic / 10
let einer = arabic % 10
@SchlenkR
SchlenkR / 1_python_reduceOnly.py
Last active September 3, 2019 05:47
Funky Roman
from functools import reduce
def convertToRoman(num):
roman_factors = [(1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'), (100, 'C'), (90, 'XC'), (50, 'L'), (40, 'XL'), (10, 'X'), (9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I') ]
return reduce(lambda state, factor: (state[0] % factor[0], state[1] + factor[1] * (state[0] // factor[0])), [(num, '')] + roman_factors)[1]
print(convertToRoman(2049))
type 'a Foo = Foo of 'a
module Foo =
let ofValue (a : 'a) : 'a Foo = Foo a
let apply (a : 'a Foo) (f : ('a -> 'b) Foo) : 'b Foo =
let (Foo f) = f
let (Foo a) = a
Foo (f a)