Fork me on GitHub
#beginners
<
2016-04-24
>
bwstearns06:04:06

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.

bwstearns06:04:27

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)))))

bwstearns06:04:01

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

https://gist.github.com/argentum47/5c5528501117e4df1f6425fdad9250f2 why does it keep saying Exception in thread "main" java.lang.ClassNotFoundException: compojure.core, compiling:(facebook_feed/core.clj:4:2)

sveri08:04:32

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

ikouchiha08:04:03

oh .. and :use ?

ikouchiha08:04:26

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

sveri08:04:10

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

dharrigan12:04:37

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...

sveri13:04:08

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

sveri13:04:37

Maybe you can narrow down your question a bit?

dharrigan13:04:22

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.

dharrigan13:04:47

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

dharrigan13:04:20

I'm undersure of the best approach wrt clojure.

dharrigan13:04:27

(In java, no problemo :-))

sveri13:04:12

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: http://clojure.org/reference/atoms. 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.

dharrigan13:04:24

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)

dharrigan13:04:29

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

dharrigan13:04:18

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.

sveri13:04:33

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.

sveri13:04:44

So you see the differences yourself.

dharrigan13:04:59

nice advice simple_smile

sveri13:04:45

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: https://github.com/papers-we-love/papers-we-love/blob/master/design/out-of-the-tar-pit.pdf

dharrigan13:04:55

awesome, will read.

sveri14:04:35

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 😄

dharrigan14:04:55

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

sveri14:04:02

Good luck simple_smile

bwstearns17:04:44

@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.

sveri17:04:47

@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 😄

bwstearns17:04:56

@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.

bojan.matic17:04:26

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

bojan.matic17:04:59

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

bwstearns17:04:54

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

bwstearns17:04:11

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

bwstearns17:04:45

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

bwstearns17:04:39

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.

nathansmutz22:04:34

{: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?

surreal.analysis23:04:31

@nathansmutz: In general, I find just setting up Prelude (described here: https://github.com/bbatsov/prelude) gets a clean install to a functioning Clojure dev environment in as much time as it takes to download