Fork me on GitHub
#beginners
<
2020-08-22
>
zackteo02:08:09

Could someone explain to me how do I tell that this function takes in 3 inputs? 😮

(require '[clojure.string :as string])

(def greetings
  (fnil string/replace "Nothing to replace" "Morning" "Evening"))

(greetings "Good Morning!" "Morning" "Evening") ;
;; "Good Evening!"
(greetings nil "Morning" "Evening")
;; "Nothing to replace"
(greetings "Good Morning!" nil "Evening")
;; "Good Evening!"

ksd03:08:15

The short answer is string/replace takes three inputs, and therefore greetings takes three inputs. fnil wraps the function it is given (in your case string/replace), returning a new function. This new function will pass along its arguments to the wrapped function, except it will replace nils in the first three arguments by the values provided when calling fnil. The function returned by fnil can take 2 or more inputs, but in your case it will fail if you give it any number of inputs other than 3, because string/replace takes exactly three arguments.

ksd03:08:12

user=> (greetings)
Execution error (ArityException) at user/eval9 (REPL:1).
Wrong number of args (0) passed to: clojure.core/fnil/fn--6902

user=> (greetings "a")
Execution error (ArityException) at user/eval11 (REPL:1).
Wrong number of args (1) passed to: clojure.core/fnil/fn--6902

user=> (greetings "a" "b")
Execution error (ArityException) at user/eval13 (REPL:1).
Wrong number of args (2) passed to: clojure.string/replace

user=> (greetings "Hello" "e" "u")
"Hullo"
user=> (greetings "a" "b" "c" "d")
Execution error (ArityException) at user/eval20 (REPL:1).
Wrong number of args (4) passed to: clojure.string/replace

ksd03:08:48

notice Wrong number of args (…) passed to: clojure.core/fnil/fn--6902 (an anonymous function produced by fnil) vs. … passed to: clojure.string/replace

seancorfield03:08:43

@zackteo Although Ruy already answered at length, one thing to remember is that you can (nearly) always see the source for library functions so you can see what is going on under the hood:

user=> (source fnil)
(defn fnil
  "..."
... (shorter arities omitted)
  ([f x y z] ; <- this is how fnil is invoked in greetings...
   (fn ; ...and it returns a function of 2-or-more arguments:
     ([a b] (f (if (nil? a) x a) (if (nil? b) y b)))
     ([a b c] (f (if (nil? a) x a) (if (nil? b) y b) (if (nil? c) z c)))
     ([a b c & ds] (apply f (if (nil? a) x a) (if (nil? b) y b) (if (nil? c) z c) ds)))))
nil
So we can see that greetings effectively has 2-arity, 3-arity, and 4+ arity implementations that will each call string/replace

seancorfield03:08:06

As Ruy pointed out, if you try to call greetings with 0, 1, 2, or 4+ arguments, it will call the anonymous function returned by fnil, so for the 0 and 1 arity calls, it will fail because the returned function does not have that arity; for the 2, 3, and 4+ arities it will call f (i.e., string/replace) with the same number of arguments.

Pragyan Tripathi06:08:48

Hey Guys, I am trying send application configuration as an HTTP API response. In my case it looks like following:

(GET "/apps" []
      {:body {:com.vadelabs.bridge.profile/base
              {:com.vadelabs.bridge/project-ns 'com.vadelabs.studio
               :com.vadelabs.block.router/compojure
               [#block/block [:com.vadelabs.studio.handler/example]
                #block/block [:com.vadelabs.studio.handler/apps]]
               :com.vadelabs.studio.handler/example {}
               :com.vadelabs.studio.handler/apps {}}
              :com.vadelabs.bridge.profile/dev {}
              :com.vadelabs.bridge.profile/local {}
              :com.vadelabs.bridge.profile/prod {}
              :com.vadelabs.bridge.server/rest {}
              :com.vadelabs.bridge/logger {}}})
Now as soon as I added tagged literal #block/block The application starts failing with following error:
; Syntax error reading source at (com/vadelabs/studio/handler.clj:19:68).
; No reader function for tag block/block
Is there a way I can send these tagged literal to the client, The client would read it as per it’s implementation.

sova-soars-the-sora19:08:25

Hmmm... What are you trying to do? You may find it useful to use a templating thing like Selmer.

Anefu Favour09:08:37

Hello everyone, I'm trying to start a cljs project using shadow-cljs, but I keep getting Undertow Request Failed and OutOfMemory errors. I get the errors after I run npx shadow-cljs watch app, when I try to connect to the running server. Thank you!

sogaiu23:08:10

if you don't get a response, you might also try #shadow-cljs

zackteo09:08:51

Thanks @seancorfield thanks - actually I thought for a moment you meant I could do something like (source greetings) thanks @ruyvalle too! Actually my mainly confusion was the six arguments. Like how this came to be ...

(greetings "Good Morning!" nil "Evening")
;; "Good Evening!"
But then I finally got why I was confused. I was thinking "Nothing to replace" "Morning" "Evening" were already arguments to string/replace but in actuality (greetings "Good Morning!" nil "Evening") just became (string/replace "Good Morning!" "Morning" "Evening") that was the part I was confused about. I understood what fnil was supposed to replace stuff when nil but it didn't click when I saw what I thought was 6 arguments with string/replace Thanks for your help!

raicotop11:08:20

Hi guys, I started to write a blog about my Clojure journey. More about it and the first post - about IDEs - here on reddit: https://www.reddit.com/r/Clojure/comments/iefjl5/choosing_clojure_ide_my_clojure_journey_part_1/

👍 6
enthusiast1711:08:39

Edit: SOLVED. Hey, I am newbie in clojure. I've installed OpenJDK 8 and clojure, leiningen. But when I run command lein repl and I've got this errors:

Could not find artifact nrepl:nrepl:jar:0.7.0 in central ()
Could not transfer artifact nrepl:nrepl:jar:0.7.0 from/to clojars (): 
Could not find artifact clojure-complete:clojure-complete:jar:0.2.5 in central ()
Could not transfer artifact clojure-complete:clojure-complete:jar:0.2.5 from/to clojars (): 
Failed to read artifact descriptor for nrepl:nrepl:jar:0.7.0
Failed to read artifact descriptor for clojure-complete:clojure-complete:jar:0.2.5
This could be due to a typo in :dependencies, file system permissions, or network issues.
If you are behind a proxy, try setting the 'http_proxy' environment variable.
Could not resolve dependencies
Help me, please!

3
👍 3
zackteo11:08:35

What do you have in ~/.clojure/deps.edn ? 😮

enthusiast1711:08:03

;; The deps.edn file describes the information needed to build a classpath.
;;
;; When using the `clojure` or `clj` script, there are several deps.edn files
;; that are combined:
;; - install-level
;; - user level (this file)
;; - project level (current directory when invoked)
;;
;; For all attributes other than :paths, these config files are merged left to right.
;; Only the last :paths is kept and others are dropped.

{
  ;; Paths
  ;;   Directories in the current project to include in the classpath

  ;; :paths ["src"]

  ;; External dependencies
 
  ;; :deps {
  ;;   org.clojure/clojure {:mvn/version "1.10.1"}
  ;; }

  ;; Aliases
	;;   resolve-deps aliases (-R) affect dependency resolution, options:
	;;     :extra-deps - specifies extra deps to add to :deps
	;;     :override-deps - specifies a coordinate to use instead of that in :deps
	;;     :default-deps - specifies a coordinate to use for a lib if one isn't found
	;;   make-classpath aliases (-C) affect the classpath generation, options:
	;;     :extra-paths - vector of additional paths to add to the classpath
	;;     :classpath-overrides - map of lib to path that overrides the result of resolving deps

  ;; :aliases {
  ;;   :deps {:extra-deps {org.clojure/tools.deps.alpha {:mvn/version "0.8.677"}}}
  ;;   :test {:extra-paths ["test"]}
  ;; }

  ;; Provider attributes

  ;; :mvn/repos {
  ;;   "central" {:url ""}
  ;;   "clojars" {:url ""}
  ;; }
}

zackteo13:08:59

I'm not too sure about this exactly :x

dorab16:08:34

What I see is that nrepl/nrepl 0.7.0 IS on clojars. So, not sure why lein is not able to pull it. FWIW, 0.8.0 seems to be the latest, but that does not appear to be your issue. Do you have a project.clj file? If so, can you share the contents? Is there a particular book or article you are trying to follow? It's Saturday morning in the US, there may be fewer folks around that can help.

dorab16:08:08

And, clojure-complete version 0.2.5 is also on clojars. Oh, also do you have a ~/.lein/profiles.clj? If so, please share its contents as well.

enthusiast1717:08:16

@U0AT6MBUL No, I don't have ~/.lein/profiles.clj

enthusiast1717:08:24

@U0AT6MBUL I am following Getting Clojure, Russ Olsen book. P.S Also, I checked lein repl in created project (with lein app) and there are the same errors.

dorab17:08:45

Looking at the errors messages a bit more closely, it says that it found nrepl, but could not transfer it from clojars. And same for clojure-complete.

dorab17:08:47

So, maybe there is something preventing the transfer.

enthusiast1717:08:00

I don't know, it is useful info or not. OS: Ubuntu 20.04 Clojure: 1.10.1 Java: openjdk version "11.0.8" 2020-07-14 OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu120.04) OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu120.04, mixed mode, sharing)

dorab17:08:05

It's been a while since I used lein so trying to remember if there is a verbose mode that would provide some more debugging info as to why the transfer is failing.

dorab17:08:23

Also, what does lein version show?

dorab17:08:51

Thanks for the system info.

enthusiast1717:08:01

lein version: Leiningen 2.9.4 on Java 11.0.8 OpenJDK 64-Bit Server VM

dorab17:08:23

That looks up-to-date.

enthusiast1717:08:53

Also I tried on Java 8 and the same errors

dorab17:08:23

I am guessing you have some permissions or connectivity issue.

enthusiast1717:08:25

Is there any way to give permits for lein?

dorab17:08:32

Not sure what you mean. lein, by itself, does not need permissions.

dorab17:08:10

Just trying some experiments myself here (on ubuntu 19.10)

dorab17:08:42

What happens if you try clj? Or clj -Sverbose at the Ubuntu prompt?

dorab17:08:17

This is just using the installed Clojure CLI, not lein, but just wanted to see if we can get that to work.

enthusiast1717:08:38

I got this message Please install rlwrap for command editing or use "clojure" instead. But, clojure works well.

dorab17:08:50

Try sudo apt install rlwrap

dorab17:08:34

Good that clojure works.

enthusiast1717:08:01

I did, clj works as clojure .

dorab17:08:02

Okay. Now try the following

dorab17:08:21

clj -Sdeps '{:deps {nrepl {:mvn/version "0.70"}}}'

dorab17:08:34

At the ubuntu prompt.

dorab17:08:46

Sorry. That was wrong.

dorab17:08:03

clj -Sdeps '{:deps {nrepl {:mvn/version "0.7.0"}}}'

dorab17:08:27

Does that download the nrepl library?

enthusiast1717:08:18

No, got errors.

dorab17:08:26

Please share.

enthusiast1717:08:07

Error building classpath. Failed to read artifact descriptor for nrepl:nrepl:jar:0.7.0
org.eclipse.aether.resolution.ArtifactDescriptorException: Failed to read artifact descriptor for nrepl:nrepl:jar:0.7.0
	at org.apache.maven.repository.internal.DefaultArtifactDescriptorReader.loadPom(DefaultArtifactDescriptorReader.java:255)
	at org.apache.maven.repository.internal.DefaultArtifactDescriptorReader.readArtifactDescriptor(DefaultArtifactDescriptorReader.java:171)
	at org.eclipse.aether.internal.impl.DefaultRepositorySystem.readArtifactDescriptor(DefaultRepositorySystem.java:255)
	at clojure.tools.deps.alpha.extensions.maven$eval813$fn__815.invoke(maven.clj:99)
	at clojure.lang.MultiFn.invoke(MultiFn.java:244)
	at clojure.tools.deps.alpha$expand_deps$fn__1230$fn__1231.invoke(alpha.clj:214)
	at clojure.tools.deps.alpha.util.concurrent$submit_task$task__500.invoke(concurrent.clj:33)
	at clojure.lang.AFn.call(AFn.java:18)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: org.eclipse.aether.resolution.ArtifactResolutionException: Could not transfer artifact nrepl:nrepl:pom:0.7.0 from/to clojars (): : Temporary failure in name resolution
	at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:424)
	at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolveArtifacts(DefaultArtifactResolver.java:229)
	at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolveArtifact(DefaultArtifactResolver.java:207)
	at org.apache.maven.repository.internal.DefaultArtifactDescriptorReader.loadPom(DefaultArtifactDescriptorReader.java:240)
	... 11 more
Caused by: org.eclipse.aether.transfer.ArtifactTransferException: Could not transfer artifact nrepl:nrepl:pom:0.7.0 from/to clojars (): : Temporary failure in name resolution
	at org.eclipse.aether.connector.basic.ArtifactTransportListener.transferFailed(ArtifactTransportListener.java:52)
	at org.eclipse.aether.connector.basic.BasicRepositoryConnector$TaskRunner.run(BasicRepositoryConnector.java:369)
	at org.eclipse.aether.util.concurrency.RunnableErrorForwarder$1.run(RunnableErrorForwarder.java:75)
	at org.eclipse.aether.connector.basic.BasicRepositoryConnector$DirectExecutor.execute(BasicRepositoryConnector.java:644)
	at org.eclipse.aether.connector.basic.BasicRepositoryConnector.get(BasicRepositoryConnector.java:262)
	at org.eclipse.aether.internal.impl.DefaultArtifactResolver.performDownloads(DefaultArtifactResolver.java:499)
	at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:401)
	... 14 more
