Skip to content

Instantly share code, notes, and snippets.

View ohlawdie's full-sized avatar
😟

Ohlawdie ohlawdie

😟
  • Michigan
View GitHub Profile
@ohlawdie
ohlawdie / ValFS.fs
Created February 19, 2020 22:39
valfs
[<Struct>]
type Foo =
val mutable bar: string
member self.ChangeBar bar = self.bar <- bar
new (bar) = {bar = bar}
@ohlawdie
ohlawdie / FSExtensionMethod.fs
Created February 19, 2020 17:29
#extension #BCL #fs
type System.String with
member this.StartsWithA = this.StartsWith "A"
let inline (>>) f g x = g(f x)
Which reads as: given two functions, f and g, and a value, x, compute the result of f of x and pass that result to g. The interesting thing here is that you can curry the (>>) function and only pass in parameters f and g, the result is a function which takes a single parameter and produces the result g ( f ( x ) ).
@ohlawdie
ohlawdie / GenericMemoization.fs
Created February 18, 2020 23:50
#FS #memoization
let memoize f =
let cache = System.Collections.Generic.Dictionary<_,_>(HashIdentity.Structural)
fun x ->
let ok, res = cache.TryGetValue(x)
if ok then res
else let res = f x
cache.[x] <- res
res
@ohlawdie
ohlawdie / OneSuchCacheFunc.fs
Created February 18, 2020 23:49
#FS #Cache
let Cache (reader: 'key -> 'value) max =
let cache = new Dictionary<'key,LinkedListNode<'key * 'value>>()
let keys = new LinkedList<'key * 'value>()
fun (key : 'key) -> (
let found, value = cache.TryGetValue key
match found with
|true ->
keys.Remove value
keys.AddFirst value |> ignore
#r "System.Data.Linq"
open System
open System.Reflection
open System.ComponentModel
open System.Linq.Expressions
open Microsoft.FSharp.Reflection
open Microsoft.FSharp.Quotations
module FSharpType =
type DealResult = {
Card : Card option
Deck : Deck
}
let dealACard deck =
match deck with
| [] -> { Card = None; Deck = deck }
| card::restOfDeck -> { Card = Some card; Deck = restOfDeck }
@ohlawdie
ohlawdie / SingletonWrappedWithAttribute
Created February 18, 2020 01:14
#singleton F# #Fsharp #F#
type ConstructionAttribute(singleInstance : bool) =
inherit System.Attribute()
member this.IsSingleton = singleInstance
let singletons = new System.Collections.Generic.Dictionary<System.Type,obj>()
let make() : 'a =
let newInstance() = System.Activator.CreateInstance<'a>()
let attributes = typeof<'a>.GetCustomAttributes(typeof<ConstructionAttribute>, true)
let singleInstance =
if attributes.Length > 0 then
@ohlawdie
ohlawdie / morefencoder.fs
Created February 11, 2020 22:45
More of the F# encoder #F
namespace global
open System
open System.Collections.Generic
open System.Linq
open System.Text
open System.Threading.Tasks
open System.IO
open System.Diagnostics
namespace File_Encoder
type Program() =
@ohlawdie
ohlawdie / FSharpEncoder.fs
Created February 11, 2020 22:44
F# char encoder? #F
namespace global
open System
open System.Collections.Generic
open System.Linq
open System.Text
open System.Threading.Tasks
open System.IO
open System.Diagnostics
namespace File_Encoder
type Encoder() =