Skip to content

Instantly share code, notes, and snippets.

View goswinr's full-sized avatar

Goswin Rothenthal goswinr

View GitHub Profile
@kylexlau
kylexlau / del_empty_dirs.py
Created January 6, 2012 05:31
Python delete empty directories, recursive algorithm
import os
def del_empty_dirs(s_dir,f):
b_empty = True
for s_target in os.listdir(s_dir):
s_path = os.path.join(s_dir, s_target)
if os.path.isdir(s_path):
if not del_empty_dirs(s_path):
b_empty = False
@vivekhaldar
vivekhaldar / church.py
Created April 21, 2012 17:11
Church numerals in Python
#! /usr/bin/python
#
# Church numerals in Python.
# See http://en.wikipedia.org/wiki/Church_encoding
#
# Vivek Haldar <vh@vivekhaldar.com>
#
# https://gist.github.com/2438498
zero = lambda f: lambda x: x
anonymous
anonymous / gist:4409734
Created December 29, 2012 22:32
F# inline and effects on performance
open System
open System.Collections.Generic
[<Struct>]
type Pair<'a when 'a: equality> =
val v: 'a
val w: 'a
new(x,y)= {v=x;w=y}
type Tone =
| Rest = 0
| GbelowC = 196
| A = 220
| Asharp = 233
| B = 247
| C = 262
| Csharp = 277
| D = 294
| E = 330
@jbtule
jbtule / NullCoalesce.fs
Last active May 13, 2022 16:38
Null Coalesce Operator for F# (|??), works with option, Nullable, and c# reference types
//inspired by http://stackoverflow.com/a/2812306/637783
type NullCoalesce =
static member Coalesce(a: 'a option, b: 'a Lazy) = match a with Some a -> a | _ -> b.Value
static member Coalesce(a: 'a Nullable, b: 'a Lazy) = if a.HasValue then a.Value else b.Value
static member Coalesce(a: 'a when 'a:null, b: 'a Lazy) = match a with null -> b.Value | _ -> a
let inline nullCoalesceHelper< ^t, ^a, ^b, ^c when (^t or ^a) : (static member Coalesce : ^a * ^b -> ^c)> a b =
((^t or ^a) : (static member Coalesce : ^a * ^b -> ^c) (a, b))
@rflechner
rflechner / Huffman.fs
Last active September 29, 2023 07:30
A FSharp implementation of Huffman compression algorithm
module Huffman
open System
open System.IO
type bit = bool
type path = bit list
type BinaryTreeNode =
| Leaf of byte * frequency:int
@ptrelford
ptrelford / JsonParser.fs
Last active May 7, 2018 14:05
JSON Parser
type json =
| Number of float
| String of string
| Boolean of bool
| Array of json list
| Object of (string * json) list
| Null
static member (?) (this,name:string) =
match this with
| Object xs -> xs |> List.find (fst >> (=) name) |> snd
@jesterKing
jesterKing / common.fs
Created June 1, 2016 21:56
Grasshopper F# sample code. Note that the code in this gist isn't complete - it probably won't compile. But it should show the basic idea of what goes in a F# component for Grasshopper.
namespace CommonStuff
open Microsoft.FSharp.Reflection
open System
open System.Text
open System.Collections.Generic
open System.Linq
open System.Drawing
open System.Windows.Forms
@mrange
mrange / fsharp_advent_2016_12_10.md
Last active December 14, 2019 21:44
F# Advent 2016 (English) - December 10 - Implementing a persistent hash map.
@mrange
mrange / data_performance.md
Last active January 4, 2020 10:16
On the topic of data locality and performance

On the topic of data locality and performance

Full source code can be found here

It is well-known that a hard disk has a long delay from that we request the data to that we get the data. Usually we measure the hard disk latency in milliseconds which is an eternity for a CPU. The bandwidth of a hard disk is decent good as SSD:s today can reach 1 GiB/second.

What is less known is that RAM has the same characteristics, bad latency with good bandwidth.

You can measure RAM latency and badndwidth using Intel® Memory Latency Checker. On my machine the RAM latency under semi-high load is ~120 ns (The 3r:1w bandwidth is 16GiB/second). This means that the CPU on my machine has to wait for ~400 cycles for data, an eternity.