Skip to content

Instantly share code, notes, and snippets.

View phipsgabler's full-sized avatar

Philipp Gabler phipsgabler

View GitHub Profile
@phipsgabler
phipsgabler / mhs.py
Last active October 21, 2015 20:48
Minimal hitting sets
#!/usr/bin/env python3
# -*- truncate-lines: t; -*-
# Yes, it is slow. But sufficient for small sets.
# Usage:
#
# > import mhs
# > list(mhs.mhs({9,11}, {2,3,6,9,10}))
# [frozenset({9}),
# frozenset({2, 11}),
@phipsgabler
phipsgabler / tree.hs
Created December 23, 2015 21:16
Creating preorder tree by folding list
data Tree a = Leaf | Branch a (Tree a) (Tree a) deriving Show
insertLeftBalanced :: a -> Tree a -> Tree a
insertLeftBalanced x Leaf = Branch x Leaf Leaf
insertLeftBalanced x (Branch m Leaf r) = Branch m (Branch x Leaf Leaf) r
insertLeftBalanced x (Branch m l Leaf) = Branch m l (Branch x Leaf Leaf)
insertLeftBalanced x (Branch m l r) = Branch m (insertLeftBalanced x l) r
listToTree :: [a] -> Tree a
listToTree xs = foldl (flip insertLeftBalanced) Leaf xs
@phipsgabler
phipsgabler / qqline.r
Last active February 27, 2017 17:02
A new stat for ggplot2 creating the comparison line in a qq-plot.
library(ggplot2)
qq.line <- function(data, qf, na.rm) {
# from stackoverflow.com/a/4357932/1346276
q.sample <- quantile(data, c(0.25, 0.75), na.rm = na.rm)
q.theory <- qf(c(0.25, 0.75))
slope <- diff(q.sample) / diff(q.theory)
intercept <- q.sample[1] - slope * q.theory[1]
list(slope = slope, intercept = intercept)
@phipsgabler
phipsgabler / free_monads.js
Last active June 15, 2016 22:02
A rough implementation of free monads in the style of Haskell's operational package
'use strict';
// see: http://hackage.haskell.org/package/operational-0.2.3.2/docs/Control-Monad-Operational.html
// run in node, e.g.:
// > let free = require('./free_monads.js')
// undefined
// > free.main()
// Log: foo + bar: 52
// test 1: [object Object]
// k: 10, v: foo
module Transducers
import Control.Category
import Control.Arrow
data Transducer : Type -> Type -> Type where
T : ({r : Type} -> (b -> r -> r) -> (a -> r -> r)) -> Transducer a b
instance Category Transducer where
id = T (\cons => cons)
// TO SEE THE PRINTLN'S, RUN `showExamples` IN THE CONSOLE
package object example {
def showExamples(): Unit = {
// # Basics
// > Goal of language: modular, functional, OO (+ Java interop)
// > Sophisticated hierarchies, type system, and flexible syntax
// # Classes
// ## Normal & abstract ones
@phipsgabler
phipsgabler / huffman_creation.ipynb
Created June 1, 2017 09:30
Toy implementation and showcase for simple Huffman encoding
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@phipsgabler
phipsgabler / ml.jl
Created October 20, 2017 16:23
Likelihoods for parameter of uniform distribution; see https://stats.stackexchange.com/q/309001
using Distributions
using Gadfly
function test()
@show θ = 100 * rand()
@show X = rand(Uniform(-θ, θ), 100)
ls = 0.1:0.5:100.0
plot(x = ls,
y = broadcast(l -> loglikelihood(Uniform(-l, l), X), ls),
@phipsgabler
phipsgabler / sigma.jl
Last active February 28, 2018 10:36
Optimizing implementation of sigma function
using Primes
using BenchmarkTools
function σ{T}(k, n, ::Type{T} = Int)
result = ones(T, n - 1)
for p in primes(n)
bound = round(Int, log(n) / log(p), RoundDown)
pᵏ = convert(T, p)^k
pᵏⁱ = one(T)

Keybase proof

I hereby claim:

  • I am phipsgabler on github.
  • I am phipsgabler (https://keybase.io/phipsgabler) on keybase.
  • I have a public key ASCsQ0akmVrVQzjf8wiXxDZf1qwTWGaGERmvjA8pxMEDmQo

To claim this, I am signing this object: