Fork me on GitHub
#beginners
<
2018-11-12
>
bmcferren00:11:27

many thanks @didibus I still want to get to the bottom of the general rule of whether to mutate/decorate the original object argument or to return a new object with key references to components of the original object argument

didibus00:11:52

Ok, I profiled it. Interestingly enough, fooAA is the quickest.

bmcferren00:11:34

wow - more than twice as fast

bmcferren00:11:53

I'm not sure what to make of it. I was expected fooBB to be faster and my questioning was "by how much". I have always thought that instantiating a new object is always more expensive than just editing the properties of an existing object

didibus00:11:03

Unclear, when I look at the code, they actually seem kinda similar. Since there's only 3 elements in the map. It is a PersistentArrayMap under the hood. Get only does an indexOf and returns the array element. And then {} calls createAsIfByAssoc. While update just calls get, which should be the same performance. And then calls assoc. Which is pretty similar the createAsIfByAssoc. But I'm guessing there might be some minute difference.

didibus00:11:35

Only difference I can see is assoc calls clone, while createAsIfByAssoc does a loop to copy over the data on its own.

andy.fingerhut00:11:08

Note that with Clojure's immutable collections, 'editing the properties of an existing object' is actually 'creating a new object that shares hopefully most of the contents of the original, but it depends on the editing you are doing'

andy.fingerhut00:11:37

When the operation is done, the original and the 'modified' versions are both accessible and usable in your code. Nothing has been modified in place.

didibus00:11:07

My head was in the gutter

didibus00:11:23

fooBB creates a bunch of intermediate maps

didibus00:11:30

So its obviously slower

theeternalpulse00:11:33

Any good articles or books on application Design and architectures in clojure and functional languages in general.

jumar07:11:02

@U1CUUKHDL A really good one although quite short and focused mostly on polymorphic aspects is https://leanpub.com/clojurepolymorphism

馃憤 1
jumar07:11:52

And perhaps Clojure Applied

馃挴 1
theeternalpulse05:11:53

Thanks for the suggestions

didibus00:11:38

While fooAA creates a single map

didibus01:11:58

@mcferren I think I figured it out. fooAA creates a single map. So it gets 3 values out of the map, increment, and then creates one new map with the new values. fooBB will get one key of the map, increment it, and then create an intermediate map with only the first key updated. Then, from this map, it will grab the second key, and increment that, and create anther map, etc. So in the end, it has to create 3 maps, while fooAA only has to create one.

mfikes01:11:48

Other variants that might be faster: Rely on bar being reducible:

(defn fooCC [bar]
  (reduce-kv (fn [m k v] (assoc m k (inc v))) {} bar))
Same, but make the accumulator transient:
(defn fooCC' [bar]
  (persistent! (reduce-kv (fn [m k v] (assoc! m k (inc v))) (transient {}) bar)))
Employ a transducer (which should involve a transient accumulator):
(defn fooDD [bar]
  (into {} (map (fn [e] [(key e) (inc (val e))])) bar))

didibus02:11:22

From my bench reduce-kv is fastest. The transient versions don't seem to make a difference, and are even a bit slower. But, that was tested on a small map with a lot of iterations. I suspect the transient versions would start winning if the map was larger.

didibus02:11:34

If the map is a constant though, inlining it like in fooAA with a macro is faster.

didibus04:11:41

Yup, confirmed. Is the map is bigger, the transient versions start to perform better then the non transient versions.

Karol W贸jcik11:11:47

What is a correct way of spiting the clojure map to .edn file with indentation of 2?

Karol W贸jcik11:11:49

This is what I got so far

(with-open [writer ( "report.edn")]
    (clojure.pprint/pprint inserts-map writer)
    (println "Exiting...")))
The problem with this approach is that the data is truncated.

schmee11:11:54

what REPL are you using?

Karol W贸jcik11:11:21

It鈥檚 not an issue in a repl. Rather the pprint truncates the data i think. I鈥檓 using cider

schmee11:11:54

are you sure? try running (set! *print-length* nil) in your REPL and then run agian

schmee11:11:21

pprint uses the default values of *print-length* and *print-level*, so if you set those to nil in your namespace you should be good: https://clojure.github.io/clojure/clojure.pprint-api.html#clojure.pprint/write

Karol W贸jcik11:11:56

Thank you @U3L6TFEJF 鉂わ笍 You were totally right 馃檪

schmee11:11:22

you鈥檙e welcome 馃槃

kaffein12:11:11

