Fork me on GitHub

I am going through some algorithm problems and I am curious, if one needed to, how would you execute a nested for loop (yes, this is ugly, but I am curious) - or maybe there is a way to get the same effect in the functional paradigm but not do it? You will start with a vector n items and at the end, end up with another vector of considerably less items...


for is not a loop, it is a list comprehension, and it can do nested iteration and filtering


you can also do nested loops if that's what you mean


Sorry, for loop mean an imperative for loop - like in JS or Python


(map (fn [thing] (map ...) ...) would also be a nested loop idea as well


@tkjone the first question is whether the important thing is the data output, or some side effect done while consuming the input?


The important thing is the output


OK - for can do nested iteration with filtering easily


Clojure 1.9.0-RC1
+user=> (pprint (for [x (map range (range 10)) y x :when (zero? (mod y 3))] [x y]))
([(0) 0]
 [(0 1) 0]
 [(0 1 2) 0]
 [(0 1 2 3) 0]
 [(0 1 2 3) 3]
 [(0 1 2 3 4) 0]
 [(0 1 2 3 4) 3]
 [(0 1 2 3 4 5) 0]
 [(0 1 2 3 4 5) 3]
 [(0 1 2 3 4 5 6) 0]
 [(0 1 2 3 4 5 6) 3]
 [(0 1 2 3 4 5 6) 6]
 [(0 1 2 3 4 5 6 7) 0]
 [(0 1 2 3 4 5 6 7) 3]
 [(0 1 2 3 4 5 6 7) 6]
 [(0 1 2 3 4 5 6 7 8) 0]
 [(0 1 2 3 4 5 6 7 8) 3]
 [(0 1 2 3 4 5 6 7 8) 6])


for reference

+user=> (pprint (map range (range 10)))
 (0 1)
 (0 1 2)
 (0 1 2 3)
 (0 1 2 3 4)
 (0 1 2 3 4 5)
 (0 1 2 3 4 5 6)
 (0 1 2 3 4 5 6 7)
 (0 1 2 3 4 5 6 7 8))


Neat. My hang up was that for some reason I thought I would have to stick with maps and reduces and using for would not be savvy


The thing I am doing is taking a vector [1 2 3] and I need to compare each item against every other item in the vector


(for [x [1 2 3] y [1 2 3] :when (some-fn x y)] ...) - for example


there's also a :let key


+user=> (let [v [1 2 3]] (for [x v y v :let [same? (= x y)]] same?))
(true false false false true false false false true)


Thanks, I will play with those - def' given me better understanding of how they can be used


you can also use maps like that as well:

