Вы, как и я, учитесь на фрагментах кода? Вот некоторые.
Давайте посмотрим на документацию map
.
user=> (doc map)
-------------------------
clojure.core/map
([f coll] [f c1 c2] [f c1 c2 c3] [f c1 c2 c3 & colls])
Returns a lazy sequence consisting of the result of applying f to the
set of first items of each coll, followed by applying f to the set
of second items in each coll, until any one of the colls is
exhausted. Any remaining items in other colls are ignored. Function
f should accept number-of-colls arguments.
nil
map
возвращает ленивую последовательность (вы уже должны прочитать ссылки, данные @noahz). Чтобы полностью реализовать ленивую последовательность (часто это не очень хорошая практика, поскольку ленивая последовательность может быть бесконечной и, следовательно, никогда не заканчиваться), вы можете использовать dorun
или doall
.
user=> (doc dorun)
-------------------------
clojure.core/dorun
([coll] [n coll])
When lazy sequences are produced via functions that have side
effects, any effects other than those needed to produce the first
element in the seq do not occur until the seq is consumed. dorun can
be used to force any effects. Walks through the successive nexts of
the seq, does not retain the head and returns nil.
nil
user=> (doc doall)
-------------------------
clojure.core/doall
([coll] [n coll])
When lazy sequences are produced via functions that have side
effects, any effects other than those needed to produce the first
element in the seq do not occur until the seq is consumed. doall can
be used to force any effects. Walks through the successive nexts of
the seq, retains the head and returns it, thus causing the entire
seq to reside in memory at one time.
nil
Хотя они кажутся похожими, это не так — обратите внимание на разницу в том, как они относятся к голове реализованной последовательности.
Обладая знаниями, вы можете влиять на то, как ленивая последовательность карты ведет себя с doall
.
user=> (defn square [x]
#_=> (println (str "Processing: " x))
#_=> (* x x))
#'user/square
user=> (doall (map square '(1 2 3 4 5)))
Processing: 1
Processing: 2
Processing: 3
Processing: 4
Processing: 5
(1 4 9 16 25)
Как вы могли заметить, я также изменил определение функции square
, так как вам не нужно do
внутри функции (это подразумевается макросом defn
).
В книге Clojure Programming есть предложение, которое может вам понравиться для случая '(1 2 3 4 5)
:
«Большинство людей просто используют векторный литерал для таких случаев, в котором выражения-члены всегда будут оцениваться».
Вместо того, чтобы копировать соответствующие разделы в поддержку этого утверждения, я скорее рекомендую книгу, поскольку она стоит потраченного времени и денег.
person
Jacek Laskowski
schedule
20.11.2012