hey guys, I am looking for an SSE (Server Sent Events) Client library (like gniazdo for websockets) do you guys have any suggestion ?

athomasoriginal14:11:45

Are there any open source Clojure libraries that you consider excellent examples of using the language that you would recommend reading through?

Toby Clemson14:11:56

@tkjone I have found browsing through anything by James Reeves (weavejester) really useful https://github.com/weavejester

馃憤 4
schmee14:11:20

was just browsing through the Codox source a couple of days ago, very readable!

Surya Poojary16:11:17

Hello gentlemen

Surya Poojary16:11:36

Im new to clojure...

Surya Poojary16:11:50

A bit of python and ruby experience ..and a bit of java.

Surya Poojary16:11:01

Nice to meet ya'all

dangercoder16:11:51

Hey @surya.malloc, live long and prosper!upside_down_parrot

pablore16:11:07

Is there an easy way to change a leiningen project name and recursively change all it鈥檚 namespaces names?

dangercoder16:11:09

pablore I think you could check out if https://github.com/clojure-emacs/clj-refactor.el have some support for that. Else I think you have to go full bash mode.

Surya Poojary16:11:54

Guys...how should i learn clojure....i mean how to get cozy with ()

manas_marthi17:11:56

Google clojure for the brave

manas_marthi17:11:29

Search for jr0cket

manas_marthi17:11:59

GitHub modern cljs

manas_marthi17:11:13

Start with installing spacemacs

manas_marthi17:11:44

IM me if you need help with set up or writing hello world

didibus21:11:50

Read through the official Reference and Guides at http://clojure.org

didibus21:11:47

Go through the exercises at http://4clojure.org

didibus21:11:40

That's the best way to go about it at first

Eric Ervin01:11:59

I had a good time learning Clojure with the materials for Clojurebridge. Especially the material that uses Quil for a lot of interactive "hello square" "hello circle" examples. https://github.com/ClojureBridge/curriculum

馃憤 1
Eric Ervin01:11:22

I'd done this type of algorithmic drawing in other programming languages, so it was learning a new language in a domain I already knew.

Surya Poojary16:11:11

This is my first lisp lang

Surya Poojary16:11:17

And im very excited

enforser16:11:25

my suggestion is https://www.braveclojure.com/clojure-for-the-brave-and-true/ since you have experience in programming already

馃憤 1
馃挴 1
dangercoder16:11:14

Splitting up code that runs inside Core.Async go-blocks -> Seperate things in their own functions and write tests for thoose functions?

Surya Poojary16:11:01

How's clojure for gui

dangercoder16:11:03

Take a look at Clojurescript, https://clojurescript.org/about/rationale also there is: https://reagent-project.github.io/ (React for CLJS) https://github.com/Day8/re-frame (A opinionated "Reagent" for CLJS(?) I like it.) https://github.com/tonsky/rum https://github.com/omcljs/om etc.... If you want desktop-gui you can use re-frame with re-com and wrap it in electron, or use some java lib, think there are some clj-libs as well for that.

Surya Poojary16:11:50

JavaFx and swing ?

manas_marthi17:11:17

Did you try clojure Seesaw?

didibus21:11:14

You can use those. But its best to use them with interop. That is, doing java fx in Clojure will feel pretty similar to doing it in java.

Adrian Smith16:11:17

how do I read a list of files from a given directory? I have http://clojure.java.io pointed at a directory, when I evaluate it, it's a

#object[infinite_carers_checklist.core$cp_files 0x15a04efb "[email protected]"]
how do I go from that to something I can iterate on?

noisesmith16:11:19

That looks like an fn generated inside cp-files

Adrian Smith16:11:38

yep that was my issue 馃檪

bpenguin17:11:06

Hey Guys! I'm very new and I've started by going through a few tutorials and walk-throughs right now I'm working on https://www.demystifyfp.com/clojure/blog/restful-crud-apis-using-compojure-api-and-toucan-part-1/ and I'm having trouble connecting to the DB. I'm on Manjaro and I have postgres up and running on localhost:5432 with the db and user existing and having the right permissions (I can psql in).

(def db-spec
  {:dbtype "postgres"
   :dbname "restful-crud"
   :user "postgres"
   :password "postgres"})
