Fork me on GitHub
#beginners
<
2020-08-08
>
Stas Makarov08:08:52

I want to get a seq of vectors from two seqs: "items" and "hotkeys". map vector items hotkeys almost does the job, but "items" might have more elements than "hotkeys". So I want the result of (thisfunc [1 2 3] "qw") to be ([1 "q"] [2 "w"] [3 nil]) I came up with this, but there's probably a nicer way..

(defn tmp [s1 s2]
  (let [cmp (compare (count s1) (count s2))
        s1 (if (< cmp 0) (concat s1 (repeat nil)) s1)
        s2 (if (> cmp 0) (concat s2 (repeat nil)) s2)]
    (map vector s1 s2)))

Markus Kiili08:08:40

(defn thisfunc [s1 s2] (map vector s1 (concat (map str s2) (repeat nil)))) seems to work.

jsn08:08:36

(defn f [& seqs]
  (let [cnt (apply max (map count seqs))
        seqs (map #(concat % (repeat nil)) seqs)]
    (take cnt (apply map vector seqs))))

Stas Makarov09:08:28

thank you guys!

Jim Newton08:08:18

Again I don't understand the namespacing of symbols referring to records (defined by defrecord ). I have a record defined in a namespace as follows:

(ns clojure-rte.bdd
  "Definition of Bdd."
  (:require [clojure-rte.cl-compat :refer [cl-cond]]
            [clojure-rte.util :refer [fixed-point member group-by-mapped print-vals]]
            [clojure-rte.type :as ty]
            [clojure.set :refer [union difference intersection]]
))

(defrecord Bdd
  [label positive negative])
and I also have a test case defined as follows
(ns clojure-rte.bdd-test
  (:require [clojure-rte.bdd :refer :all]
            [clojure.test :refer :all]))

(deftest t-typep
  (testing "bdd and-not"
    (with-bdd-hash []
      (is (bdd-typep 42 (Bdd. 'Long)))
      (is (not (bdd-typep 42 (Bdd. 'String))))
      (is (not (bdd-typep "42" (Bdd. 'Long))))
      (is (bdd-typep "42" (Bdd. 'String)))
      (is (bdd-typep 42 (bdd-or (Bdd. 'Long)
                                (Bdd. 'String))))
      (is (bdd-typep "hello" (bdd-or (Bdd. 'Long)
                                     (Bdd. 'String))))
      (is (bdd-typep 42 (bdd-and-not (Bdd. 'Long)
                                 (Bdd. 'String))))
      (is (not (bdd-typep "hello" (bdd-not (Bdd. 'Long)
                                           (Bdd. 'String)))))
      (is (not (bdd-typep 42 (bdd-and-not (Bdd. 'String)
                                          (Bdd. 'Long)))))
      (is (bdd-typep "hello" (bdd-and-not (Bdd. 'String)
                                          (Bdd. 'Long)))))))
Both from the shell lein test and also if I restart cider and run the tests I get the following error.
clojure-rte.core> (test/run-tests 'clojure-rte.bdd-test)
Execution error at clojure.test/test-ns (test.clj:743).
No namespace: clojure-rte.bdd-test found
Syntax error (IllegalArgumentException) compiling new at (bdd_test.clj:77:25).
Unable to resolve classname: Bdd
It seems the ?parser? cannot understand the Bdd. syntax. Can someone tell me what's wrong?

Jim Newton08:08:08

I've discovered something more I don't understand. If the current ns is that in which the Bdd record is defined, clojure-rte.bdd , then I can evaluate Bdd in the repl to obtain clojure_rte.bdd.Bdd which is an object of type java.lang.Class

clojure-rte.bdd-test> (in-ns 'clojure-rte.bdd)
#namespace[clojure-rte.bdd]
clojure-rte.bdd> Bdd
clojure_rte.bdd.Bdd
clojure-rte.bdd> (type Bdd)
java.lang.Class
clojure-rte.bdd>  
However, if the current name space is something else, a ns which requires clojure-rte.bdd
(ns clojure-rte.bdd-test
  (:require [clojure-rte.bdd :refer :all]
            [clojure.test :refer :all]))
Then Bdd cannot be evaluated at the repl, and (resolve 'Bdd) returns nil
clojure-rte.bdd> (in-ns 'clojure-rte.bdd-test)
#namespace[clojure-rte.bdd-test]
clojure-rte.bdd-test> (resolve 'Bdd)
nil
clojure-rte.bdd-test> Bdd
Syntax error compiling at (clojure-rte:localhost:61915(clj)*:1:15115).
Unable to resolve symbol: Bdd in this context
clojure-rte.bdd-test> 

Jim Newton08:08:19

however, the fully qualified name is resolvable

clojure-rte.bdd-test> clojure_rte.bdd.Bdd
clojure_rte.bdd.Bdd
clojure-rte.bdd-test> (type clojure_rte.bdd.Bdd)
java.lang.Class
clojure-rte.bdd-test> 

Jim Newton08:08:14

Perhaps it is the case that (ns clojure-rte.bdd-test (:require [clojure-rte.bdd :refer :all])) does not import record names into the namespace?

Jim Newton08:08:53

Since this is a lot of text, I'll copy it to clojureverse. https://clojureverse.org/t/failed-to-import-record-names/6365

Matthew Curry13:08:32

To pull in all bindings from one ns to another, use ‘(:use <ns>)’ inside your ns form, instead of ‘:require’ for that ns. Pulling everything in is not recommended generally of course, but may make sense for a test namespace to pull in the implementation namespace.

noisesmith18:08:40

require does not import class names, you can use :import for that, or even better use the auto generated ->Bdd function that clojure creates instead of the class constructor

Jim Newton09:08:14

Can someone help me understand this error message? Why is it complaining about java.util.concurrent.Future ?

clojure-rte.bdd-test> (def ^:dynamic *xyzzy* false)
#'clojure-rte.bdd-test/*xyzzy*
clojure-rte.bdd-test> (map? @*xyzzy*)
Execution error (ClassCastException) at clojure-rte.bdd-test/eval15290 (form-init5713417271367683958.clj:26770).
class java.lang.Boolean cannot be cast to class java.util.concurrent.Future (java.lang.Boolean and java.util.concurrent.Future are in module java.base of loader 'bootstrap')
clojure-rte.bdd-test> 

Jim Newton09:08:44

I get the same error from @false

zclj10:08:07

@ is a reader macro for deref. false is a boolean and can not be deref:ed, hence the error

zclj10:08:55

same for your *xyzzy* it is a boolean

zclj10:08:39

so in essence, don't use @ unless dealing with something that is dereferable

Jim Newton11:08:33

is there a predicate for deciding whether something is @ compatible? I notice the warning on atom? which says it is not a function for deciding if something has been returned from atom

noisesmith18:08:11

you can ask via instance? if the object is a clojure.lang.IDeref or java.util.concurrent.Future - those are the two supertypes (your perplexing error is because the second, fallback case failed)

Jim Newton12:08:14

is there a %something I can use in (format ...) which will cause the object to be printed with print-method rather than printing with its default print?

(defmethod print-method Bdd [bdd w]
  (.write w (format "#<Bdd %s %s %s>" (:label bdd) (:positive bdd) (:negative bdd))))
this is my print-method but when (:positive bdd) is another object of the same type, it prints as #<Bdd Short true [email protected]> rather than making a recursive call to print-method

Jim Newton12:08:58

If I use cl-format, apparently it works with ~A (as in Common-Lisp)

(defmethod print-method Bdd [bdd w]
  (.write w (cl-format false "#<Bdd ~A ~A ~A>" (:label bdd) (:positive bdd) (:negative bdd))))
which outputs something like this as requested:
#<Bdd java.io.Serializable true #<Bdd Double true #<Bdd Long true false>>>

Matthew Curry14:08:26

Clojure core’s format is just java String’s so probably not

Jim Newton14:08:33

well I have a work-around. But doesn't the java format have a way to trigger the java print-self/to-string method (whatever it is called, I don't know java).

Jim Newton14:08:11

and is print-method in any way linked to the java to-string method?

Matthew Curry16:08:12

No, it’s clojure’s own multimethod thing iirc. I think you want to look up clojure’s protocols to implement the java toString interface

Jim Newton15:08:04

what is the correct way to determine whether two values are the same object as opposed to simply being = ? I found the function eq but it has https://clojuredocs.org/clojure.core.logic.fd/eq.

dpsutton15:08:01

(doc identical?)

👍 3
Jim Newton15:08:20

Thanks.

👍 3
Dimitar Uzunov21:08:28

What are the best practices on doing a docker build or equivalent with tool-deps? Should I create a jar and run it in the container?

seancorfield22:08:29

@dimitar.ouzounoff Overall, I think I've seen most folks end up going with an AOT'd "uber" JAR file run inside Docker via java -jar.

seancorfield22:08:24

I think that gives you the fewest moving parts and the most "standard" approach.

seancorfield22:08:54

(I assume you mean for running an app in production?)