Fork me on GitHub
#beginners
<
2019-04-16
>
yunior01:04:32

Hello everyone

yunior01:04:10

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

yunior01:04:56

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

tabidots07:04:01

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)

leonoel08:04:28

there's nothing wrong with this pattern

tabidots08:04:09

That’s a relief, then. Thanks!

clojurites_1911:04:43

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

thumbnail12:04:01

(controller.controller/function ...) ?

clojurites_1912:04:36

wanna to call from main file

manutter5112:04:06

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?

schmidt7312:04:23

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

clojurites_1912:04:25

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

manutter5112:04:12

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.

dpsutton12:04:30

@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

dpsutton12:04:39

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

clojurites_1912:04:18

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

manutter5112:04:32

Ah, ok, that’s good then

manutter5112:04:20

So your question is more like, “How do I call a function from my-project.foo when I’m in my-project.bar, correct?

clojurites_1912:04:38

yes, but my-project.foo is in main dir and my-project.bar in another folder

manutter5112:04:03

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

manutter5112:04:28

Here’s my-project.foo:

(ns my-project.foo)

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

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

manutter5112:04:21

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

Matsukiro13:04:03

Luminus is good for web dev?

manutter5113:04:17

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

clojurites_1913:04:42

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

manutter5113:04:01

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.

manutter5113:04:18

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.

clojurites_1913:04:22

@manutter51 thanks, will add new namespace

joelsanchez15:04:42

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

adam18:04:09

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

adam18:04:30

I just want to import a single function renamed

adam18:04:31

Cool, thanks

juan_18:04:30

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.

noisesmith18:04:31

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

juan_18:04:03

Oh, thank you. Will look into that.

juan_18:04:28

That was very helpful, thank you.

noisesmith18:04:22

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

juan_18:04:12

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.

dpsutton22:04:20

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

Azrea22:04:41

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?

mloughlin08:04:58

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.

Chase22:04:03

so I found this exercise: https://www.reddit.com/r/rust/comments/bdlna5/is_there_a_good_rust_translation_of_these_2_lines/ 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?

Chase22:04:17

the rust answers are hard core!

hiredman22:04:35

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

hiredman22:04:10

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

hiredman22:04:25

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

hiredman22:04:29

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

hiredman22:04:09

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

hiredman22:04:49

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

Chase22:04:14

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

Chase22:04:19

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 https://github.com/clojure/math.combinatorics

Alex Miller (Clojure team)22:04:21

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

hiredman23:04:42

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

hiredman23:04:19

(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

Chase23:04:24

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)
        step
        (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 :)

hiredman23:04:52

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

Chase23:04:19

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