Reputation: 305
Let's say I have a macro, inside the macro I have this let:
let[
elements# //stuff//
#newlist (for [e# elements#] (if (number? e#) (**add e# to #newlist**)))
]
Since I am having a really hard time finding proper information on the really simple Clojure stuff, I'm here to ask: what do I need to do to accomplish this task above? I think it should be possible to do this but I don't know the most crucial part!
Upvotes: 0
Views: 132
Reputation: 370162
It looks like you're trying to create a list called newlist
which contains all those elements of elements
that are numbers. If so, you can just use the filter
function like this:
(let
[elements whatever
newlist (filter number? elements)]
do-stuff-with-newlist)
filter
takes a predicate function and a list and returns a new list that contains those items for which the predicate returns a true result.
Note that your initial idea of adding to a list inside a for-loop would not work because lists are immutable, so you can not add to them in-place. You could re-assign a variable holding the list inside a for loop (something like (set! my-list (cons e my-list))
, but that would not be idiomatic Clojure code. It would also create the list in reverse.
Upvotes: 2