Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why did the designer make vector, map, and set functions in clojure?

Rich made vector, map, and set functions, while list, and sequence are not functions. Why cannot all these collections be function to make it consistent?

Further, why don't we make all these compose data as a function which maps position to it's internal data?

If we make all these compose data as function then there will be only function and atom data in clojure. This will minimize the fundamental elements in that language right?

I believe a minimal, best only 2, set of fundamental elements would make the language simpler, more expressive and more flexible. Is this correct?

like image 246
Shark Avatar asked Apr 08 '16 23:04

Shark


2 Answers

Vectors, maps, and sets are all associative data structures. Maps are the most obvious; they simply associate arbitrary keys with arbitrary values. A vector can be thought of as a map whose key set must be the set of all nonnegative integers less than the vector's size. Finally, sets can be thought of as maps that map keys to themselves.

It's important to understand that the sequential nature of a vector and the associative nature of a vector are two orthogonal things. It's a data structure that's designed to be good at supporting both abstractions (to some extent; for instance, you can't efficiently insert at the beginning of a vector).

Lists are simpler than vectors; they are finite sequential data structures, nothing more. A list can't efficiently return the element at a particular index, so it doesn't expose that functionality as part of its core interface. Of course, you can get an element of a list by index using nth, but in that case, you're explicitly treating it as a sequence, not as an associative structure.

So to answer your question, the IFn implementations for vectors, maps, and sets are there because of the extremely close relationship between the idea of an associative data structure and the idea of a pure function. Lists and other sequences are not inherently associative, so for consistency, they do not implement IFn.

like image 99
Sam Estep Avatar answered Sep 28 '22 02:09

Sam Estep


Elogent's answer is excellent. There is one more reason that it wouldn't make sense for lists to be functions:

Literal lists already have a different, very important role, so they can't also be treated as functions in the way that vectors are.

Let's start with a vector containing two functions, partial and +, and a number, 5. We can treat the vector as a function, as you know, to return the value indexed by its argument:

user=> ([partial + 5] 2)
5

So far, so good. Suppose we want to use a list (partial + 5) in place of the vector, as you suggested, to return the value 5. Will we get an error message? No! But we won't get 5 as the result, either:

user=> ((partial + 5) 2)
7

What happened? (partial + 5) returned a function--the function that adds 5 to its single argument--and then this function was applied to the argument 2.

When a list is evaluated, its first element is evaluated, and should return a function. If the first element is a symbol, it's evaluated, and then the function that's its value is applied to the arguments, which are the other elements of the list. If the first argument of a list is itself a list, then it is evaluated in the same way that it would be evaluated if it were at the top level. The entire expression in that inner list should return a function, which will then be applied to the other elements of the outer list.

Since an inner list that's the first element of list that's being evaluated already has this role, it can't also play the kind of role that vectors that are first elements play.

like image 21
Mars Avatar answered Sep 28 '22 04:09

Mars