Welcome! Please see the About page for a little more info on how this works.

0 votes
in Errors by

I've been working on a script and I don't understand what this error means.

My function, takes a number (id) and extract the first element of a list in my vector.

Details :

My function :

 (defn getnameonly [number] 
  (doall (nth (
         (filter #(= number (first %)) customers) 

where customers is :
[1 (John Smith 123 Here Street 456-4567)]
[2 (Sue Jones 43 Rose Court Street 345-7867)]
[3 (Fan Yuhong 165 Happy Lane 345-4533)]

So basically, I want to pull out (return value) "John Smith" if I give 1 to my function.

The code :

(nth (
             (filter #(= number (first %)) customers) 

works fine outside the function. However, when I call :

(println getnameonly 1)

I get :

#object[db$getnameonly 0x66908383 db$getnameonly@66908383] 1

Any idea why and how to solve this issue?

2 Answers

+1 vote

First - is that an extra layer of parentheses? Remember, parentheses are not passive grouping marks. Each layer of parentheses means "execute this as a function".

Second - general advice to develop code bottom-up and troubleshoot it inside-out. Try testing the innermost forms at a REPL, make sure they work, and move to the next outer form.

No there's no extra layer of parenthesis. The operation I want to execute is complicated...
I have to look for an id in my list and pull out the name (first element of the list inside a vector). As I said, the bloc of code inside the function "getnameonly" works outside. However, for some reason, inside the function, it returns the error I've shown above.
I really need it to be in a function because I'll be working with this logic all along my assignment... I checked everywhere and for this error there are many interpretations. So far, I didn't find the solution to mine.
When I execute this line :
(println (nth ((nth (filter #(= 1 (first %)) customers) 0) 1) 0))

It gives me :
John Smith
+1 vote

Interestingly, what you've got there is not a error it at, it's an object (!) like it says. (:

I might write it like this, given the shape you've provided:

user> (def customers '([1 ("John Smith" "123 Here Street" "456-4567")]
                       [2 ("Sue Jones" "43 Rose Court Street" "345-7867")]
                       [3 ("Fan Yuhong" "165 Happy Lane""345-4533")]))
user> customers
([1 ("John Smith" "123 Here Street" "456-4567")]
 [2 ("Sue Jones" "43 Rose Court Street" "345-7867")]
 [3 ("Fan Yuhong" "165 Happy Lane" "345-4533")])
user> (defn id->name
        [customers id]
        (->> (filter #(= id (first %)) customers)
user> (id->name customers 3)
"Fan Yuhong"
user> (id->name customers 2)
"Sue Jones"
user> (id->name customers 1)
"John Smith"

Best of luck with your assignment!