Fork me on GitHub

Hello everyone


I'm having probably an easy problem to solve for someone with more experience


I'm running an app with my own server and can't access the figwheel repl


Since there must be an equal number of args in recur and bindings in loop, is it unavoidable that a loop binding that takes its initial value from a previously given value must be written this way? (I’m referring to the x x) Not the first time I’m dealing with this, but it has come up many times already and I started to wonder if this is really the Right Way to write it. (I’ve had cases with multiple such values in more complicated algorithms as well)


there's nothing wrong with this pattern


That’s a relief, then. Thanks!


How can i call function of project.clj in controller file which is in controller/controller.clj?


(controller.controller/function ...) ?


wanna to call from main file


The project.clj file is intended to be used by the leiningen build tool, and isn’t really part of your project/app/etc. You might be able to get your app to read it and use a function from it, but it would be an unusual thing to do. What is it that you are trying to do, that makes you want to call a function from project.clj?


@rondon.sarnik if you still need help PM me.


@manutter51 i have written general function for creating cookie in project.clj file and wanna to use it in controller. I am very new to clojure so do not know this approach is right or not?


Ok, I would recommend just copy that function into a different file in your project. The project.clj file is for things like pulling in dependencies, starting a REPL, building a jar file, and so on. It’s not for code that you actually run as part of your application.


@shraddha.paktolus not sure how familiar with other languages you are but you can treat the project.clj file similar to package.json, Makefile or gemfile in other languages if that makes sense. There's almost no reason to have a function defined in there that the rest of the project needs


its a file about your project. It defines the name, the dependencies, lists the author, and tasks like how to build, release, development environment, etc. It shouldn't have code of your project but about your project


sorry it is main.clj where i have written routes,handler, middlewares which is located at src/projectName/main.clj. And all dependencies are written in deps.edn ...


Ah, ok, that’s good then


So your question is more like, “How do I call a function from when I’m in, correct?


yes, but is in main dir and in another folder


Ok, good then, let me write up a quick example




(defn make-cookie [stuff]
  ;; make a cookie somehow
And now we’ll call make-cookie from
  (:require [ :as foo])) ;; <== hook this namespace up to

(defn make-headers [stuff]
  {:cookie (foo/make-cookie stuff)
  ... })


The :require clause lets us define foo as a short alias for, so we can call make-cookie by typing (foo/make-cookie ...). Technically, we could also write out ( ...), which is perfectly valid, but the alias is easier.


Luminus is good for web dev?


I’ve used it and would use it again. It’s real quick to get a server set up, but you may find some rabbit holes if you decide to make changes to the default config. It uses a bunch of libraries that you’ll need to learn in order to do get things going (but then you’d probably have to do that anyway).


it gave me exception Cyclic load dependency: [ /my-project/foo ]->/my-project/controller/bar->[ /my-project/foo ]


Ok, this happens sometimes. You can’t have A require B if B requires A. The way to fix that is to make a new namespace C, and then have A require C and B require C. In your specific case, I’d recommend making a new namespace like my-project.cookie-utilities, and put your make-cookie function in there. Then in main or wherever else, do

(ns my-project.whatever
  (:require [my-project.cookie-utilities :as cookie]))
You can require the new namespace in as may other namespaces as you want.


When I write code, I usually end up with very little code in my main or core or whatever. Most of the actual work is done in “child” namespaces, and main just requires them, and calls one or two functions from each.


@manutter51 thanks, will add new namespace


another possibility is to simply merge the two namespaces. of course, it's not always a reasonable option, but one worth thinking about


What is the equivalent of from app1 import views as views1 in Clojure, the as part specifically?


I just want to import a single function renamed


Cool, thanks


Hello, i’m new to concurrency. I’ve been using pmap for parallel tasks but it seems to take alot of time to finish this pmap process even though individually, tasks complete quickly. (system/exit 0) closes everything, but wondering if there is an alternative.


if you use anything that uses the clojure agent thread pools (including pmap) you should use (shutdown-agents) on exit to tell clojure to shut down the pools, that's why you get the process hanging


Oh, thank you. Will look into that.


That was very helpful, thank you.


also for a more nuanced alternative to pmap, there's the claypoole library which lets you use and configure a custom thread pool


That customization option looks really good, i’ll try it out.

hipster coder22:04:37

@noisesmith writing parallel capable code has been on my mind. I discovered Amdahl’s laws that says a system can speed up to 20 times if 95% of the code can be parallelized.

hipster coder22:04:18

I find this is extremely interesting because Elixir/Beam maxes out around 15 cores. And I am trying to figure out if this has anything to do with the theoretical 20 times limit according to the law.


where are you getting the 15 cores number?

hipster coder22:04:37

