Fork me on GitHub
#beginners
<
2022-01-18
>
Dumch08:01:44

how to you join lines with ‘\n’ or ‘\newline’ so you can see strings on different lines in the cursive repl? (repl out example in thread)

Dumch08:01:48

(str/join (newline) (concat ["12-20 14:03:58.745"]
                             ["12-20 14:55:07.374"]))

=> "12-20 14:03:58.74512-20 14:55:07.374"
(str/join "\newline" (concat ["12-20 14:03:58.745"]
                             ["12-20 14:55:07.374"]))
=> "12-20 14:03:58.745\newline12-20 14:55:07.374"
(clojure.pprint/pprint
 (str/join "\newline" (concat ["12-20 14:03:58.745"]
                              ["12-20 14:55:07.374"])))
"12-20 14:03:58.745\newline12-20 14:55:07.374"
=> nil
(clojure.pprint/pprint
 (str/join "\n" (concat ["12-20 14:03:58.745"]
                              ["12-20 14:55:07.374"])))
"12-20 14:03:58.745\n12-20 14:55:07.374"

flowthing08:01:18

user=> (println (str/join \newline (concat ["12-20 14:03:58.745"] ["12-20 14:55:07.374"])))
⁣12-20 14:03:58.745
12-20 14:55:07.374
⁣nil

flowthing08:01:41

I guess I'd ask what you're trying to achieve, though. 🙂

Dumch08:01:01

thanks, that worked) it’s just easier to works with separated* lines (when having large output), than with one long string

Dumch08:01:38

I tried prn, pprint, pp but missed println )

Dumch08:01:35

does anyone use some shortcut to insert a #_ comment on any form under a cursor with idea/cursive?

Dumch08:01:02

I used this in spacemacs

