beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
seancorfield 2021-06-14T00:47:12.121Z

@orlandomr27 Where is the reference to https://repository.jboss.org/nexus/content/repositories/deprecated/ coming from? Is that part of your custom configuration? I see you have https://repo.enonic.com/public/ in your project.clj file?

omendozar 2021-06-14T16:20:22.142600Z

Just for the record @seancorfield io.xapix/paos is now on Clojars so I deleted my .m2 folder, removed the :repositories tag from my project.clj and it’s fine now. 🙂

1
omendozar 2021-06-14T17:40:40.146400Z

Searching for another solution I came to clj-soap. While a was playing with it I realize that is very easy to consume a SOAP service as a client, easier than paos. But there are no examples on how to send an array as parameters. I was using a version forked from your repo so I wonder if you have examples on how to do it.

seancorfield 2021-06-14T17:45:43.146600Z

@orlandomr27 I couldn’t get clj-soap to do anything useful (which is why it is archived). I ended up using wsdl2java, compiling the Java files and making a JAR, and then using that with clj-http as I recall. But it was years ago — and we no longer use that 2rd party service so we no longer have to deal with SOAP! Yay!

omendozar 2021-06-14T17:48:38.146800Z

deal with SOAP it’s horrible, really. Well, I’ll stick to paos then which is working fine so far

omendozar 2021-06-14T00:53:26.123800Z

Yes, I’,m using io.xapic/paos for a WSDL Service and they recommend to add enonic repository https://github.com/xapix-io/paos#installation

Because soap-ws depends on a list of external dependencies that are not published in clojars or maven, you need to add them to the project's repository list yourself.

omendozar 2021-06-14T00:57:21.124500Z

It worked until 3 days ago, no idea what happened

Erik B Good 2021-06-14T01:10:00.126700Z

Hello, I was wondering how I could 'next' on a lazy cycle object and preserver lazyness. Let's say I have (cycle ["A" "B"]). I will get "A" if I call 'first'. I would like to get "B" the next time I would be calling first. Essentially (first (next (cycle ["A" "B"]))) And preserve the lazyness of my object. Thank you..

Erik B Good 2021-06-14T12:48:12.137600Z

yes I see what you mean @didibus I will think of another solution that behave in a pure way. Thank you for confirming what I thought

tws 2021-06-14T20:11:51.163200Z

maybe a loop. (loop [thing (first my-cycle)] … (recur (rest my-cycle)))

2021-06-14T02:30:18.126900Z

What you want isn't a functional programming behavior. You're asking for some mutation.

2021-06-14T02:50:59.127200Z

You should rethink what you are doing instead.

Yang Xu 2021-06-14T03:37:13.129200Z

Hello, I installed clojure via homebrew, and I add a new library in deps.edn that located in the clojure installed directory, and then I restart clj, but the library didn' download. Who can tell me which step I have mistake?

solf 2021-06-14T03:43:47.129300Z

A first debugging step would be to ask clojure what’s in it’s classpath:

clojure -Spath

Yang Xu 2021-06-14T03:46:13.129500Z

Nice skill, and it worked now, thank you.

solf 2021-06-14T03:46:53.129700Z

👌 There’s also

clojure -Sdescribe
to check which edn files are loaded

Yang Xu 2021-06-14T03:49:38.129900Z

Wow, how such helpful command, thank you again.

2021-06-14T05:07:45.130100Z

But if you really insist, this is a way to achieve something similar to what you are asking:

(defn mut-cycle
  [elements]
  (let [c (atom (cycle elements))]
    (fn[]
      (ffirst (swap-vals! c rest)))))

(def foo (mut-cycle ["A" "B"]))
(foo)
;;=> "A"
(foo)
;;=> "B"
(foo)
;;=> "A"
You cannot do what you want with sequences, because they force you into an immutable use. But you can achieve it with a mutable atom and a function closure like I did. Each time you call the function returned by mut-cycle you get the next element. You can call this infinite number of time, it will keep cycling.

popeye 2021-06-14T07:38:38.130700Z

I am practising clojure and wrote below code

(def  map1 [ {:size 5 :color "blue" } {:size 7 :color "blue" }  {:color "Orange" :size 8} {:color "yellow" :size 10} ] )
(def  map2 [ {:size 7 :color "blue" } {:size 8 :color "Orange" }{:color "yellow" :size 10}  ])

