Skip to content

Instantly share code, notes, and snippets.

@brunoro
brunoro / gist:5762689
Created June 12, 2013 03:45
implementation of the Wadler document algebra for strict languages described by Lindig (2000)
defmodule WadlerLindig do
# Elixir implementation of the Wadler document algebra as described in
# "Strictly Pretty" (2000) by Christian Lindig
# http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.34.2200
#
# The original Haskell implementation of the algorithm relies on lazy
# evaluation to unfold document groups on two alternatives:
# _flat_ (breaks as spaces) and _broken_ (breakes as newlines).
# Implementing the same logic on a strict language such as Elixir leads
# to an exponential growth of the possible documents, unless document
@brunoro
brunoro / benchmark.ex
Last active December 18, 2015 12:58
Pretty printer benchmark on deeply nested data structures
defmodule BenchmarkInspect do
def big_string do
s = trunc(:random.uniform * 400) + 1
String.duplicate "x", s
end
def rand_struct do
a = [
fn(x) -> { x, 1, 2 } end,
fn(x) -> { big_string, x, 2 } end,
@brunoro
brunoro / :lists.duplicate.txt
Last active December 18, 2015 15:18
Profiling results for Lists.duplicate and String.duplicate on the implementation of repeat/2 on wadler.ex
FUNCTION CALLS % TIME [uS / CALLS]
-------- ----- --- ---- [----------]
'Elixir.Wadler':group/1 5 0.00 0 [ 0.00]
'Elixir.Binary.Inspect.Utils':'-group_maybe/2-fun-0-'/1 1 0.00 0 [ 0.00]
'Elixir.Wadler':glue/2 27 0.00 1 [ 0.04]
gen:call/4 1 0.00 1 [ 1.00]
gen:do_call/4 1 0.00 1 [ 1.00]
gen_server:call/3 1 0.00 1 [ 1.00]
'Elixir.Binary.Inspect.Utils':'-group_maybe/3-fun-0-'/1 4 0.00 1 [ 0.25]
code:ensure_loaded/1 9 0.01 2 [ 0.22]
@brunoro
brunoro / plot.sh
Created June 25, 2013 20:06
Handy plotting script using gnuplot. Plots all the data on .dat files on the current directory, using the first and second columns as data.
#!/bin/bash
GPI="plot.gpi"
echo "set terminal svg size 480,320 fname 'Gill Sans' fsize 8 rounded dashed
# set background
set object 1 rect from screen 0, 0, 0 to screen 1, 1, 0 behind
set object 1 rect fc rgb 'white' fillstyle solid 1.0
# Line style for axes
set style line 80 lt 0
@brunoro
brunoro / goldwasser_micali.py
Last active June 3, 2022 09:18
An implementation of the Goldwasser-Micali cryptosystem on (HAC 8.7) - http://cacr.uwaterloo.ca/hac/
#!/usr/bin/env python
# encoding: utf8
from unicodedata import normalize
from string import ascii_letters
from random import randint
# Miller-Rabin probabilistic primality test (HAC 4.24)
# returns True if n is a prime number
# n is the number to be tested
# t is the security parameter
@brunoro
brunoro / pollard_rho.py
Created June 30, 2013 06:46
Pollard's Rho algorithm for discrete logs as described on (HAC 3.6) - http://cacr.uwaterloo.ca/hac/
#!/usr/bin/env python
# encoding: utf8
# HAC 3.61: generates x_{i+1}, a_{i+1} and b_{i+1}
# from a, b and c, partitioning the field
def step_xab(x, a, b, alpha, beta, n, Z):
s = x % 3
# S1
if s == 1:
@brunoro
brunoro / wadler_defrecordp.ex
Created July 3, 2013 05:35
Pattern matching fails with records defined using defrecordp.
defmodule WadlerLindig do
@moduledoc """
Pretty printing library inspired by Philip Wadler.
Custom pretty printers can be implemented using
functions, exported from this module.
Elixir implementation of the Wadler document algebra as described in
"Strictly Pretty" (2000) by Christian Lindig
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.34.2200
@brunoro
brunoro / memoize.ex
Created August 5, 2013 20:34
Memoization using a `defmem` macro and a gen_server.
defmodule Memoize do
alias Memoize.ResultTable
defmacro defmem(header, do: body) do
{ name, _meta, vars } = header
quote do
def unquote(header) do
case ResultTable.get(unquote(name), unquote(vars)) do
{ :hit, result } ->
@brunoro
brunoro / kepler-bouwkamp.clj
Last active December 25, 2015 13:39
Draws the inscribed polygons and circles for the prime analog of the Kepler-Bouwkamp constant using Quil.
(ns kepler-bouwkamp.core
(:use quil.core))
(def tau 6.283185307179586)
(defn circle [cx cy r]
"Draws a circle centered at (cx, cy) with radius r."
(let
[d (* r 2)]
(ellipse cx cy d d)))
@brunoro
brunoro / BezierCurveNode.h
Created August 20, 2015 15:01
Visualize Bézier curves in cocos2d
#import "cocos2d.h"
@interface BezierCurveNode : CCNode
@property (nonatomic) CGPoint origin;
@property (nonatomic) CGPoint cp1;
@property (nonatomic) CGPoint cp2;
@property (nonatomic) CGPoint destination;
@property (nonatomic) ccColor3B color;