Build a vector from single items in Clojure -
i have numbers: 1 2 3 4
, want build vector (or kind of seq) them, want [1 2 3 4]
.
as in (= (__ 1 8 3 4) 8)
, without using max
. here have far :
(= (reduce #(if (< %1 %2) %2 %1) 1 8 3 4) 8)
but arityexception
, , therefore want build vector
of [1 8 3 4]
.
how can convert single numbers items of collection ?
use vector
:
;; create vector long way user=> (vector 1 2 3 4)
note if talking hardcoded values, advantage of having code data can represent them in code :
;; code, equivalent code above [1 2 3 4]
if talking generating sequence of numbers, have @ range
:
user=> (range 1 5) (1 2 3 4)
vec
take collection , turn vector :
user=> (vec (range 1 5)) [1 2 3 4]
taking account comment :
user=> (reduce #(if (< %1 %2) %2 %1) (vector 1 8 3 4)) 8 user=> (= (reduce #(if (< %1 %2) %2 %1) (vector 1 8 3 4)) 8) true
but surely know, use max
:
user=> (max 1 2 3 4) 4 user=> (apply max [1 2 3 4]) 4
if wish not use max
, concise way of getting max : user=> (last (sort [1 2 3 4])) 4
or anonymous function, using threading macro :
user=> #(-> % sort last) ;; % stand arguments, sequence 4
Comments
Post a Comment