Caused by: java.net.UnknownHostException: : Temporary failure in name resolution
	at java.base/java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
	at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:929)
	at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1515)
	at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:848)
	at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1505)
	at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1364)
	at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1298)
	at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
	at org.apache.http.impl.conn.DefaultClientConnectionOperator.resolveHostname(DefaultClientConnectionOperator.java:263)
	at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:162)
	at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:326)
	at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:610)
	at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:445)
	at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:835)
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
	at org.apache.http.impl.client.DecompressingHttpClient.execute(DecompressingHttpClient.java:164)
	at org.eclipse.aether.transport.http.HttpTransporter.execute(HttpTransporter.java:290)
	at org.eclipse.aether.transport.http.HttpTransporter.implGet(HttpTransporter.java:246)
	at org.eclipse.aether.spi.connector.transport.AbstractTransporter.get(AbstractTransporter.java:67)
	at org.eclipse.aether.connector.basic.BasicRepositoryConnector$GetTaskRunner.runTask(BasicRepositoryConnector.java:457)
	at org.eclipse.aether.connector.basic.BasicRepositoryConnector$TaskRunner.run(BasicRepositoryConnector.java:364)
	... 19 more

dorab17:08:31

Okay. That is a bit more infor : Temporary failure in name resolution

dorab18:08:23