from the JVM architects in a talk. They talked about Azul and Erlang

hipster coder22:04:34

Azul is running 1000 core machines. But Erlang caps at around 15 cpu’s. I guess the cross talk is related to how the cpu’s retrieve memory. But Erlang actor model also copies memory for every single actor. It does not share memory, it all.

hipster coder22:04:10

This is also related to the concept of coherent memory, how fast all of the cpu’s can talk to memory… some architecture has faster local memory and can’t access all the memory at the same speeds…

hipster coder22:04:15

I knew my intuition was correct… that there was some tradeoff with the actor model… finally I verified it by listening to the JVM architects


The rule of thumb I've heard is that the BEAM soft-caps at 15 nodes due to the amount of cross talk

hipster coder23:04:35

So Elixir scales horizontally really well. But I’d have trouble trying to run huge memory computing tasks on top of it?

hipster coder23:04:24

Sound like I definately need to run other computational work across a message bus if I used Elixir. But I am leaning towards using the JVM with Clojure, Python/Jython

hipster coder23:04:06

I might need the ability to load 20 GB files into memory and run machine learning on them

hipster coder23:04:21

ahh, cross talk… the path from the cpu to the RAM has a cross talk problem? are do you mean the CPU’s only?


The BEAM model is optimised for concurrent message passing, not necessarily parallel computation and definitely not number crunching. Is machine learning message passing (I have no idea how it works)?

hipster coder14:04:44

Machine learning can use both highly parallel workloads (cuda on a gpu for example) or distributed computing where the workload is spread across servers. However, Clojure STM actually copies a lot of memory just like the actor model. But Clojure has advantage of being able to share memory for number crunching unlike Erlang which doesn’t even allow shared memory.


so I found this exercise: and thought it would be fun to try in clojure. I came up with this:

(defn cartesian [[x y]]
   (for [x x
         y y]
     [x y])) 
It gives me the answer to that one specific example but what am I missing here. I'm still confused on how to utilize lazy, infinite constructs to solve that part of the exercise. I can add z and so on to add to the parameters but can't figure out how to make it & args kind of generic, if that makes sense. How would you approach that problem? Is mine actually answering the problem? How would you test to make sure you solved it?


the rust answers are hard core!


doing so well is actually very tricky, the simple answer ends up building up a stack of lazy seqs which will cause problems if you have a lot of lists


but, really, getting a cross product of an infinite number of seqs is pretty useless


it will just block and run out of memory as soon as you call first on it


the big difference with haskell (not sure about this entirely) is the list that is the product is also computed lazily


so for example, in clojure [x y] is not lazy


there is likely some really torturous construction that would yield a lazy-seq of lazy-seqs, which would be the closest possible to the haskell


ok, this is interesting stuff! that clojure code you posted is considered pretty advanced, right? That's going to take me a while to parse


a long while. haha

hipster coder22:04:58

@dpsutton I responded in a thread. The JVM architects said Erlang caps at around 15 cores based on how the memory is used. And from real world scaling tests. I knew there was probably a vertical scale trade off by using the actor model.

Alex Miller (Clojure team)22:04:18

math.combinatorics has a generic cartesian product impl

Alex Miller (Clojure team)22:04:21

lots of very nice code in that lib for stuff like this


general in the real world sense, for games with infinite seqs, it won't work, it both calls every? and vec


(not that there is anything wrong with that, just not strictly equivalent to what the linked reddit post claims the haskell code will do, which I don't even know if it will)

Alex Miller (Clojure team)23:04:27

those are both on the seq of seqs, not on the seqs themselves


that library looks solid! Is this the specific code that probably pertains to this exercise:

(defn cartesian-product
  "All the ways to take one item from each sequence"
  [& seqs]
  (let [v-original-seqs (vec seqs)
        (fn step [v-seqs]
          (let [increment
                (fn [v-seqs]
                  (loop [i (dec (count v-seqs)), v-seqs v-seqs]
                    (if (= i -1) nil
                      (if-let [rst (next (v-seqs i))]
                        (assoc v-seqs i rst)
                        (recur (dec i) (assoc v-seqs i (v-original-seqs i)))))))]
            (when v-seqs
              (cons (map first v-seqs)
                    (lazy-seq (step (increment v-seqs)))))))]
    (when (every? seq seqs)
      (lazy-seq (step v-original-seqs))) 

Alex Miller (Clojure team)23:04:36

yes, that's what I linked above :)


right, and my understanding of the claim in the post is that the list of inputs can be infinite, but I may be misunderstanding that

Alex Miller (Clojure team)23:04:03

well, that's a stupid thing to want :)


lol, oops! I didn't see your second link. And I was proud to just find it!

Alex Miller (Clojure team)23:04:29

I reject that requirement :)