Fork me on GitHub

so I have a vector of vectors like [:a [:b 1] [:c 2] [[:d 3] [:e 4]] [:f 5]]. Anyone know an idiom to flatten just the vector containing the :d and :e vectors so that the end result is like [:a [:b 1] [:c 2] [:d 3] [:e 4] [:f 5]]? The specific situation is that it's a hiccup tree structure and I want to promote the :d and :etags.


So. It's not idiomatic or good in any way shape or form, but it works

(def poorly-nested [:a [:b 1] [:c 2] [[:d 3] [:e 4]] [:f 5]])
(into [:a] (map vec (partition 2 (flatten (drop 1 poorly-nested)))))


Ah. I was going about it all wrong. Got where I wanted to end up another processing step further by doing (apply hash-map (flatten args)) where args would be (rest poorly nested)

ikouchiha08:04:56 why does it keep saying Exception in thread "main" java.lang.ClassNotFoundException: compojure.core, compiling:(facebook_feed/core.clj:4:2)


@argentum47: The :require expression belongs into the ns expression.


oh .. and :use ?


what is the difference between :use and use ' if any


Use imports the whole Namespace. You should not be using that. Move that require Statement inside the ns and it should work. Also inside require do not use :all instead use :as


Say I have a concept of a "Basket" that should contain items. In Java, I would have a Basket class with a list of Items that are added to it (so the Items is internal to the Basket). In Clojure, what would be the approach to take, since I have to keep hold of the items that are added somehow...


@dharrigan: Everything you want, a map, a vec a list


Maybe you can narrow down your question a bit?


Yes, I think I wasn't clear enough. Basically, I'm looking to understand how to hold on to 'state'. Since a basket changes over time (i.e., there are items added to it), then I have to maintain that list of 'items' somewhere. So, when I "add" a product to a Basket, then this product gets added as an item within the basket. So, when I'm finished calling the "add" function, either the Basket maintains this list of items internally, or, I believe, I could pass back a new list (conjoined with the item that I've just added), so I'm continuously passing back an ever growing list of items when I invoke add of a product to a basket.


Or, I could have the basket maintain that list internally, and have the function simply return back nothing (i.e., == successfully added)


I'm undersure of the best approach wrt clojure.


(In java, no problemo :-))


It aint a problem in clojure either, even better, in clojure there is concurrency built in. So you got two options: 1. Move the state into the database (or any other third level storage) and pass down a vec through your functions. This is what I try usually first, most of my applications dont hold any state in memory. 2. Using one of clojures reference type. For instance one of these three: Ref, Agent, Atom: Where I think you might want to look at atom first as it is the easiest to understand and mostly the tool to use when holding state in an applications memory. Atoms can exactly do what you want.


okaydokey, will have a look. For what it's worth - this is jut a mental excercise in Clojure - no databases or external sources. Simply a REPL with my editor and unit tests (expecations)


I'm doing my little challenge using the constructs of the language itself, e.g., functions, defrecords, lists and maps.


I guess what I was trying to understand is whether holding the state within a "Basket" namespace is okay, or trying to be functional, to just pass back the list each time from the function call.


From a theoretic point of view, try to go functional as far as you can and avoid state if you can. But only up to a certain point. Of course there are usecases where it makes sense to keep state inside your application. In the end you have to decide for yourself. It's always hard to judge from the outside. Apart from that, if you just want to learn you could use an atom first and then, if everything works, refactor your application to get rid of that atom.


So you see the differences yourself.


nice advice simple_smile


There is always value in avoiding global state and there is also a lot of literature to that. One of my favourite papers about that topic is:


awesome, will read.


One more warning though, you will get spoiled by clojure and if you keep on learning it there will be a time where you find it hard to code java everyday, so beware the devil 😄


It will be a while coming - slowly slowly, baby steps...


Good luck simple_smile


@sveri: clojure spoiling is real haha. After about a month or so of clojure, debugging issues stemming from wanton use of state makes you significantly sadder than it used to.


@bwstearns: Agreed. It took me over a year to settle with the fact that in my location I will not code clojure for money any time soon. I mean, I have an awesome job, but, its java 😄


@sveri: Perl here 😄 I like my job otherwise though. When it does come time to move on though definitely looking for a functional place for sanity and learning purposes. On the plus side Clojure/ML-type languages do seem to be making headway in the paid-to-program world.


i come from a python/javascript background…am I in the minority here? 😄


@bwstearns: i think clojure and ML-type languages are not very much alike


I would agree that they're not very similar, but they're certainly more similar than to each other than they are to C/Java or Python/JS/Ruby


actually [[[CL Clojure] [Haskell/MLs]] [[Python Ruby JS] [C Java]]] kind of fits together nicely as a rough taxonomy


@bojan.matic: I come from a python background as well


I'm trying to get a handle on the MLs at the moment and while they fundamentally appeal to me (I now end up sketching out "types" of functions/methods in weakly typed languages to help think about them), I haven't had that moment where it clicked and I became productive in any of them yet.


{:fantasy "I'm going to learn some Emacs today" :reality "I'm going to setup Emacs today."} Any suggestions for a non-stale Clojure Emacs setup guide?


@nathansmutz: In general, I find just setting up Prelude (described here: gets a clean install to a functioning Clojure dev environment in as much time as it takes to download