Can you get to via a web browser from that Ubuntu machine?

dorab18:08:50

seems like your machine cannot resolve the name .

enthusiast1718:08:24

Yeah, exactly. I can't get to .

dorab18:08:16

Can you get to some other well-known host (say ).

enthusiast1718:08:18

Yes I can. I did try use VPN in browser to get and now I can get it.

dorab18:08:16

You seem to have a networking issue then.

enthusiast1718:08:52

Hmm... My internet provider blocks

dorab18:08:15

That would explain things.

dorab18:08:47

I am a bit surprised that the ISP is blocking clojars. Perhaps it is blocking http or https?

dorab18:08:17

You said you used an VPN. Is that VPN on the browser or the Ubuntu host?

enthusiast1718:08:46

VPN on the browser

dorab18:08:16

If you can set up a VPN on the Ubuntu host to get around the ISP block, you might be able to get it to work. Unfortunately, VPN on the browser will not help.

enthusiast1718:08:39

I guess, the only solution is use VPN on terminal.

dorab18:08:40

Do you have access to an external http/https proxy that your ISP does not block?

enthusiast1718:08:22

No, I don't have access

enthusiast1718:08:59

Anyway, @U0AT6MBUL thanks a lot for helping me. Now, I see issue clearly.

dorab18:08:29

