Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Ruby is beautiful

Name: Anonymous 2009-08-26 15:25

class Cycle
    def cycle(*args)
      args[((@_cycles ||= Hash.new(-1))[args.hash] += 1) % args.size]
    end
end

More beautiful code plees?

Name: Anonymous 2009-08-27 0:51

>>1
If what >>12 said was right, here's how to do it in CL:

(let ((ht (make-hash-table :test #'equal)))
  (defun cycle (&rest args)
    (unless (gethash args ht)
      (setf (gethash args ht) args))
    (pop (gethash args ht))))


And test run:

CL-USER> (cycle :a :b :c) ; => :A
CL-USER> (cycle :a :b :c) ; => :B
(cycle :a :b :c) ; => :C
(cycle :a :b :c) ; => :A
(cycle :a :b :c) ; => :B
(cycle :a :b :c) ; => :C
(cycle :a :b :c :d) ; => :A
(cycle :a :b :c :d) ; => :B
(cycle :a :b :c :d) ; => :C
(cycle :a :b :c :d) ; => :D
(cycle :a :b :c :d) ; => :A
(cycle :a :b :c :d) ; => :B
...

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List