Skip to content

Instantly share code, notes, and snippets.

@emasaka
Created July 22, 2013 16:04
Show Gist options
  • Save emasaka/6055058 to your computer and use it in GitHub Desktop.
Save emasaka/6055058 to your computer and use it in GitHub Desktop.
(defn encoded-num-seq [n digits]
(let [base (.length digits)
enc1 (fn [n r]
(let [q (quot n base) r2 (str (.charAt digits (rem n base)) r)]
(if (zero? q) r2 (recur q r2)) ))]
(map #(enc1 % "") (iterate inc n)) ))
(defmacro nth [c i & nf]
(if (and (list? c) (= (first c) 'encoded-num-seq))
(let [(f a1 a2) c] `(first (~f (+ ~i ~a1) ~a2)))
`(clojure.core/nth ~c ~i ~@nf) ))
def enc1(n, digits, base)
r = ''
begin
r.prepend digits[n % base]
end while (n /= base) > 0
r
end
def encoded_num_seq(n, digits)
base = digits.size
(n..Float::INFINITY).lazy.map{|x| enc1(x, digits, base) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment