Skip to content

Instantly share code, notes, and snippets.

View sycue's full-sized avatar

sycue

View GitHub Profile
@sycue
sycue / NextHammingWeight
Created September 14, 2016 01:35 — forked from crsaracco/NextHammingWeight
NextHammingWeight - a function for finding the next int with the same hamming weight
/* I was searching for an algorithm that gave you the next integer (counting
* upwards) that has the same hamming weight as the current number, and I
* couldn't find anything. So here's a function that does that for you. If
* there is no 'next number' (i.e. this is the largest number that has this
* hamming weight), it simply returns the same number again.
*
* This can probably be optimized; I whipped it up pretty quickly. Relatedly, I
* only tested a few edge cases I could think of, so if you use this, be sure
* to test it more thoroughly.
*
@sycue
sycue / lithp.rb
Created January 5, 2013 05:24 — forked from p8/lithp.rb
class Lisp
Fs = {
:label => lambda {|name,value| Fs[name] = lambda { value } },
:car => lambda {|sexp| sexp.first },
:cdr => lambda {|sexp| sexp.slice(1, sexp.size) },
:cons => lambda {|head,tail| [head] + tail },
:atom => lambda {|sexp| !sexp.is_a?(Array) },
:eq => lambda {|a,b| a == b },
:if => lambda {|cnd,thn,els| cnd ? thn : els }
}