(defn -main
  [& args]
  (models/set-root-namespace! 'restful-crud.models)
  (db/set-default-db-connection! db-spec)
  (run-jetty app {:port 3000}))
Gives me an error with a caused by that says
Caused by: java.lang.Exception: DB is not set up. Make sure to call set-default-db-connection! or bind *db-connection*.
	at toucan.db$connection.invokeStatic(db.clj:124)
	at toucan.db$connection.invoke(db.clj:117)
	at toucan.db$query.invokeStatic(db.clj:275)
	at toucan.db$query.doInvoke(db.clj:271)
	at clojure.lang.RestFn.invoke(RestFn.java:410)
	at toucan.db$simple_select.invokeStatic(db.clj:379)
	at toucan.db$simple_select.invoke(db.clj:368)
	at toucan.db$select.invokeStatic(db.clj:641)
	at toucan.db$select.doInvoke(db.clj:635)

bpenguin17:11:53

I've tried a few of the other arg map values suggested in the toucan guide but same error

seancorfield18:11:02

@btenggren20 That looks like a very old-fashioned way to interact with a DB (using mutable global state for the connection) so I'm surprised that's a recent blog post....

seancorfield18:11:17

...Ah, Toucan is trying to imitate an ORM...

bpenguin18:11:39

Yeah it uses some magic and macros to let you specify a user schema

seancorfield18:11:43

...I don't think that's a good approach, to be honest. Toucan doesn't look very idiomatic 馃槥

bpenguin18:11:58

What are the more common ways of handling connection pools and running queries?

bpenguin18:11:05

ls there like a default library?

seancorfield18:11:55

People use c3p0 or HikariCP for connection pooling mainly. The clojure.java.jdbc docs have an example of the former http://clojure-doc.org/articles/ecosystem/java_jdbc/reusing_connections.html

seancorfield18:11:16

org.clojure/java.jdbc (aka clojure.java.jdbc) is the "standard" JDBC library in Clojure (it's a Contrib library and it's the basis of pretty much everything else built around databases, including Toucan, which in turn seems to be based on / inspired by Korma).

bpenguin18:11:41

Well alright! I'll give up on this. Although I did manage to make the connections work through the Repl

bpenguin18:11:51

but not when I ran lein run

jwhitlark19:11:54

join #datomic

jwhitlark19:11:15

\join #datomic

jwhitlark19:11:52

sorry, third time's the charm.

Lennart Buit20:11:33

Its just like good ol鈥 IRC

Karol W贸jcik22:11:02

JVM noob here: Would highly appreciate if you could point me how to get rid of it during compilation?

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See  for further details.

noisesmith22:11:57

you need to add some version of sl4j to your deps, or change something else so nobody is trying to use it

noisesmith22:11:01

logging in java is a bit precious

Karol W贸jcik22:11:02

I鈥檓 trying to use exclusions like so:

[com.github.javafaker/javafaker "0.16"
                  :exclusions
                  [org.slf4j/slf4j-api
                   org.slf4j/slf4j-log4j12
                   log4j/log4j
                   org.slf4j/slf4j-simple]]
but it does not work at all 馃槥

noisesmith22:11:16

that's the opposite of what you need

noisesmith22:11:27

you need to make sure some implementation is actually present

noisesmith22:11:38

(or purge all the libs that try to use it)

Karol W贸jcik22:11:14

It鈥檚 very bizarre that I have to include some logging library to just make the compilation silent.

noisesmith22:11:31

it's because the logging design in java is a mess

noisesmith22:11:03

it's not part of the core language, and there were multiple competing implementations, and weird decisions made about how to compromise on this so libs can do logging

Karol W贸jcik22:11:21

I鈥檓 shocked

Karol W贸jcik22:11:15

Will definitely read it @noisesmith. Thank you very much 馃檪 I鈥檝e added the slf4j and it鈥檚 working perfect 馃檪 Btw where do Java guys search for the packages? I know that there is maven but is there something cleaner/with-examples/with-description like on npm website 馃槃?

Jp Soares23:11:08

Do you still recommend http://www.4clojure.com for learning? I'm bothered by the fact that I don't see clean code in the solutions.. I'm going through the exercises and looking for other's solutions to learn better approachs and more about the most used functions. What is bothering me is that I'm following a lot of people and I don't see mantainable code; code that one would commit in production; that's simple, easy and fast to understand. I see that the site value more the small code (code golf) than readablility. So I would like to know good references and examples of Clean Clojure Code, it would be perfect if you know some user with solutions like that.

didibus03:11:26

I think this is 50/50. Some of the solutions are definitly going for cleverness. But I think 50% of it is because your eyes aren't trained to Clojure yet.

didibus03:11:51

It takes some time to train your eyes to the syntax, but also the core functions, their meaning and semantics, how they compose together, etc.