Skip to content

Instantly share code, notes, and snippets.

View bitops's full-sized avatar

Sebastian Wittenkamp bitops

View GitHub Profile
@bitops
bitops / rhickey_primes.clj
Created December 29, 2011 11:50
Rich Hickey's Prime number sieve using Java Interop
;; from http://paste.lisp.org/display/69952
(defn sieve [n]
(let [n (int n)]
"Returns a list of all primes from 2 to n"
(let [root (int (Math/round (Math/floor (Math/sqrt n))))]
(loop [i (int 3)
a (int-array n)
result (list 2)]
(if (>= i n)
@bitops
bitops / digitsum.clj
Created January 2, 2012 01:02
My implementation of a digit sum function in Clojure (using Java interop)
(defn digit-sum [val] (apply + (map #(Integer. (str %)) (str val))))
@bitops
bitops / digit_sum.rb
Created January 7, 2012 05:16
Digit sum in Ruby 1.9.2
def digit_sum(n)
n.to_s.each_char.map {|c| c.to_i }.reduce(:+)
end
@bitops
bitops / factorial.rb
Created January 7, 2012 05:20
Factorial in Ruby 1.9.2
def factorial(n)
(1..n).reduce(:*)
end
@bitops
bitops / params.rb
Created January 10, 2012 03:15
optional params and blocks
def f(x, y=nil, &block)
puts x
puts y
block.call
end
f(1) { puts "inside block" }
f(1, 2) { puts "inside block" }
@bitops
bitops / log_reader.rb
Created January 28, 2012 05:01
Bad file descriptors
class LogReader
def initialize(f)
@f = f
end
def process
yield(@f.read)
end
@bitops
bitops / commit_msg.txt
Created February 1, 2012 21:49
From Linus: what a good commit message looks like.
Header line: explaining the commit in one line
Body of commit message is a few lines of text, explaining things
in more detail, possibly giving some background about the issue
being fixed, etc etc.
The body of the commit message can be several paragraphs, and
please do proper word-wrap and keep columns shorter than about
74 characters or so. That way "git log" will show things
nicely even when it's indented.
@bitops
bitops / hack.sh
Created April 24, 2012 19:03 — forked from erikh/hack.sh
OSX For Hackers
#!/usr/bin/env sh
##
# This is script with usefull tips taken from:
# https://github.com/mathiasbynens/dotfiles/blob/master/.osx
#
# install it:
# curl -sL https://raw.github.com/gist/2108403/hack.sh | sh
#
@bitops
bitops / MediaPlayerStateWrapper.java
Created April 29, 2012 01:00 — forked from danielhawkes/MediaPlayerStateWrapper.java
A drop-in replacement for a MediaPlayer instance, that provides an accessor for the current state.
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.AbstractCollection;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import android.media.AudioManager;
import android.media.MediaPlayer;
@bitops
bitops / block_comment.rb
Created May 17, 2012 19:42
Ruby block comment
=begin
This is how you do block comments in Ruby.
=end
puts "#{2 + 2} equals 4"