(defn intersectionexample
  ([s1 s2]
   (if (> (count s2) (count s1))
     (recur s2 s1)
     (reduce (fn [acc item]
               (println s2 "-- " item "---")
               (if (contains? s2 item)
                 (do
                   (update acc :match  conj item)
                   (disj s2 (set item)))
                 (do
                   (update acc :non-match  conj item))
                 ))
             {}
             s1))))

(println (intersectionexample (set map1) (set map2)))

popeye 2021-06-14T07:39:20.131200Z

This gave me clojure.lang.PersistentHashSet cannot be cast to clojure.lang.Associative at (disj s2 (set item)) what am I doing wrong!!

solf 2021-06-14T07:40:59.131300Z

You can check examples of how to call disj here: https://clojuredocs.org/clojure.core/disj

solf 2021-06-14T07:41:38.131500Z

You’re passing (set item) in second arg, however it expects one element per arguments, like this:

(disj #{1 2 3} 2 3 4)

popeye 2021-06-14T07:42:33.131900Z

This worked for me

(def st #{{:color "yellow", :size 10} {:size 8, :color "orange"}} )
(println (disj st {:size 7, :color "blue"} ))

popeye 2021-06-14T07:42:55.132100Z

but running in reduce is giving issue

solf 2021-06-14T07:43:01.132300Z

You can use apply to call a function with the arguments from a collection:

(apply disj #{1 2 3} [1 3])

phronmophobic 2021-06-14T07:44:48.132500Z

the error isn't with (disj s2 (set item)) , it's with (update acc :match conj item)

popeye 2021-06-14T07:45:23.132700Z

@smith.adriane without disj line of code it is working fine

popeye 2021-06-14T07:45:32.132900Z

i added one more code disj

phronmophobic 2021-06-14T07:46:20.133100Z

right, the value of acc is the value returned on the previous iteration. putting in the disj means that acc will be a set rather than a map for the next iteration. The first call to the reducing function should succeed and then error on the second call because of the disj

solf 2021-06-14T07:47:49.133400Z

@popeyepwr as a general remark, this kind of code is not going to work as you expect:

(do
                   (update acc :match  conj item)
                   (disj s2 (set item)))
The vast majority of clojure functions are immutable, so this update call is not going to modify acc, but return a new object, and the new object is immediately discarded as you don’t use it for anything

popeye 2021-06-14T07:48:59.133600Z

ok I agree, How can I update both acc and s2 ?

popeye 2021-06-14T07:50:35.133800Z

I do not want to use contains function from the beginning again of collection item

solf 2021-06-14T07:52:14.134100Z

You’ll have to rethink your whole code. This is the challenge of learning functional programming. But in this case you don’t need to update s2 at all. You don’t want to call contains, but there’s actually nothing wrong with that

solf 2021-06-14T07:52:28.134300Z

Here’s an excerpt from contains? doc string:

'contains?' operates constant or logarithmic time;
it will not perform a linear search for a value.

solf 2021-06-14T07:53:19.134500Z

Since you’re using contains? on a set, the search time is not going to increase linearly with the size of the set

popeye 2021-06-14T07:55:02.134700Z

this will check wach element in one map with each element with the other right? this will n*n complexity

solf 2021-06-14T07:56:22.134900Z

each contains? call is around~ constant time, so 1, then reduce is n, and… that’s it? so n total

solf 2021-06-14T07:57:40.135200Z

or n log n, I don’t know how contains? exactly works with set

popeye 2021-06-14T07:58:28.135400Z

so u mean contains? call the thread?

solf 2021-06-14T07:58:47.135600Z

there’s no thread here

solf 2021-06-14T07:59:00.135800Z

What I mean is that contains? is not O(n)

popeye 2021-06-14T07:59:15.136Z

i meant inner implementation of contains?

solf 2021-06-14T07:59:26.136200Z

it’s O(1) or O(log n) per it’s docstring, when used on sets

popeye 2021-06-14T08:02:01.136500Z

so you mean how many records available in the both the map, contains work efficiently, right?

solf 2021-06-14T08:02:35.136800Z

yes

solf 2021-06-14T08:03:12.137Z

that’s the point of using a map or set, accessing a key is linear~ time

popeye 2021-06-14T08:04:31.137200Z

thanks @dromar56 that helped

1👌
Erik B Good 2021-06-14T12:48:12.137600Z

yes I see what you mean @didibus I will think of another solution that behave in a pure way. Thank you for confirming what I thought

quan xing 2021-06-14T13:43:06.139700Z

hello, everybody How can I insert the eval exp result into the end of the exp in emacs. I used C-c C-v C-f d is nothing happen

2021-06-14T14:12:16.141200Z

accessing a key in a hash map is linear time? or constant time

popeye 2021-06-14T14:13:23.141400Z

as the document says for contains, it says logarithmic or constant depends on the data

1👍
2021-06-14T14:20:03.141700Z

> that’s the point of using a map or set, accessing a key is linear~ time does the ~ post fix mean "better than"? @dromar56

2021-06-14T15:21:47.141900Z

quote is often helpful - it reveals what the reader expands without evaluating:

user=> '@foo
(clojure.core/deref foo)

2021-06-14T15:22:41.142200Z

it even reveals itself 😄

user=> ''foo
(quote foo)

2021-06-14T15:23:02.142400Z

etc.

user=> '#(+ % %)
(fn* [p1__159#] (+ p1__159# p1__159#))

omendozar 2021-06-14T16:20:22.142600Z

Just for the record @seancorfield io.xapix/paos is now on Clojars so I deleted my .m2 folder, removed the :repositories tag from my project.clj and it’s fine now. 🙂

1
Jacob Rosenzweig 2021-06-14T17:10:37.142900Z

@noisesmith oh nice. I was doing (type foo) but this seems to tell me more.

2021-06-14T17:11:44.143100Z

it's not always useful, but it's easy to try and informative when it works

eric wolf 2021-06-14T17:11:58.143300Z

hi quan, i tried running C-c C-v C-f d in the cider REPL and got a result in a buffer called cider-result i hope that helps. i've also found that C-c C-e also does an eval and puts the results in the message window.

2021-06-14T17:12:13.143700Z

and learning why it works is a big step forward in deep understanding of clojure

eric wolf 2021-06-14T17:13:55.145500Z

today i learned : if you add dependencies to a project and install bring them in them using lein deps you have to close the cider REPL and restart it (M-x cider-restart didn't seem to work for me) before cider will allow compilation.

2021-06-14T17:19:10.145600Z

to be pedantic: you don't install anything by using lein deps , all it does is check for their location in a cache, and load them into the cache if they are currently missing

2021-06-14T17:19:46.145800Z

lein isn't an installer, it's a build tool that manages a cache and uses declarative rules to decide which things in the cache a given process should use

2021-06-14T17:19:58.146Z

this is done at startup, and that's why your repl needed to be restarted

2021-06-14T17:21:24.146200Z

also, really, "lein deps" should be considered an implementation detail of other tasks - anything you do which uses the project (start a repl, run the app, compile, build a jar) already does that deps task for you

omendozar 2021-06-14T17:40:40.146400Z

Searching for another solution I came to clj-soap. While a was playing with it I realize that is very easy to consume a SOAP service as a client, easier than paos. But there are no examples on how to send an array as parameters. I was using a version forked from your repo so I wonder if you have examples on how to do it.

seancorfield 2021-06-14T17:45:43.146600Z

@orlandomr27 I couldn’t get clj-soap to do anything useful (which is why it is archived). I ended up using wsdl2java, compiling the Java files and making a JAR, and then using that with clj-http as I recall. But it was years ago — and we no longer use that 2rd party service so we no longer have to deal with SOAP! Yay!

omendozar 2021-06-14T17:48:38.146800Z

deal with SOAP it’s horrible, really. Well, I’ll stick to paos then which is working fine so far

seancorfield 2021-06-14T17:51:51.147Z

There are ways to add new dependencies to a running REPL but they are not widely used right now. lein uses Pomegranate under the hood so you can require that in your REPL and load new libraries without a restart — see https://github.com/clj-commons/pomegranate

2💡
seancorfield 2021-06-14T17:53:00.147300Z

I use the Clojure CLI and deps.edn and a branch of org.clojure/tools.deps.alpha (via my dot-clojure repo files) and that branch — add-lib3 — has a way to load new dependencies into a deps.edn-based REPL (I use this quite a bit so I can keep my REPLs running for weeks).

2💡
eric wolf 2021-06-14T17:55:55.147500Z

Thanks for the clarification. In any case, just wanted to save others the headache of wondering what went wrong when the REPL complains. Also great resources, Sean.

2021-06-14T18:15:32.149500Z

Is there a way to build a Clojure project to Clojure, i.e. just concat all the files to one (while resolving namespaces etc.)? Like e.g., tell shadow-cljs not to compile to JS/Java, but just give a one big main.cljs/clj file. 🙂

2021-06-15T09:38:43.171200Z

Ok, thanks.

sova-soars-the-sora 2021-06-14T18:56:29.149600Z

i no understand

2021-06-14T19:05:44.149900Z

I rewrote the original message, I hope it helps.

popeye 2021-06-14T19:26:34.151200Z

i am practising clojure and I have a list as `

(def fruit ({:color "Orange" , :size 8} {:color "yellow", :size 10} {:size 7, :color "blue"}))
How can I add `
{:type "Fruit"}
each element of the list ?

2021-06-14T19:27:34.151700Z

that def makes no sense - surely that's [] or the literal is quoted

2021-06-14T19:28:14.152700Z

(map #(assoc % :type "Fruit") fruit) returns a new lazy-seq with :type "Fruit" added to each hash

popeye 2021-06-14T19:29:53.154800Z

Why `

(map assoc fruit :type "Fruit")
does not work and ?

2021-06-14T19:30:43.155400Z

super beginner here, is there an equivalent of a restProps in reagent? ex.

function Greeting({ name, ...restProps }) {
  return <div {...restProps}>Hi {name}!</div>;
}

2021-06-14T19:32:17.155500Z

map calls its first arg as a function, with the items of each other arg as args

user=> (map list [:a :b :c] [1 2 3])
((:a 1) (:b 2) (:c 3))

2021-06-14T19:32:52.155800Z

so the whole assoc, with all the args, needs to be inside a function that map can use

2021-06-14T19:34:05.156100Z

it might be helpful to note that map isn't a special syntax or operator (so it doesn't do magic argument packing / unpacking for readability), it's just a function that calls other functions on elements of collections

seancorfield 2021-06-14T19:36:47.156700Z

@mitul (defn greeting [name & more] ...)

2021-06-14T19:37:50.158100Z

and then into usually being used to replace that interopolation right? never mind I read that wrong

seancorfield 2021-06-14T19:38:03.158400Z

more will be a sequence of all the additional arguments passed into the call of greeting. If you’re producing Hiccup output, you’ll need to use into or similar to “unroll” it.

seancorfield 2021-06-14T19:38:56.159400Z

Or [name & {:as props}] if you want a hash map there.

phronmophobic 2021-06-14T19:39:30.159900Z

I thought the equivalent would look more like:

(defn greeting [{:keys [name] :as m}]
  [:div (dissoc m :name) (str "Hi " name "!")])

1💯
phronmophobic 2021-06-14T19:40:24.160400Z

although @seancorfield’s example is probably how I would write it

(defn greeting [name props]
  [:div props (str "Hi " name "!")])
;; usage
(greeting "Mitul" {:class "greeting"})

seancorfield 2021-06-14T19:42:45.161800Z

☝️:skin-tone-2: That would probably be my preferred approach — using a name and an actual hash map rather than key/value pairs — although with Clojure 1.11 that’s less of an issue since you can now pass a hash map into a function that accepts “named arguments”.

1🙌
2021-06-14T19:48:17.162Z

yes this makes sense, thank you so much!

1👍
2021-06-14T19:52:09.162400Z

also interesting, props have to be in order when they’re passed through?

2021-06-14T19:52:43.162600Z

because you didn’t define name="Mitul" , you just passed the actual string?

phronmophobic 2021-06-14T19:56:49.162800Z

If you write the function this way. The other version, you would just pass a single map:

(greeting {:class "greeting" :name "Mitul" })

phronmophobic 2021-06-14T19:57:26.163Z

Separating the props and the name is just a matter of preference

tws 2021-06-14T20:11:51.163200Z

maybe a loop. (loop [thing (first my-cycle)] … (recur (rest my-cycle)))

popeye 2021-06-14T20:48:41.163600Z

That helped @noisesmith

popeye 2021-06-14T22:01:00.166600Z

I have 2 sets which has n number of maps, where I need to perform whether elements in one map is present in another map or not, this would be a time complexity of n*n , how efficiently I can do in clojure?

2021-06-16T14:00:22.229800Z

how is this different from set/intersection? you don't need to compare every map to every map, you just need to check which maps are present in the other set (it will be most efficient if the smaller set is the first arg)

2021-06-16T14:03:01.230Z

never mind, it's fast regardless of arg order

cmd)user=> (time (s/intersection #{{:a 0}} (into #{} (map (partial hash-map :a)) (range 10000))))
"Elapsed time: 5.630487 msecs"
#{{:a 0}}
(cmd)user=> (time (s/intersection (into #{} (map (partial hash-map :a)) (range 10000)) #{{:a 0}}))
"Elapsed time: 4.806462 msecs"
#{{:a 0}}

2021-06-14T23:11:06.167Z

use clojure.set/index instead of just a bare set