Skip to content

Instantly share code, notes, and snippets.

@abo-abo
Created July 19, 2013 16:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abo-abo/6040382 to your computer and use it in GitHub Desktop.
Save abo-abo/6040382 to your computer and use it in GitHub Desktop.
elisp/clojure/python basic operations comparison.

hyperpolyglot

elispclojurepython
generationnumber-sequencerangerange
vectorvector⁅⁆
make-vector make-list make-string
lengthcountlen
indexingelt nth arefnth⁅⁆
aset setcar/nthcdr-⁅⁆=
sortingsortsortsorted
reverse listnreverse reversereversereversed reverse ⁅::-1⁆
append seqs
lex numbersnotation

length

elisp

(setq x1 (length '(1 2 3)))
(setq x2 (length [1 2 3]))
(setq x3 (length "123"))
(format "x1 = %s x2 = %s x3 = %s\n" x1 x2 x3)

clojure

(def x1 (count '(1 2 3)))
(def x2 (count [1 2 3]))
(def x3 (count "123"))
(print "x1 = " x1 "x2 = " x2 "x2 = " x3)

python

x1 = len([1, 2, 3])
x2 = len([1, 2, 3])
x3 = len("123")
print "x1 = ", x1, "x2 = ", x2, "x3 = ", x3

nth

For lists only.
(setq x1 (nth 0 '(1 2 3)))
(format "x1 = %s" x1)

aref

For arrays only. Strings are arrays.
(setq x2 (aref [1 2 3] 0))
(setq x3 (aref "123" 0))
(format "x2 = %s x3 = %s\n" x2 x3)

clojure

(def x1 (nth '(1 2 3) 0))
(def x2 (nth [1 2 3] 0))
(def x3 (nth "123" 0))
(print "x1 = " x1 "x2 = " x2 "x2 = " x3)

python

x1 = [1, 2, 3][0]
x2 = [1, 2, 3][0]
x3 = "123"[0]
print "x1 = ", x1, "x2 = ", x2, "x3 = ", x3

setcar nthcdr

For lists only.
(setq x1 '(1 2 3))
(setcar (nthcdr 0 x1) 9)
(format "x1 = %s" x1)

clojure

python

Only for lists. Strings don’t support this.
x1 = [1, 2, 3]
x1[0] = 9
print "x1 = ", x1

clojure

(def x1 (range 9))
(def x2 (range 1 9))
(def x3 (range 1 9 2))
(print "x1 = " x1 "x2 = " x2 "x2 = " x3)

python

x1 = range(9)
x2 = range(1, 9)
x3 = range(1, 9, 2)
print "x1 = ", x1, "x2 = ", x2, "x3 = ", x3

generate vector from items

elisp

Note that [] notation auto-quotes.
(setq x (vector 'foo 23 [bar baz] "spam"))
(format "x = %s" x)

clojure

(def x1 '(1 3 2 6 5 4 0))
(def x2 (sort > x1))
(print "x1 = " x1 "x2 = " x2)

python

x1 = [1, 3, 2, 6, 5, 4, 0]
x2 = sorted(x1, cmp=lambda x,y: x-y, reverse = True)
print "x1 = ", x1, "x2 = ", x2

python

non-destructive

x1 = ["a", "b", "c"]
x2 = [i for i in reversed(x1)]
print "x1 = ", x1, "x2 = ", x2

destructive

x1 = ["a", "b", "c"]
x1.reverse()
print "x1 = ", x1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment