Error while trying to get first element of sequence by index in a function call

Viewed 234

I've encountered a problem while I was doing a task from 4clojure.com. Here is the description of a task:

Write a function which returns the last element in a sequence.

I've solved it using the following code:

#(first (reverse %))

When I wanted to change the first function with a number of an index. like so:

#(0 (reverse %))

I've received an error:

java.lang.ClassCastException: java.lang.Long cannot be cast to clojure.lang.IFn

My question is: Why am I receiving this error? I cannot get it, because for instance ([1 2 3 4] 0) is perfectly valid and returns the first element of a sequence so why I cannot use index of an array in the function?

EDIT1: Even the following code does not work and I suppose APersistentVector is first there.

#((reverse %) 0)

EDIT2: I managed to make it work by converting the list which is returned from reverse function to vector. Thanks @Josh

(#((vec (reverse %)) 0)[1 2 3])
1 Answers
Related