--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--You received this message because you are subscribed to the GoogleGroups "Clojure" group.To post to this group, send email to clo...@googlegroups.comNote that posts from new members are moderated - please be patient with your first post.To unsubscribe from this group, send email to
For more options, visit this group at---You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
http://clojure.org/guides/faq#seqs_vs_colls
http://insideclojure.org/2015/01/02/sequences/
http://insideclojure.org/2016/03/16/collections/
Alex
(ns clj.core(:require [tupelo.core :as t] ))(t/refer-tupelo)
(def v [1 2 3])(conj v 4) => [1 2 3 4](conj (map identity v) 4) => (4 1 2 3)(conj (remove (constantly false) v) 4) => (4 1 2 3)(conj (filter identity v) 4) => (4 1 2 3)
(t/append v 4) => [1 2 3 4](t/append (map identity v) 4) => [1 2 3 4](t/append (remove (constantly false) v) 4) => [1 2 3 4](t/append (filter identity v) 4) => [1 2 3 4]
Clojure has the cons
, conj
, and concat
functions, but it is not obvious how they should be used to add a new value to the beginning of a vector or list:
; Add to the end > (concat [1 2] 3) ;=> IllegalArgumentException > (cons [1 2] 3) ;=> IllegalArgumentException > (conj [1 2] 3) ;=> [1 2 3] > (conj [1 2] 3 4) ;=> [1 2 3 4] > (conj '(1 2) 3) ;=> (3 1 2) ; oops > (conj '(1 2) 3 4) ;=> (4 3 1 2) ; oops ; Add to the beginning > (conj 1 [2 3] ) ;=> ClassCastException > (concat 1 [2 3] ) ;=> IllegalArgumentException > (cons 1 [2 3] ) ;=> (1 2 3) > (cons 1 2 [3 4] ) ;=> ArityException > (cons 1 '(2 3) ) ;=> (1 2 3) > (cons 1 2 '(3 4) ) ;=> ArityException
These failures are irritating and unproductive, and the error messages don’t make it obvious what went wrong. Instead, use the simple prepend
and append
functions to add new elements to the beginning or end of a sequence, respectively:
(append [1 2] 3 ) ;=> [1 2 3 ] (append [1 2] 3 4) ;=> [1 2 3 4] (prepend 3 [2 1]) ;=> [ 3 2 1] (prepend 4 3 [2 1]) ;=> [4 3 2 1]
Both prepend
and append
always return a vector result.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.
Hi Colin,I too have been bitten by this type of inconsistency in clojure.core functions.
The root of the problem is that conj has different behavior for lists and vectors, and that a seq behaves like a list. When map, filter, etc convert the source vector into a seq, the behavior of conj changes accordingly.
In order to avoid this kind of unpredictability,
you may wish to explore some of the functions to the Tupelo library. The goal is to make things simpler, more obvious & predictable, and as bulletproof as possible. One example is the append function. Here is a sample program comparing conj and append:(ns clj.core(:require [tupelo.core :as t] ))(t/refer-tupelo)(def v [1 2 3])(conj v 4) => [1 2 3 4](conj (map identity v) 4) => (4 1 2 3)(conj (remove (constantly false) v) 4) => (4 1 2 3)(conj (filter identity v) 4) => (4 1 2 3)
(t/append v 4) => [1 2 3 4](t/append (map identity v) 4) => [1 2 3 4](t/append (remove (constantly false) v) 4) => [1 2 3 4](t/append (filter identity v) 4) => [1 2 3 4]
I think simpler and more bulletproof functions can go a long toward making Clojure easier to use, especially for beginners or when you are uncertain about the exact type of a parameter.
...
You can use `into` to "pour" the sequence into the collection of your choice. If you're using `into`, then most of these sequence functions support transducers to avoid allocation of intermediate sequences, providing a speed boost.