(let [coll [1 2 3]]
  (map (fn [v] (map #(compare v %) coll)) coll))
((0 -1 -1) (1 0 -1) (1 1 0))


Do you by any chance get the followin error when running the code @noisesmith posted? ClassCastException clojure.lang.PersistentList$EmptyList cannot be cast to clojure.lang.IFn user/eval1274 (form-init5831160276747267566.clj:1)


I pasted that directly from a clojure.jar repl


ok. I'm using clojure "1.8.0", so I was wondering whether that could be the reason...


Nevermind, I figure out what I was doing wrong 😛


@noisesmith Thanks so much for the info on types!


@noisesmith I got a deftype working that implements IPersistentCollection and Associative. Very cool stuff. I am having a more difficult time figuring out how one would control what is considered the "items" when map-ing a function down a user defined type. For example, if we have a deftype with an bunch of fields, one of which is called something like :data or :table and we would like (map f thing-of-our-type) to be the same as if we did (assoc thing-of-our-type :data (map f (:data thing-of-our-type))) what interface would we have to implement to achieve this? java.lang.Iterable maybe?


map has existing and well-defined behaviour. i would imagine that different behaviour would (and should) require a different function, like with a protocol. i do not much about this kind of thing though.


@sundarj I suppose that makes sense. Thanks!


@erp12 Clojure.lang.Seqable is the interface you want.


Hi guys is there a way to specify which lein version should be use for specific project only? For some reason tests fail when using 2.8 we suspect it's a bug but we need to investigate more. So we wanted to use 2.7 specifically only for that project is there a way to do that?


I guess it depends how you run your tests. You can install multiple leiningen versions and use the 2.7 version for the specific project If you use brew on MacOS you can access the old versions e.g.

I'd say it's mostly packaging system question


i've got a really general question which probably won't have a definitive answer: We (2 man team starting own web development company) have gotten a request to implement to supply a system that handles paid subscriptions for a fitnes site (customer information, payment and content reminders, intergration with 3ed party payment provider). Being impressed with clojure my hope is that clojure (i.e. with yada/bidi and datomic) would be a good fit for it. in short: would it be wise? or am i deluding myself because i happen to like the language. (I'm experienced in php and python, but still a beginner in clojure)


IMHO, pick familiar tools, and focus on the customer's needs / problems. Alternatively, get the customer's sign off on the idea of using a core tool that's new to you. Best wishes for a successful project :spock-hand:


I struggled with this some time ago; not with Clojure, but with Elixir. After much consideration, I decided to build only a small (and easily replaceable) part of the system in Elixir. I picked a component that was self-contained, had very few dependencies, and didn’t have to save data.


Especially for persistence, I say go with what you know.


In your particular case, perhaps I would try to build the reminder system in Clojure.


I think you're both right that it would be smarter to use languages that i'm more comfortable in. @U7PBP4UVA how did your self-contained elixer part work out? Are you happy with it?


Extremely happy! Keep in mind, it played into the strengths of Elixir (communicating in parallel with ~200 machines on a private network), and out of the weaknesses of Python.


I still had a few “oh-shit” moments that I had to resolve even for a relatively simple component. Which is why I say “pick something simple”, no matter the language, you will have “unknown unknowns”, best to keep those confined to a single part of the system than let them permeate your entire stack.


I'm glad the part worked out for you, and regarding the unkown unkowns, that sounds like good advice. I'll try and find a small, non-critical part of the system where i can use clojure


and with communication between 200 machines it sounds like it was a cool project to work on


Thanks to both of you for the advice


The Self-Contained Systems pattern might be of interest as you strive to mix languages:


But it might also add undue complexity as many people like to start out with a monolith and then break things up.


Most welcome, @U3675LFHP Here are a couple of more thoughts 1. Use Clojure to write things like functional test suites (UI automation etc.) --- lower the risk of tool-originating failure, for you and for the customer, and also create a QA resource for yourself that you could carry from project to project. 2. Once you've understood the customer's problem and solved it satisfactorily, perhaps see if you can rewrite a small part of it in Clojure. (And again, best wishes! As a fellow freelance/independent engineer, I know how it is to take on new/greenfield projects.)


Personally I would not commit to delivering a production system built in a language I was new to, not if money was involved. You’ll want at least one experienced Clojure dev on board, or one good production-ready not-for-money system built, IMO.


@manutter51 thanks for your reply, i think you're right. unfortunately i don't think we can spare the funds to hire a experienced clojure dev at the moment, so we'll probably confine clojure to a small, non-critical part of the system as orestis suggested.

Leo E16:11:33

So if I want to use clojure-script in my leiningen project should I use this or should I use the jar from their website?


Does anyone have any tips on figuring out what interfaces need to be implemented to get certain functionalities in our deftypes? So far you all have been very helpful, but I would hate to continuously spam here until I commit this stuff to memory 🙂 For example, I have a deftype that implements clojure.lang.Associative but it fails on get. Are there any docs that can be used to figure out which interface will allow support for get?


I’m getting a weird issue

(defn my-fn [f] (var f))

CompilerException java.lang.RuntimeException: Unable to resolve var: f in this context, compiling:(/private/var/folders/bd/np6270sn31j1d0z3zhl82tjh0000gn/T/form-init7905493545882869081.clj:1:13) 
why (var f) is being evaluated before I call the function


it's a special form, evaluated at read time


never mind, this is wrong


@oliv f must be a var (i.e. defined with def). You are attempting to capture a local binding "f" which is just a symbol. What's the goal anyway?


I’m just playing around to understand the difference of #’full-name against passing only the symbol in the example below

(defn full-name [] "Oliv")
(defn greetings [honorific f] 
	(fn [] 
		(str honorific " " (f))))

(def mr-name (greetings "Mr." full-name))
(def sr-name (greetings "Sr." #'full-name))


(defn full-name [] "Olivia")



sr-name , is using the var instead of the symbol full-name . once I change the full-name function, sr-name changed as well


full-name is a var, so essentially a name-value pair. Your defs of mr-name and sr-name are sending to the function greetings the var full-name, not the value of the var at that point. The var is local-bound to "f" (just an alias) and de-reffed at the point where (f) is invoked.


Some confusion is generated when you type full-name at the REPL, which calls (eval full-name) and eventually ends up in de-reffing the var for you. That's why you have (var full-name) to actually access the var. But when is not at the REPL, full-name or #'full-name are resolving to the same thing.


hey @reborg I just ran: example.clj

(defn full-name [] "Oliv")
(defn greetings [honorific f] 
	(fn [] 
		(str honorific " " (f))))

(def mr-name (greetings "Mr." full-name))
(def sr-name (greetings "Sr." #'full-name))

(println (mr-name))
(println (sr-name))

(defn full-name [] "Olivia")

(println (mr-name))
(println (sr-name))
lein exec example.clj


the output was:

Mr. Oliv
Sr. Oliv
Mr. Oliv
Sr. Olivia


the sr-name was changed


Dove into the for loop yesterday which is awesome for the problem I was solving. I want to optimize it a little more though, for example, if I have a vector like [ [1 3 ] [ 1 5] [2 4 ] ] and I do something like

(for [x sorted-v 
        y sorted-v]
The first "iteration" (not sure what its called actually) would allow me to compare 1 3 and 1 3 second iteration would be 1 3 and 1 5 etc. Cool, but when I get to the end, and everything increments, the iteration would be 1 5 and 1 3 . thing is, I have already compared these two, in a different order, but the comparison for my purposes is done. Is there a way to force an increment, or keep local mutable state with these loops? My thought was create a set of already compared comhinations


@tkjone I think a loop would be better for that situation but it's hard to tell without more detail about what you're trying to do.


for produces a lazy sequence for consumption -- it's not like a for loop in other languages. It isn't really a "loop" at all.


I would like to compare each item against every item in the vector


And do what with the result of that comparison? That's the key part.


Return a collection of vector pairs that match a condition


I still think loop with an accumulator will be better.


Okay, I will checkout the loop


what about something like

(for [x sorted-v 
      y sorted-v
      :when (matches-condition? x y)]
  [x y])


Thats actually similar to what I have now, which works, but I wanted to avoid further comparing vector items that have already been compared


(for [[i x] (map-indexed vector sorted-v) 
      y (subvec sorted-v i)
      :when (matches-condition? x y)]
  [x y])


not sure if that’s better than just using loop


OK, here's a solution using for that might satisfy you @tkjone

(for [[x :as xs] (take (count sorted-v) (iterate next sorted-v))
      y xs
      :when (matches-condition? (x y)]
  [x y])


This is actually a really interesting approach. Question about iterate - it does not apply the function on the first iteration, correct?



(take 10 (iterate inc 0))
=> (0 1 2 3 4 5 6 7 8 9)


(I expect there's a cleaner way of getting that iterated sequence)


Thank you. I will take a look at both solutions, but to be clear, you would say a loop might be the better choice?


@tkjone here's something with reduce (similar to loop):

(->> [[1 3] [1 5] [2 4] [10 8]]
      (fn [[seen result :as acc] [x y :as val]]
        (if (some #{val} seen)
          [(conj seen val) (conj result (< x y))]))
      [[] []])
     ((fn [[_ result]] result)))
[true true true false]


Well, you are not really iterating over the elements of the sequence, you are performing filtering on the sequence, the rest of the sequence, the rest of the rest of the sequence etc.