Welcome. I will have to leave soon.

dorab18:08:37

Hope you can get the VPN working.

dorab18:08:58

Or convince your ISP to remove the clojars block.

dorab18:08:03

Out of curiosity, how did you install clojure and lein? Via apt?

dorab18:08:17

Can you get to github? If so, an option might be to download the nrepl (and clojure-complete) repos and then install locally into your ~/.m2/repository.

dorab18:08:02

Sorry, I have to go now. Hope you can get the connection working.

enthusiast1718:08:52

SOLVED: It was internet issue, my Internet Provider blocks , so I used OpenVPN on Ubuntu and solved this issue. @U0AT6MBUL thanks a lot again.

enthusiast1718:08:22

Now I can start a journey in clojure world!

👏 6
zackteo05:08:16

So weird that clojars is being blocked by your ISP. Sorry I wasn't much help. But glad you got it working!

Alexander G13:08:00

Hello everyone! I want to write application that will handle many events, so I wanted to use vertx for that, but I see that vertx dropped clojure support after vertx2. What do you think, current ring infrastructure (with undertow beneath, maybe) can handle such type of load efficiently? Or maybe better to use java interop here and write with vertx?

Shuai Lin01:08:49

What's the scale of your application, and have you confirmed that a thread pool based solution (e.g. jetty) really won't make it?

Alexander G11:08:41

Is it really depends on a scale? I cant predict a scale upfront for this app, but I thought it is better to pick the right tool from the start, and to not rewrite after