0

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!

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

It definitely works for number? and such, I never mentioned anything because I assumed that instance? would work as well but it doesn't. (filter (instance? java.something) elements) just says something about "Boolean cannot be to cast to clojurer.lang.IFn)
@Deragon The argument to filter needs to be a function, not the result of calling a function. So you should create a one-argument function (either named or anonymous) that returns true if its argument is an instance of java.something and false otherwise.
For instance (filter #(instance? java.something %) elements) will work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.