Skip to content

Instantly share code, notes, and snippets.

@jocke-l
jocke-l / gist:4b3fdef28d8eeda7acdb
Created January 25, 2015 22:50
mandelbrot colour
/*return iterations == DEPTH ? -1 :
iterations + 1 - log(log(sqrt(x*x + y*y))) / log(2);*/
/*
* Coolt mönster:*/
return (iterations == depth ? -1 :
(iterations << 4)) - log(sqrt(x*x - y*y)) / log(2);
@jocke-l
jocke-l / pi.hs
Last active December 15, 2015 11:59
A function that computes π
import Data.Number.CReal
piit accuracy = showCReal 100 (go 1 (1 / sqrt 2) (1 / 4) 1)
where go a b t p | (a - b) >= accuracy = let aa = (a + b) / 2
bb = sqrt (a * b)
tt = t - p * (a - aa)^2
pp = 2 * p
in go aa bb tt pp
| otherwise = (a + b)^2 / (4 * t)
@jocke-l
jocke-l / hangman.hs
Last active December 17, 2015 12:09
Hangman in Haskell
module Main where
import System.Random
import Data.List (intersect)
import Prelude hiding (words)
import Text.Printf
obscure word letters = map hide word
where hide c
| c `elem` (' ' : letters) = c
@jocke-l
jocke-l / Main.hs
Created December 18, 2015 08:32
hazel
module Main where
import Control.Monad (forever)
import System.Posix.Process (forkProcess, executeFile)
import System.Environment (getEnvironment)
import System.IO (hSetBuffering, stdout, BufferMode(NoBuffering))
import Data.Text (Text, pack, unpack, split)
startProcess :: Text -> [Text] -> IO ()
@jocke-l
jocke-l / enum.py
Last active December 19, 2015 17:19
Dynamic enumration function
def enum(varnames, start=0):
return dict(zip(varnames, range(start, start + len(varnames))))
@jocke-l
jocke-l / fizzbuzz.hs
Last active December 27, 2015 13:09
Functional FizzBuzz
module Main where
fizzbuzz x
| fizz x && buzz x = "FizzBuzz"
| fizz x = "Fizz"
| buzz x = "Buzz"
| otherwise = show x
where fizz = (== 0) . flip mod 3
buzz = (== 0) . flip mod 5
import argparse
import zmq
context = zmq.Context()
def bind_sockets(pub_port, pull_port):
pub = context.socket(zmq.PUB)
pub.bind('tcp://*:{}'.format(pub_port))
@jocke-l
jocke-l / utils.py
Created August 21, 2016 14:34
Currying functions in python
import inspect
def curry(func):
return Currying(func)
class Currying:
def __init__(self, function, args=None):
self._function = function
@jocke-l
jocke-l / Makefile
Last active December 5, 2016 05:03
Makefile
# This Makefile uses gcc's -MM-flag to scan your source tree for dependencies
# Other compilers may or may not have this feature. Adjust the DEPS
# definition accordingly.
CC := gcc
RM := rm
CFLAGS := -pedantic -Wall -Wextra -g
LIBS :=
@jocke-l
jocke-l / disable_signals.py
Last active January 12, 2017 16:09
Signal disabler context manager
from contextlib import contextmanager
from functools import update_wrapper
from django.db.models import signals
@contextmanager
def disable_signals():
receiver_backups = {}
signals_list = [(signal_name, signal) for signal_name, signal in