(defun user/clojure-comment ()
  (interactive)
  (cond
   ((not current-prefix-arg)
    (save-mark-and-excursion
      (if (equal "#_" (buffer-substring-no-properties (point) (+ 2 (point))))
          (while (equal "#_" (buffer-substring-no-properties (point) (+ 2 (point))))
            (delete-char 2))
        (progn
          (unless (or (equal (char-after) 40)
                      (equal (char-after) 91)
                      (equal (char-after) 123))
            (backward-up-list))
          (if (string-suffix-p "#_" (buffer-substring-no-properties (line-beginning-position) (point)))
              (while (string-suffix-p "#_" (buffer-substring-no-properties (line-beginning-position) (point)))
                (delete-char -2))
            (insert "#_"))))))

   ((numberp current-prefix-arg)
    (let* ((curr-sym (symbol-at-point))
           (curr-sym-name (symbol-name curr-sym))
           (line (buffer-substring-no-properties (point) (line-end-position)))
           (i 0))
      (save-mark-and-excursion
        (when curr-sym
          (unless (string-prefix-p curr-sym-name line)
            (backward-sexp))
          (while (< i current-prefix-arg)
            (insert "#_")
            (setq i (1+ i)))))))

   ((equal '(4) current-prefix-arg)
    (save-mark-and-excursion
      (unless (and (equal (char-after) 40)
                   (equal (point) (line-beginning-position)))
        (beginning-of-defun)
        (if (equal "#_" (buffer-substring-no-properties (point) (+ 2 (point))))
            (delete-char 2)
          (insert "#_")))))))

Dumch08:01:15

(define-key evil-normal-state-map (kbd ",c") 'user/clojure-comment)

Dumch08:01:36

so pressing ,c would comment any form

delaguardo09:01:32

there is a nice function defined in clojure-mode #'clojure-toggle-ignore-surrounding-form

Dumch08:01:44

It’s possble to use f as ‘form’ for selecting in visual mode in spacemacs (by default) and vim (with https://github.com/guns/vim-sexp). For example, typing daf in visual mode will delete a form (“text”, (…), […}, {…}) under the cursor. Is it possible to achieve something like this in idea with cursive?

Tero Matinlassi09:01:51

I don’t know about this - I hope someone else does. But I noticed there’s also a #cursive channel - maybe you could also try asking there?

🙏 1
Muhammad Hamza Chippa11:01:11

I am using reagent-material-ui (https://github.com/arttuka/reagent-material-ui) in my clojurescript UI and unable to create makeStyle from reagent-material-ui. In react application my makeStyle theme looks like this

import {
    makeStyles,
} from "@material-ui/core/styles";

const eventStyling = makeStyles(theme => ({
    root: {
      width: "100%",
      marginTop: theme.spacing(3),
      overflowX: "auto",
      backgroundColor: "#F1F4F6",
    },))
How can I make the same makeStyle using reagent-material-ui ?

jussi15:01:54

make-styles is in reagent-mui.jss-styles namespace. make-styles itself is defined like this

(defn make-styles
  "Takes a styles-generating function or a styles object.
   Returns a React hook which accepts a props object and returns the mapping from styles to class names.
   Note: React hooks can't be used in regular Reagent components: "
  ([styles]
   (make-styles styles {}))
  ([styles opts]
   (let [use-styles (mui-styles/makeStyles (util/wrap-styles styles) (clj->js opts))]
     (util/wrap-js-function use-styles))))
So, it should be as straightforward as requiring it and using [reagent-mui.jss-styles :refer [make-styles]] I guess (didn't try) that you above eventStyling would be something like this
(def eventStyling
  {:root {:width "100%" ...}})

Muhammad Hamza Chippa17:01:33

I don't understand it how the eventStyling variable will be used tk created make-styled?

MatElGran12:01:01

Hi, I’m playing with re-frame and trying to validate my app-db with clojure.spec as per the doc recommendation and would love some feedback. For now the (simplified) spec looks something like this:

(s/def ::start-time inst?)
(s/def ::elapsed-time int?)
(s/def ::running boolean?)
(s/def ::schema (s/keys :req-un [::running ::elapsed-time]
                        :opt-un [::start-time]))
and I have an injected interceptor that validates the app-db against the schema on every event. The thing is, ::start-time is not really optional, it should be absent if ::running is false and required otherwise. I thought about having something like this called from the interceptor (which has access to app-db)
(s/def ::running-schema (s/and
                         (s/def ::running (s/and boolean? #(%)))
                         (s/keys :req-un [::running ::elapsed-time ::start-time])))
(s/def ::idle-schema (s/and
                      (s/def ::running (s/and boolean? #(not %)))
                      (s/keys :req-un [::running ::elapsed-time])))

(defn current-schema
  [{running :running}]
  (if running
    ::running-schema
    ::idle-schema))
but, first, it does not work (validation fails with failed: boolean? spec: :tomate-web.db/running), and even if it were, I’m not sure I should even be doing that. So I guess my questions are: is it possible to define a spec based on current values, is it a good idea and if it is, how could I do it ?

lassemaatta18:01:25

There's most likely several ways to solve this. One way might be to use a predicate function to check the state using s/and.

MatElGran18:01:39

Oooh interesting, hadn’t thought about it this way, but that should definitely do it, thanks!

pavlosmelissinos12:01:21

Given a timezone string, e.g. "America/New_York" , how can I get its UTC offset in hours, i.e. -5 ? I'm using clojure.java-time (it's what the project I'm working on uses, so I'd like to stick to it at the moment)

(time/zone-offset (time/system-clock "America/New_York"))
;; => #time/zoffset "-05:00"
I can turn the ZoneOffset object into a str and parse it manually but I was wondering if there was a simpler way to do it

MatElGran12:01:40

Just guessing here, but maybe

(.get (time/zone-offset (time/system-clock "America/New_York")) java.time.temporal.ChronoField/HOUR_OF_DAY)
Also, you may be aware, or this does not apply to your problem, but some timezone offsets have fractions of hours

MatElGran12:01:25

or

(quot (.getTotalSeconds (time/zone-offset (time/system-clock "America/New_York"))) 60)

pavlosmelissinos14:01:31

I was missing the HOUR_OF_DAY constant and for some reason I completely glossed over the .getTotalSeconds method, thanks I need the offset in hours (a float/double, I'm aware timezone can be a fraction of an hour but thanks for the heads up), so this seems to work for me: (double (/ (.getTotalSeconds (time/zone-offset (time/system-clock "Asia/Rangoon"))) 3600))

MatElGran14:01:48

yes, of course, 3600, not 60, sorry about that

pavlosmelissinos14:01:59

No need to apologize! Minutes make more sense imo (div 60 gives you the hours, mod 60 the minutes) but I'm kind of stuck with this implementation 🙂

Santanu Chakrabarti18:01:03

Hi, I am trying to install deps-new (https://github.com/seancorfield/deps-new) as a Clojure tool using the command: clojure -Ttools install io.github.seancorfield/deps-new '{:git/tag "v0.4.9"}' :as new. But I am getting the error message: Execution error (ExceptionInfo) at clojure.tools.deps.alpha.extensions.git/coord-err (git.clj:45). Library io.github.seancorfield/deps-new has invalid tag: v0.4.9. I am using PowerShell in Windows 10. Please help me to resolve this issue. Please also find the detail error log attached to this post. Thanks and regards.

Alex Miller (Clojure team)18:01:44

Almost certainly quoting around the tag - try using triple quotes

Santanu Chakrabarti18:01:07

@U064X3EF3 I may sound stupid, but I can't understand what is "triple quotes". I find only single quotes and double quotes. Sorry for this!!!

seancorfield19:01:47

He means " three times.

seancorfield19:01:35

I think `{:git/tag '""v0.4.9""}' will work on Powershell (doubled quotes), but tripled quotes is needed on cmd.exe?

Santanu Chakrabarti19:01:13

@U04V70XH6 Thanks!!! Let me check with doubled quotes in PowerShell.

seancorfield19:01:14

@U02T32E0V1B The reality is that only about 5% of Clojure users are using Powershell or cmd.exe so most library readmes and books and tutorials do not cater to Windows -- 95% of the examples you'll see are for macOS/Linux. About half of Windows users do everything command-line related on WSL2/Ubuntu instead.

seancorfield19:01:10

I never use cmd.exe or Powershell on Windows -- I run all my Clojure stuff on WSL2 and then use VS Code with the remote-wsl extension on Windows to access it all.

Santanu Chakrabarti19:01:50

@U04V70XH6 Thanks for your inputs!! Previously I used to code on Ubuntu but at present I don't have that option available. I am using Atom editor for coding. I am not sure how to use WSL2 in Windows 10, but would definitely like to find out.

Santanu Chakrabarti19:01:45

@U04V70XH6 Thanks again!!! Using doubled quote I am able to successfully install deps-new from PowerShell.

1
seancorfield20:01:07

I used to use Atom with Chlorine but switched to VS Code with Clover (and also added Calva) because it's just a better-maintained editor with better Clojure extension support. You can install Ubuntu from the Microsoft Store BTW, as long as your Windows 10 is up-to-date.

👍 2
Santanu Chakrabarti20:01:08

Just read about WSL2 and found that I can install GNU/Linux distros from Marketplace. I am looking forward to experiment with it. I have also planned to use Atom with Chlorine, and as I have just completed the setup so I would like to work with it just to have a feel of it for now. May be later I would consider shifting to VS Code with Clover and Calva. Anyways, thanks for all your inputs; I shall work on them one by one.

lemuel20:01:55

Which indentation is preferred? `(:require [thing])` `(:require [thing])` I see some libraries use the latter but my editor is formatting in the first style

seancorfield20:01:00

@mail188 I think the former is more common and more in keeping with https://guide.clojure.style/

👍 2
noisesmith21:01:37

having a line break after :require is weird (if you mean literally this specific form) and there are forms that should be indented like your second example (if you mean the question generically)

lemuel21:01:33

It's more of a general question - another example (map on line 2). My editor formats in the first style but I see the second style a lot

(let error-page (layout/error-page
                 {:status 403
                  :title
(let error-page (layout/error-page
                  {:status 403
                   :title

noisesmith21:01:01

in the general case, you can refer to the style guide Sean linked, but there are specific forms that are indented in the second style (because they introduce a "body" of code), and the first option is the normal one

👍 1
Alex Miller (Clojure team)21:01:52

I don't think it's weird - I almost always do that so all requires are at the same indentation level :)

👍 2
noisesmith21:01:08

what I usually see:

(:require [foo.bar :as bar]
          [foo.baz :as baz])
the indentations still line up

upvote 2
Alex Miller (Clojure team)21:01:51

ah, I don't use that style usually, I usually indent everything 2 spaces

upvote 2