Skip to content

Instantly share code, notes, and snippets.

@thiagoa
Forked from gus/index.txt
Created December 30, 2019 14:28
Show Gist options
  • Save thiagoa/b9137d45be3aef2c4878ea7cf9107c45 to your computer and use it in GitHub Desktop.
Save thiagoa/b9137d45be3aef2c4878ea7cf9107c45 to your computer and use it in GitHub Desktop.
Ruby/Clojure analogs
For each Ruby module/class, we have Ruby methods on the left and the equivalent
Clojure functions and/or relevant notes are on the right.
For clojure functions, symbols indicate existing method definitions, in the
clojure namespace if none is explicitly given. clojure.contrib.*/* functions can
be obtained from http://github.com/kevinoneill/clojure-contrib/tree/master,
ruby-to-clojure.*/* functions can be obtained from the source files in this
gist.
If no method symbol is given, we use the following notation:
[name] Ruby method is an alias for Ruby method [name], see that method
[Enum] Ruby method is also defined in Enumerable, see the equivalent there
des! Ruby method is destructive, an alternative may be suggested
ni Ruby method doesn't have a direct equivalent, an alternative may be given
uf Ruby method is IMO an "unfeature", so no equivalent is given
lib Ruby method is not fundamental to data structure, belongs in other lib
Right now we only have Enumerable and Array, though we hope to have Set, Hash,
and String/Regexp eventually.
Note that Clojure data structures and the associated sequence functions are
fundamentally different from the Ruby data structures and methods because the
Clojure data structures are always persistent and most of the sequence functions
are lazy. You can get started on the Ruby => Clojure road with a guide like
this, but to fully leverage Clojure you will need to understand persistence and
laziness.
== Enumerable
each doseq (use for side effects only)
all? every?
any? some
collect [map]
count count
cycle cycle
detect some
drop drop
drop_while drop-while
each_cons partition
each_slice partition
each_with_index ruby-to-clojure.seq-utils/map-with-index
entries [to_a]
find [detect]
find_all [select]
find_index ruby-to-clojure.seq-utils/some-index
first first, take
grep ruby-to-clojure.seq-utils/grep
group_by clojure.contrib.seq-utils/group-by
include? clojure.contrib.seq-utils/includes?
index_by clojure.set/index or ruby-to-clojure.seq-utils/index-by
inject [reduce]
map map
max last (if ordered)
note that clojure/max acts on numerical args, not seqs
max_by ni (use sorted data structure with last)
member? [include?]
min first (if ordered)
note that clojure/min acts on numerical args, not seqs
min_by ni (use sorted data structure with first)
minmax first and last
minmax_by ni (see max_by and min_by above)
none? not-any?
one? ruby-to-clojure.seq-utils/exactly? /one?
partition clojure.contrib.seq-utils/separate
reduce reduce
reject remove
reverse_each rseq, reverse
select filter
sort sort, sort-by
sort_by sort-by
take take
take_while take-while
to_a seq, list, vector
to_set set
zip ruby-to-clojure.seq-utils/zip
== ARRAY
& ni (use clojure.set/intersection on sets)
* ruby-clojure.seq-utils/cycle
+ concat
- ni (use clojure.set/difference on sets)
<< des! (use clojure/conj)
<=> compare
== =
[] get
[]= des! (use clojure/assoc)
abbrev lib
assoc uf
at [[]]
choice ruby-to-clojure.seq-utils/choice
clear des! (use empty)
collect [map]
collect! [map!]
combination lib
compact ruby-to-clojure.seq-utils/compact
compact! !des
concat !des (use concat)
count count
cycle [*]
dclone des!
delete des!
delete_at des!
delete_if des! (use remove)
drop [Enum]
drop_while [Enum]
each [Enum]
each_index (range (count arr))
empty? empty?
eql? =
fetch get
fill des!
find_index [Enum]
first [Enum]
flatten clojure.contrib.seq-utils/flatten
flatten! des!
frozen? uf
hash hash
include? [Enum]
index [find_index]
initialize_copy uf
insert des!
inspect print-str
join clojure.contrib.str-utils/str-join
last last, (take (rseq coll))
length [count]
map map
map! des!
new vecotr
pack lib
permutation clojure.contrib.lazy-seqs/permutations
pop des! (use pop)
pretty_print lib
product lib
push [<<]
rassoc uf
reject [Enum]
reject! des!
replace des!
reverse rseq, reverse
reverse! des!
reverse_each [reverse]
rindex uf
select [Enum]
shelljoin lib
shift des! (use first, rest)
shuffle ruby-to-clojure.seq-utils/shuffle
shuffle! des!
size [count]
slice subvec
slice! des!
sort [Enum]
sort! des!
take [Enum]
take_while [Enum]
to_a seq
to_ary seq
to_csv lib
to_s [inspect]
to_yaml lib
transpose lib
try_convert uf
uniq distinct
uniq! des!
unshift des!
values_at get, subvec
yaml_initialize lib
zip [Enum]
| ni (use clojure.set/union on sets)
(ns ruby-to-clojure.seq-utils)
(defn map-with-index
"Like clojure/map, but f should accept 2 arguments: the element and its index
in the collection."
[f coll]
(map f coll (iterate inc 0)))
(defn some-index
"Returns the index of the first element of coll for which (f elem) returns
logical truth, or nil of no such element exists."
[f coll]
(loop [rcoll coll index 0]
(when (seq rcoll)
(if (f (first rcoll))
index
(recur (rest rcoll) (inc index))))))
(defn grep
"Returns a lazy seq of strings in coll that match the given pattern."
[re coll]
(filter #(re-find re %) coll))
(defn index-by
"Returns a map from the results of invoking f on elements of coll
to the corresponding elements. The order of elements in the values of the map
reflects their order in the original collection, and there may be duplicates
if there were duplicates in the collection."
[f coll]
(reduce
(fn [map x]
(let [k (f x)]
(assoc map k (conj (get map k []) x))))
{}
coll))
(defn exactly?
"Returns true if (f x) returns logical truth for exactly n xs in coll."
[n f coll]
(loop [m n more coll]
(when (seq more)
(if (f (first more))
(if (= m 1)
(not-any? f (rest more))
(recur (dec m) (rest more)))
(recur m (rest more))))))
(defn one?
"Returns true if (f x) returns logical truth for exactly 1 x in coll."
[f coll]
(exactly? 1 f coll))
(defn remove
"Returns a lazy seq: (filter (complement pred) coll)."
[pred coll]
(filter (complement pred) coll))
(defn zip
"Returns a sequence of sequences where the jth element of the ith inner
sequence corresponds to the ith element in the jth sequence in coll."
[& colls]
(apply map (fn [& xs] xs) colls))
(defn ncycle
"Returns a lazy seq of repetitions of the items in coll n times."
[n coll]
(take (* n (count coll)) (cycle coll)))
(defn choice
"Returns a random element from the given vecotr."
[#^clojure.lang.IPersistentVector vec]
(get vec (rand-int (count vec))))
(defn compact
"Returns a lazy seq of non-nil elements in coll."
[coll]
(remove nil? coll))
; http://groups.google.com/group/clojure/browse_thread/thread/180842eb58c58370/43239b0de45a5d56?lnk=gst&q=shuffle#
(defn shuffle
"Returns a shuffled seq for the given coll."
[coll]
(let [l (java.util.ArrayList. coll)]
(java.util.Collections/shuffle l)
(seq l)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment