Fork me on GitHub

Hi, I have a datomic sql transactor running

System started datomic:sql://<DB-NAME>?jdbc:, you may need to change the user and password parameters to work with your jdbc driver
and i want to connect to it now, i tried
(d/create-database "datomic:sql://<DB-NAME>?jdbc:")
=> Execution error (URISyntaxException) at .URI$Parser/fail (
Illegal character in opaque part at index 14: datomic:sql://<DB-NAME>?jdbc:
(d/create-database "datomic:")
=> Execution error (SQLException) at java.sql.DriverManager/getDriver (
No suitable driver
and a few more combinations. Could anybody explain to me how I make this work? Any help is greatly appreciated. :hugging_face:


you could try asking the same question in #datomic


thx i give it a try


Hey folks, I have a question regarding reagent and SVG files. Is it possible to import an SVG file and inline it inside hiccup as a component? For example, in JS+React one would do something like

import MyIcon from './myIcon.svg';
If not, what are possible/usual ways how to handle SVG files without directly copying their content into .cljs files? Thank you


Not that I’m aware of, but you can very easily wrap them yourself with a function component. I’d suggest you use the object tag if you need to access the SVG elements from js


Can show you an example in a bit!


Thank you, would love to see it


(defn foo []
  ;; with this code you can access all your svg elements in JS
   {:data "/file.svg"
    :id "file"
    :width ...
    :height ...}
   ;; make sure you access the elements only when the svg has fully loaded
   :onLoad (fn []
             (let [svg-content (.-contentDocument
                                (js/document.getElementById "file"))]
               ;; now you can normally access your svg elements in JS and
               ;; perform any operation i.e.
               (set! (-> (.getElementById svg-content "my-svg-element-id")
                         .-style .-fill) "red")


^use this approach if you need to control elements within your svg


if you only need to display the svg, then you could just do: [:img {:src "/file.svg"}]


  (:require [xxx.records]
            [xxx.records :refer [map->Account]])
  (:import [xxx.records Account]))
How do I require both the namespace and the auto generated map function for my Account record?

Ramon Rios15:01:14

Folks, just a question to check if my understanding is right: macro is a way of treat a value before it be evaluated by clojure evaluator. Am i right?


this is my understanding as well - but I too am completely new to clojure

Ramon Rios15:01:02

That's fine, we're all begginers.


Macros essentially re-write your source code just before the evaluator evaluates it. You have to be careful when you say a macro does something to a “value” because the only values available to a macro are the actual symbols and constants that make up your source code.


not really, or at least it depends on value deffinition. In my mind it comes from the step in Read/Eval/Print/Loop. macroses are evaluated in Reading step and functions in Evaluating step.


(defmacro print-if-odd
  (if (and (number? some-value)
           (odd? some-value))
    `(println ~some-value "is odd")))
=> #'user/print-if-odd
(let [x 5]
  (print-if-odd x))
=> nil
(print-if-odd 5)
5 is odd
=> nil
x is a runtime value, so at “macro time” it won’t exist yet. Calling (print-if-odd x) will be evaluated with some-value set to the literal symbol 'x , not its runtime value of 5.


But when you call (print-if-odd 5), you’re passing in 5 as a literal constant value in the source code, so then your macro will evaluate some-value as the literal 5.


(let [x 5]
  (macroexpand-1 '(print-if-odd x)))
=> nil
(macroexpand-1 '(print-if-odd 5))
=> (clojure.core/println 5 "is odd")


(some-macro arg1 arg2) during reading will read arg1 and arg2 before executing some-macro. So for some-macro values of arg1 and arg2 will be the it’s form after reading. (some-function arg1 arg2) during evaluation will eval arg1 and arg2 before. So for some-functions valueas of arg1 and arg2 will be the values of them after evaluation


if arg1 and arg2 are macros themselves, will they be expanded before some-macro or after?


before, because during the read only macroses are available to been executed. calculation of this (some-macro arg1 (another-macro arg2)) 1. some-macro is a macro so reader will “execute” it 2. to execute we need to read its arguments (`arg1` and (another-macro arg2)) 3. arg1 is not a macro so we can just read it 4. another-macro is a macro so the reader will “execute” it 5. to execute we need to read its arguments (`arg2`) 6. arg2 is not a macro so we can just read it 7. lets bubble up… another-macro executed because arg2 already expanded by reader 8. arg1 and result of (another-macro arg2) also expanded so we can execute (some-macro …) 9. done. all the steps above was done in reading step


then eval step hits the ground


This is why the First Rule of Macros is “Don’t use macros” 😉


nono) don’t be afraid of macro) the only difference - what arguments are for them, thats it.


i'd like to master macros, i don't know what i'd use them for


Definitely worth learning, and very useful once you get the hang of thinking like a compiler.


translate all my clojure code into java bytecode directly 😈 ?


thats what i’m doing right now)


@delaguardo thanks, that was a useful explanation


macros are a programmatic way to edit code?

💯 8

i get the general idea but i still have a hard time stumbling into a real usecase. maybe my macro eyes are dusty


(defmacro consK
  ([K] `(~K ()))
  ([el K] `(~K (cons ~el nil)))
  ([l el K] `(~K (cons ~el ~l))))

(defmacro assocK
  ([K] `(~K {}))
  ([[k v] K] `(~K (assoc {} ~k ~v)))
  ([m [k v] K] `(~K (assoc ~m ~k ~v))))

(declare list-to-cps const-to-cps)

(defmacro to-cps [n K]
    (seq? n) `(list-to-cps ~n (fn [x#]
                                (prn "===DBG list" x#)
                                (~K x#)))
    (number? n) `(const-to-cps ~n (fn [x#]
                                    (prn "===DBG const" x#)
                                    (~K (if (= x# 2) ::panic! x#))))))

(defmacro const-to-cps [value K]
  `(~K ~value))

(defmacro list-to-cps [l-expr K]
     (empty? l-expr)
     `(consK ~K)

     (= 1 (count l-expr))
     `(to-cps ~(first l-expr)
              (fn [el#]
                (consK el# ~K)))

     `(list-to-cps ~(rest l-expr)
                   (fn [l#]
                     (to-cps ~(first l-expr)
                             (fn [el#]
                               (consK l# el# ~K)))))))


as an example: this set of macros can transform clojure list into continuation passing style form


(to-cps '(1 2 3) (fn [x]
                   (prn "===DBG" x)
to quick try it out


I think the -> macro is a good example too


(source ->) in the repl to have a look at it.


When you do that, though, you have to remember that condition has to be a literal code value, not a runtime value, as in my first example above.


So for example you can write a macro that says “If I have an odd number of arguments, throw an exception” but you can’t (successfully) write a macro that says “If the current user is logged in, do X”


(if (am-i-logged-in?) true false) why not?


You can use a macro to rewrite code into something that will do X if the current user is logged in, but at macro time the value of current-user is not defined.


I'm pretty sure the diesel ORM for Rust implements a macro that connects to your database at compile time 😬

😱 4

if its what i'm thinking, they also typecheck your queries


I read about that years ago at this point, so take my assertion with a grain of salt 😉


the meander lib uses a lot of cool macros to do its work

Eric Ihli19:01:34

What's a good way to inspect a Clojure object? I miss Pythons dir


(type x) tells you what type a value is. If it is a map, (keys x) tells you the keys. You can just type x at a REPL and see the value, although it might be bigger than you want to look at. (pprint x) can show it with nicer formatting, rather than one long line.


Cognitect's REBL has a graphical UI for exploring large and/or nested values in various ways.


I will mention clojure.inspector/inspect-tree but at the same time warn you that it has some limitations of what kinds of values it works with that can be annoying. REBL is more general purpose and polished by far.


Some Clojure IDEs have their own value inspectors they provide, but I have not used any of them.


for inspecting Java objects, bean is good

Eric Ihli19:01:04

Rad. Thanks. I'll dig into some of these.


or, if you want to really deeply inspect a java object, clojure.reflect/reflect will give you way more information than you could ever want. just remember it's pretty slow


(ns-publics *ns*) will show you all the public symbols defined in the current namespace, as a hash map from symbols to Vars, so something like (keys (ns-publics 'clojure.set)) is probably easier to read.


For simple inspecting of clojure data structure like a map, I've found the reader tag #spy/p from invaluable.


Is ISeq implemented for JS objects, arrays, and ES6 sets? I’m wondering if it would be possible to use keep immutability through functions rather than the data structures being immutable themselves


Every immutable data structure is only immutable because you choose not to do anything to it to modify it, even in Haskell. It is simply how many hoops you need to jump through to mutate it.


I haven't done any JS development myself, really, but my impression was that the issue in JS is that most code and libraries are written assuming it is OK to mutate things you pass in function calls, so you can write all the immutable library code you want, but as soon as you step outside of that, you are in danger?


I guess that’s possible yes. Working in a pragmatic CLJS app I can’t imagine that risk could be avoided


I know that you can say "be diligent, and avoid calling that code". The thing is, that is a hoop that is so easy to jump through that it is hard not to jump through it, at least on a real project of any significant size or longevity. Not impossible, just challenging.


The impetus for the question was around performance. I was talking with some who work in the creative tech / interactive graphics area. They said they moved away from CLJS because of performance issues with immutable data, even with transient


So I wondered if we could “enforce” immutability for JS data structures directly


As far as your question on ISeq implemented for various types, folks in the #clojurescript and/or #cljs-dev channels would be likely to know more, more so than #beginners


I am sure it is possible to make someone jump through at least one more hoop to mutate something, in some cases, but if that hoop means you cannot call the JS library you want on your data, then it wouldn't achieve the goal, perhaps. I am speaking generally here, not from specific knowledge of JS and the possibilities, so don't take my word for it.


Hmm, this is certainly something to think about. I would hope that I could find JS libs with immutable operations by default, but hard to ssay


#clojurescript is likely to find people who have thought about this more (and more importantly, some of them know JS and JS libraries, unlike me)


Sure, I will ask in there


using real mutation in tight loops (eg. graphics, signal processing) is a really common fallback (I've even had to resort to this for a graph in a social data analysis project to make our algorithm fast enough), but ideally you can code everything with clojure default immutable first, then translate the trouble spots to normal mutables in for loops

👍 4

I don't think pretending javascript arrays are iseq helps much there - once you know where the problem is, do the ugly fast thing, if you are profiling to find hotspots and do this in a principled way it's not likely to spread to the rest of the codebase

👍 8

(come to think of it, we had to use java mutable arrays for graphs on the backend .. and on the display side we did have to use js arrays specifically for the graph data display)


@U051SS2EU What type of work were you doing with signal processing and graphics? And which methods did you use for real mutation?


for graphics it was displaying graph connection data, for signal processing it was hobby work at home doing audio stuff (via a library called pink)


for real mutation, you use the generic mutable data types the platform defines, via interop


in fact, it was easier to rewrite the back end graph code in java (complete with a vector -> array translation step for incoming data) than it was to type hint and unbox all the stuff correctly in clojure code


thanks to clojure's excellent interop, both are easy to do


Nice, thanks for the details


It also is possible to extend them to support ISeq, see the other section:


(defn it-seq [iterator]
  (let [next (.next iterator)]
    (when-not (.-done next)
          (.-value next)
          (it-seq iterator))))))

(extend-type object
    (it-seq (.values this))))

(map inc (doto (js/Map.) (.set 1 1) (.set 2 2) (.set 3 3)))


Might be a few more protocols to implement, but this gives you first, second, nth, next, rest, seq which already covers most things like map, filter, remove, concat, etc.


It'll work with every object that support .values returning an iterator


there are drawbacks to plugging mutables into code written for immutable objects though, and the root issue here is best addressed by not using the ISeq api

💯 4

Ya, I agree, but, you know, if you're really going for optimization and trying to reuse the same APIs of your existing logic or wtv, if you keep it local to a namespace and know what you're doing etc. Basically, I gave you a gun, don't shoot your foot with it 😛