Fork me on GitHub
#beginners
<
2021-10-23
>
walterl00:10:12

Re partial vs lambas ☝️: I've remember being told here (repeatedly) that lambdas are considered more idiomatic than partial and comp, and that it was backed up by pretty good reasons. There were possibly even references given, but nothing I can find now. 1) Can someone maybe help with a reference to the reasons to prefer lambdas?

hiredman00:10:50

They are often shorter, and often produce nicer stacktraces

hiredman00:10:27

The behavior of comp and partial when used to create top level defs can be surprising when you redefine things

hiredman00:10:59

I believe rich has at least once said partial wouldn't be part of clojure.core if the function short hand syntax had existed first

walterl00:10:43

I think nicer stacktraces is the "big reason" I was looking for. Thanks!

jumar04:10:00

They are also easier to debug - if that’s your thing; I find cider’s debugger very useful

slipset20:10:13

I tend to prefer partial, as it clearly just binds parameters. Any #() needs to be read and understood. I am aware of the fact that there are downsides to partial wrt REPL driven development.

walterl00:10:56

2) Why does the style guide suggest the opposite?

Ty01:10:11

Is there a primitive operation that allows rotation of a vector?

; Left rotate once
[1 2 3] => [2 3 1]

emccue02:10:13

(->> (range (count v)) (map #(rem (inc %) (count v))) (mapv #(get v %)))

emccue02:10:26

this isn’t pretty, but its one way

Ty02:10:08

I need to start using the threading macro...

Ty02:10:43

Here's what I have so far (not working)

(defn mode [scale i]
  (let [b (subvec scale 0 i)
        r (map #(+ 12 %) (subvec scale i))]
    (map #(- (scale i) %) (conj r b))))

Ty02:10:42

Most of that is normalizing values during the rotation which isn't relevant to this question

emccue02:10:04

can you write out the unit tests for what you expect?

Ty02:10:07

Honestly I don't know how atm xD I should definitely look into testing and spec ASAP. Thanks for the reminder.

Ty02:10:38

I did luckily get it working, though. It ended up being pretty weirdly that order of the % in my lambdas...

Ty02:10:51

I had to move the % to be the first param to the +/- ops.

Ty02:10:52

(conj vector single-element-vector) does not work how I would have expected, though. End up with something like [1 2 3 (4)]

Ty02:10:25

Ah, looks like I actually just fundamentally misunderstood what map is actually doing...

emccue02:10:45

spec is something else - just learn clojure.test to start

Bob B04:10:10

could you conj the first element onto a subvec?

👍 1
javi12:10:05

i may be misunderstanding what you are trying to achieve but, (into [] (take 3 (drop 1 (cycle [1 2 3])))) just wrap it in a function that takes input an number of rotations. to generalize for left and right rotations use the length of the input in the drop

Ty00:10:36

@U9VP9VCE6 FYI that worked very well for me. Thank you ❤️

javi07:10:05

👍 i am glad :) cycle is one of my fav clojure.core functions.

dpsutton01:10:26

You can make one easily with peek pop and conj but nothing primitive

dpsutton02:10:02

No I confused myself. Was working with a queue last sorry

Benjamin08:10:29

is there a named function like ignore that does this (fn [& _] ) ?

hiredman08:10:14

(constantly nil)

👍 1
popeye11:10:56

Is there anyway where we can debug how results are coming in a step for lazy-cat ? what value get into fib-seq-cat

(def fib-seq-cat
  (lazy-cat [0 1] (map + (rest fib-seq-cat) fib-seq-cat)))

(println (take 30 fib-seq-cat))

popeye11:10:21

i am not getting how this behaving internally

stagmoose12:10:58

I am trying to use clj and cljs in a monorepo and try to follow oakes' repo https://github.com/oakes/full-stack-clj-example However, I have difficulty figuring out how to have a running repl. In the readme of that repo, it says use clj -M:cljs:dev to have a webpage running at http://localhost:3000 . With that command, it worked quite well but had no repl. I tried to modify it to clj -M:cljs:dev:repl/nrepl with the hope to have a repl running with nrepl server (I have https://github.com/practicalli/clojure-deps-edn in my ~/.clojure) but it didn't work out. Is there anyone have any suggestions or encounter similar problems?

stagmoose12:10:03

btw, the deps.edn in ~/.clojure has the following alias snippest:

:repl/nrepl
  {:extra-deps {nrepl/nrepl                {:mvn/version "0.8.3"}}
   :main-opts  ["-m" "nrepl.cmdline"]}

Jim Strieter14:10:09

Hi I'm following an exercise in Web Development With Clojure by Sotnikov, 3rd edition. Page 75 (Approx Location 1470 in Kindle) When I execute: $ curl localhost:3000 I am expecting to get an output like this: <html><body>Your IP is: 0;0;0;0;0;0;0;1</body></html> Instead, I get this: <html><body>Your IP Is: 127.0.0.1</body></html> Here is my core.clj: (ns ring-app.core (:require [ring.adapter.jetty :as jetty] [ring.util.response :as response] [ring.middleware.reload :refer [wrap-reload]])) (defn handler [request-map] (response/response (str "<html><body>Your IP Is: " (:remote-addr request-map) "</body></html>"))) (defn wrap-nocache [handler] (fn [request] (-> request handler (assoc-in [:headers "Pragma"] "no-cache")))) (defn -main [] (jetty/run-jetty handler {:port 3000 :join? false})) Why am I getting an IPv4 address instead of IPv6?

dharrigan15:10:46

Depending on the OS, ipv6 will take precidence over ipv4, for example, on my Arch linux system:

dharrigan15:10:48

❯ ping localhost
PING localhost(localhost (::1)) 56 data bytes
64 bytes from localhost (::1): icmp_seq=1 ttl=64 time=0.017 ms
64 bytes from localhost (::1): icmp_seq=2 ttl=64 time=0.069

Jim Strieter15:10:04

Yes but I am getting 4. I want 6

dharrigan15:10:56

Interestingly enough, on my OS, running your code, I get this;

dharrigan15:10:58

❯ http :3000                                                                                   
HTTP/1.1 200 OK
Date: Sat, 23 Oct 2021 15:30:12 GMT
Server: Jetty(9.4.42.v20210604)
Transfer-Encoding: chunked

<html><body>Your IP Is: [0:0:0:0:0:0:0:1]</body></html>

dharrigan15:10:43

Additionally

dharrigan15:10:48

❯ curl -4 localhost:3000                                      
<html><body>Your IP Is: 127.0.0.1</body></html>%                                                                                                                                               
  ~                                                                                                                                                                                  system
❯ curl -6 localhost:3000
<html><body>Your IP Is: [0:0:0:0:0:0:0:1]</body></html>%

dharrigan15:10:31

This would suggest that whatever you're using to query the endpoint on your machine, may be preferring to initiate the connection over ipv4, not ipv6, thus the server (jetty) is responding correctly.

Apple15:10:29

curl -6 localhost:3000
perfect.

Apple15:10:00

check ip -6 ro

Jim Strieter15:10:10

::1 dev lo proto kernel metric 256 pref medium fe80::/64 dev enp0s3 proto kernel metric 100 pref medium

Apple15:10:41

so you do have ipv6 in the system. what's the url you use in browser?

Apple15:10:46

what's ping localhost

Jim Strieter15:10:32

PING localhost (127.0.0.1) 56(84) bytes of data. 64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.016 ms 64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.019 ms 64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.034 ms 64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.017 ms 64 bytes from localhost (127.0.0.1): icmp_seq=5 ttl=64 time=0.014 ms 64 bytes from localhost (127.0.0.1): icmp_seq=6 ttl=64 time=0.041 ms 64 bytes from localhost (127.0.0.1): icmp_seq=7 ttl=64 time=0.022 ms 64 bytes from localhost (127.0.0.1): icmp_seq=8 ttl=64 time=0.025 ms 64 bytes from localhost (127.0.0.1): icmp_seq=9 ttl=64 time=0.041 ms

Jim Strieter15:10:03

@UP82LQR9N oh good point lemme check...

Jim Strieter15:10:12

@UP82LQR9N the URL I use in the browser is: http://localhost:3000 Is that somehow limited to ipv4?

Apple15:10:52

you need to check /etc/hosts

Apple15:10:00

grep localhost /etc/hosts

Apple15:10:35

mine is

$ grep localhost /etc/hosts
127.0.0.1      localhost
::1	       localhost

Jim Strieter17:10:58

Mine is identical

Jim Strieter17:10:04

Found the solution. Used: http://[::1]:3000 for the URL instead of the more common http://localhost:3000, which defaults to IPv4. But also had to turn off the browser's autofill option that was converting http to https, and then complaining that the connection is insecure.

Jim Strieter17:10:09

Thanks everyone!

Apple18:10:53

i dont believe the app is ready for https. if browser hits https it will get an error. also your browser or system probably prefers v4 over v6.

sheluchin15:10:14

Is there a name for that versioning scheme that Clojurians prefer - the one that is not semver?

dharrigan15:10:23

Not really, although I do know a lot of projects (including my own), do prefer major.minor.commits over major.minor.patch

sova-soars-the-sora17:10:46

we could invent a new name for things like 0.0.0.0.3.111 sometimes I use lots of dots in my version numbers 😄

alexmiller18:10:08

Clojure depends heavily on Maven, which does not understand that

sheluchin15:10:26

Are there any commonly used release automation tools that work by looking at repo commits to create the changelog entries and follow the Maven way?

Jim Strieter15:10:16

I have no idea what that means

lispyclouds15:10:07

its a new question being asked, unrelated to the one you asked. you can maybe start a slack thread for your question?

Jim Strieter15:10:13

I'm trying to say that I have no idea what ::1 dev lo proto kernel metric 256 pref medium fe80::/64 dev enp0s3 proto kernel metric 100 pref medium means

lispyclouds15:10:38

ah right, i misunderstood

Chris17:10:55

Hi all, I'm trying to use eastwood using the practicalli edn (https://github.com/practicalli/clojure-deps-edn#lint-tools) and get the error: Unknown error: java.lang.Exception: No file found for namespace example.core-test Anyone know what this is about or a way I can get it to skip the test directory so I can at least get it to run over my src? (Edit: Figured this out. The test path was set in the edn. I thought it was default) Thanks. Edit: I will add that the tests run with koacha and the code runs through repl and main and the idiom lint works, just not eastwood.

practicalli-john12:10:59

Sounds like its not picking up the namespaces under the test directory. A quick fix may be to also use the :env/test alias in the command

clojure -M:env/test:lint/eastwood
I'll also try adding test as an :extra-path in the eastwood alias

practicalli-john12:10:22

I do include the test path in the :main-opts part of that alias by default, so I may split the aliases into two, one with just src and and one with srce and test paths

practicalli-john12:10:38

Incidentally, I tend to use kaocha for testing and clj-kondo for linting, so probably havent tested eastwood in a while. If you have a project that has an issue with eastwood you can share, let me know.

Chris15:10:42

@U05254DQM Just to let you know, adding env/test fixed my issue and I can now run eastwood including my tests. Thanks for the help!

practicalli-john15:10:49

Thanks for the feedback. I'll update the Eastwood alias with test as an extra-path.

Lukas19:10:33

Hey, is the possible to achieve the same behavior as

(jetty/run-jetty #'app {:port 3000, :join? false})
with a function like this:
(jetty/run-jetty (app inject-db-for-middleware) {:port 3000, :join? false})

didibus23:10:43

Yes, run-jetty is function, so everything in the arguments will be evaluated first and what they return will be the argument passed to run-jetty.

didibus23:10:00

So if `(app inject-db-for-middleware) ` returns a handler than it will work

solf23:10:02

If you pass directly a value to run-jetty, (like passing app instead of #'app), it has no way of knowing if app is modified. #'app is sugar syntax for (var app), a var that points to app. This extra layer of abstraction allows jetty to always access the latest version of app. In your case, the simplest way to achieve that would be something like:

(def my-app (app inject-db-for-middleware))
(jetty/run-jetty #'my-app {:port 3000, :join? false})
When you re-evaluate my-app, jetty will be able to access the latest version without restarting it.

👍 2
didibus23:10:26

That's true, if you're interested in being able to swap the handler without restarting jetty, you have to have a global var.

Lukas14:10:03

Thank you a lot