Monday, March 8, 2010

Tried some functions that operate on collections

To get to the "user=>" prompt, use the following from the DOS prompt (DOS is dead. Long live DOS!):
java -cp clojure.jar clojure.lang.Repl

; does the collection(vector in this case) contain index 4?
user=> (contains? [1 2 3 4] 4)
false

; does the vector contain index 0?
user=> (contains? [1 2 3 4] 0)
true

; could not get it to work for lists. apparently works only for numerically indexed collections like vectors.
; doesn't throw an exception either
user=> (contains? (list "3e" "2 tired" "1 more") "3e" )
false

; create a vector
user=> (vector "a" "c" 3)
["a" "c" 3]

; number of items in collection (list)
user=> (count (list 1 2 34 "er") )
4

; number of items in collection (vector)
user=> (count (vector 1 2 34 "er") )
4

; counts number of items in collection
user=> (count (list 1 2 3 4 "er" 34) )
6
user=> (count (list 23 "3er" "oel" 5) )
4

; return unique items in a list collection
user=> (set (list 1 2 3 5 6 3 2))
#{1 2 3 5 6}

; return unique items in a vector collection
user=> (set [1 2 3 5 6 3 2])
#{1 2 3 5 6}

; same a previous one. return unique items in a vector collection
user=> (set (vector 1 2 3 5 6 3 2))
#{1 2 3 5 6}

; cast a number to short
user=> (short 8383747474747448)
28728

; sort a collection
user=> (sort (vector "how" "are" "you" "?") )
("?" "are" "how" "you")

No comments:

Post a Comment