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!

Deragon
  • 305
  • 1
  • 4
  • 13

1 Answers1

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.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • 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 Jan 20 '13 at 15:47
  • 2
    @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. – sepp2k Jan 20 '13 at 15:52
  • For instance (filter #(instance? java.something %) elements) will work. – NielsK Jan 21 '13 at 20:44