beginners

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

I would start by writing a for expression that destructs the input, and generates paths and values, then just reduce with assoc-in

2021-06-29T00:40:45.217200Z

Deconstructs

2021-06-29T00:45:33.217900Z

(reduce
 (fn [m v]
   (assoc-in m (pop v) (peek v)))
 {}
 (for [[a-or-b m] {:AAA {:prop1 ["A"] :prop2 ["a"]}
                   :BBB {:prop1 ["B"] :prop2 ["b"]}}
       [p1-or-p2 m] m]
   [p1-or-p2 a-or-b m]))

lsenjov 2021-06-29T00:46:57.218600Z

@grzegorz.rynkowski In your output, are you sure you want the nested structure to be a vector, or should it be a map?

greg 2021-06-29T11:25:45.234100Z

oh, yes, you are right, map is better there

2021-06-29T00:47:07.218800Z

ah, sorry, misread the request output

lsenjov 2021-06-29T00:47:23.219100Z

That was a question for him, actually. My bad

2021-06-29T00:49:40.220700Z

for vectors the same, just more annoying to construct, but always going through for to tear things apart

👍 1
sova-soars-the-sora 2021-06-29T05:11:44.222Z

hi how can i know if something is set up for lazy evaluation (versus total realization) ? i assume some functions are lazy while others are not... is there a good resource I can use to find out which is which?

sova-soars-the-sora 2021-06-29T05:13:51.222600Z

phrased differently, if i have a large dataset, how can i ensure i'm consuming one input at a time from the dataset, as opposed to lining it up for total eval

2021-06-29T06:21:37.224Z

The only thing lazy is lazy is lazy seqs, anything not lazy seqs in -> lazy seq out is not lazy

1
2021-06-29T06:22:33.225400Z

And lazy seqs may not be strictly lazy, they may sometimes chunk up operations to improve efficiency

quan xing 2021-06-29T09:45:06.227200Z

I run clj -X hello/run reprot error :Execution error (FileNotFoundException) at clojure.run.exec/requiring-resolve' (exec.clj:31). Could not locate hello__init.class, hello.clj or hello.cljc on classpath. I'm going to follow the code here https://clojure.org/guides/deps_and_cli

practicalli-john 2021-07-01T07:42:40.291700Z

Is it hard because you are more familiar with other programming languages? Many language are based on the concepts from C, meaning they have obvious similar concepts. Clojure is a variation of Lisp, so prior knowledge of lisp makes Clojure trivial to learn. The issues raised in this thread are related to the environment rather than the syntax of the language itself (which is very small).

quan xing 2021-06-29T09:45:22.227300Z

2021-06-29T09:50:43.227400Z

did you create src/hello.clj as stated on this page?

quan xing 2021-06-29T10:04:20.227700Z

yes

quan xing 2021-06-29T10:04:41.227900Z

im os is win10

quan xing 2021-06-29T10:05:14.228100Z

2021-06-29T10:08:59.228500Z

looks like you running clj -X hello/run from src directory but you should run it from hello-world

2021-06-29T10:09:41.228700Z

clj command expects to have deps.edn in the same directory where it is invoked

quan xing 2021-06-29T10:12:18.229500Z

yes. you'are right. good job. thank you very much!

2021-06-29T10:13:01.229900Z

np)

quan xing 2021-06-29T10:17:56.230200Z

I have another question. I Include a local jar on disk. and I use (:require [libs/pdftools :as pdf]), when I press C-c C-k compile file report error:

2021-06-29T10:24:53.230800Z

require expects a namespace not a file

2021-06-29T10:25:54.231Z

I don’t know what is inside of pdftools to suggest what to put into require.

quan xing 2021-06-29T10:28:37.231200Z

pdftools is a local disk jar file

quan xing 2021-06-29T10:29:38.231400Z

I want to use a local jar file from disk to my project,

2021-06-29T10:35:23.231600Z

sure, but the content of that jar should be either compiled java classes or clj source files. depending of type of the content you should use either :require to “require” clojure namespace or :import to load compiled java classes

2021-06-29T10:37:01.231800Z

in your last screenshot you have wrong argument for second :require statement (btw. this is an antipattern in organizing clojure namespaces, normally you should have single :require in one ns declaration)

2021-06-29T10:37:52.232Z

could you show what is inside of that jar? jar -tf path/to/file.jar

quan xing 2021-06-29T10:43:14.232200Z

This is my create a process pdf file tools in java

2021-06-29T10:48:44.232400Z

ok, so there are some java .class files then you should add into your ns something like this

(ns 
  ...
  (:import (org.pansome.pdf.common PdfUtils)
           (org.pansome.pdf.common FontUtils)
           ...))
that means you are “importing” java class to make it accessable as PdfUtils , FontUtils etc.

2021-06-29T10:49:38.232600Z

https://clojure.org/reference/java_interop this guide might be handy

quan xing 2021-06-29T10:53:07.232800Z

is this right?

quan xing 2021-06-29T10:53:55.233Z

in deps.edn file add depend file

2021-06-29T10:54:55.233200Z

yes

quan xing 2021-06-29T11:02:15.233400Z

run error 😭

quan xing 2021-06-29T11:02:41.233600Z

2021-06-29T11:04:28.233800Z

(org.pansome.pdf.common PdfUtils) note the difference - in my form there is no dot between common and PdfUtils

quan xing 2021-06-29T11:37:56.234600Z

Thanks, It's run.

quan xing 2021-06-29T11:39:39.234800Z

clj -X hello/run is run. but in Emacs C-c C-k is error.

2021-06-29T11:50:06.235Z

emacs? looks like screenshots are taken in intellijidea

practicalli-john 2021-06-29T12:06:02.235200Z

@delaguardo Emacs 27 with tabs and LSP activated makes it look a lot more like Intellij...

practicalli-john 2021-06-29T12:07:14.235400Z

@imxingquan there is an #emacs channel for help. You might want to say what command you are running to help understand what you are doing in Emacs. The key binding could be doing anything...

practicalli-john 2021-06-29T12:10:09.235700Z

Generally you need to evaluate things in order, so ensure the namespace has been evaluated in Emacs with Cider, which makes the functions from the required libraries available to the rest of the code.

quan xing 2021-06-29T13:51:07.235900Z

How can I import dependencies from mvn. report error like this

sheluchin 2021-06-29T14:49:22.239300Z

I'm trying to use this thing: https://clojars.org/cljsjs/rangy-textrange Steps I've taken: - added cljsjs/rangy-textrange {:mvn/version "1.3.0-1"} to deps.edn under :deps - added [cljsjs.rangy-textrange :as rangy] to my ui.cljs under :require - restarted my compiler and confirmed that rangy gets installed:

shadow-cljs - starting via "clojure"
Downloading: cljsjs/rangy-textrange/1.3.0-1/rangy-textrange-1.3.0-1.pom from <https://repo.clojars.org/>
Downloading: org/clojure/clojure/maven-metadata.xml from <https://repo1.maven.org/maven2/>
Downloading: org/clojure/clojure/maven-metadata.xml from <https://repo.clojars.org/>
Downloading: cljsjs/rangy-core/1.3.0-1/rangy-core-1.3.0-1.pom from <https://repo.clojars.org/>
Downloading: cljsjs/rangy-core/1.3.0-1/rangy-core-1.3.0-1.jar from <https://repo.clojars.org/>
Downloading: cljsjs/rangy-textrange/1.3.0-1/rangy-textrange-1.3.0-1.jar from <https://repo.clojars.org/>
but now when I go to compile it, I get the following error:
The required namespace "cljsjs.rangy-textrange" is not available, it was required by "sheluchin/ui.cljs".
Can anyone explain what I'm doing wrong?

Fredrik 2021-06-29T15:14:09.240Z

According to https://github.com/cljsjs/packages/wiki/Using-Packages#a-quick-javascript-interoperability-refresher, you shouldn't use :as rangy . Simply require cljsjs.rangy-textrang , then use js/some-fn-from-rangy to call into it.

Fredrik 2021-06-29T15:17:28.240800Z

I'm not 100% sure your problem is related to this, because I don't get the same error. However, when I do as the link suggest, everything seems to work fine. EDIT: Nevermind, I didn't catch you were using shadow-cljs 🙂

dpsutton 2021-06-29T15:19:49.241100Z

shadow doesn't work with cljsjs packages, but with the native npm package itself

pithyless 2021-06-29T15:22:15.241600Z

This may also be relevant: https://shadow-cljs.github.io/docs/UsersGuide.html#_using_npm_packages

sheluchin 2021-06-29T15:56:27.241900Z

Thanks all.

bnstvn 2021-06-29T16:09:58.246800Z

I’m trying to process a 2mb xml with xml-seq parsed with clojure.data.xml/parse with 1Gb heap — I’m getting

Syntax error (OutOfMemoryError) ,,,
Java heap space
with higher heap it works and it is fairly fast. it boils down to the below — any obvious thing I’m doing wrong?
(-&gt;&gt; (xml-seq response)
       (filter #(some? (seq (:content %))))
       (keep (fn [e]
               (when-let [id (get-in e [:attrs :id])]
                 [id e])))
       (into {})))

bnstvn 2021-07-02T09:40:39.359200Z

[turned out i actually fixed this issue and encountered a similar one with a bigger xml (30mb) — apologies — that didn’t even parsed with 512mb. in that case, i only needed to count some elements, and eventually went with plain java stax route. thanks for the help!]

bnstvn 2021-06-29T16:10:38.246900Z

from the OOM heap dump:

bnstvn 2021-06-29T16:11:22.247100Z

allocation profile with async profiler:

2021-06-29T16:13:50.247300Z

you could refactor ->> to transducer equivalent to avoid creation of intermediate collections

(into {}
      (comp (filter #(some? (seq (:content %))))
            (keep (fn [e]
               (when-let [id (get-in e [:attrs :id])]
                 [id e]))))
      (xml-seq response))

Apple 2021-06-29T16:14:44.247500Z

i was gonna say the filter and keep can be fit into one keep, right?

Apple 2021-06-29T16:15:03.247700Z

on top of transducer

2021-06-29T16:15:57.247900Z

the semantic is different, not sure if it is possible to replace with single operation

bnstvn 2021-06-29T16:17:55.248100Z

thanks, trying this out — but what it the actual issue here? is this somewhat expected?

Apple 2021-06-29T16:18:27.248300Z

(into {} (comp (keep (fn [e] (if (some? (seq (:content e))) (when-let [id (get-in e [:attrs :id])] [id e]))))) (xml-seq response))

alexmiller 2021-06-29T16:20:22.248500Z

every seq op is essentially a new chain of seq cells over the input (this is mitigated somewhat by chunking)

Apple 2021-06-29T16:20:48.248700Z

if that holds transducer is not needed.

alexmiller 2021-06-29T16:21:36.249Z

transducers remove all intermediate seq cells and produce only the output collection, which is particularly good if you are going to hold the entire output in memory

sova-soars-the-sora 2021-06-29T16:21:44.249300Z

favorite lib for assertions / testing ?

alexmiller 2021-06-29T16:22:45.249400Z

this definitely seems like a case where transducers would reduce memory footprint

Apple 2021-06-29T16:22:47.249600Z

2mb is not that big probably something else applies here

alexmiller 2021-06-29T16:23:31.249800Z

the in-memory object representation of that 2 mb is probably a lot bigger (and then there are multiple seq wrappers around each node)

Apple 2021-06-29T16:29:29.250Z

other aspects: 1) some? is not needed 2) (get-in e [:content :attrs :id]) instead of (get-in e [:attrs :id]), right?

bnstvn 2021-06-29T16:30:56.250200Z

well seems a good opportunity to learn transducers.. in the meanwhile, with clj-memory-meter, the retained size of the whole xml is:

(mm/measure (xml/parse-str (slurp "/Users/bani/tmp/loadgroup.xml")))
=&gt; "3.8 MiB"

greg 2021-06-29T16:34:24.250400Z

for testing data structures https://github.com/nubank/matcher-combinators is really nice

➕ 2
alexmiller 2021-06-29T16:37:31.250800Z

is that fully realized or lazy?

alexmiller 2021-06-29T16:39:01.251Z

I guess that's realized

bnstvn 2021-06-29T16:39:19.251200Z

(mm/measure (doall (xml-seq (xml/parse-str (slurp "/Users/bani/tmp/loadgroup.xml")))))
=&gt; "26.6 MiB"

alexmiller 2021-06-29T16:39:35.251400Z

well, might be something else going on then

sova-soars-the-sora 2021-06-29T17:02:09.253200Z

neat

bnstvn 2021-06-29T17:09:43.253400Z

(seems so, i failed to create a minimal example yet..)

alexmiller 2021-06-29T17:12:16.253600Z

tools like yourkit can take heap snapshots and diff heap snapshots which I find to be pretty instructive in understanding leaks

alexmiller 2021-06-29T17:12:36.253800Z

they also have a "path to root" which can tell you why something is being held

2021-06-29T19:44:48.256600Z

I’m finishing my read of the Brave Clojure and would like to know where to go from here. I started a small project putting the things I learned to work and would like to know more about the following topics: • Server-side clojure, or how to make a rest api with it • Deploying clojure, specially on Heroku. Any help or recommended readings would be nice, thanks!

2021-06-30T08:10:41.259100Z

Here is an example of a backend REST API whose contains all Heroku deployment config, etc. Maybe it can help: https://github.com/prestancedesign/todo-backend-reitit

jeffy 2021-06-29T19:49:02.256800Z

I found this documentation helpful to deploy a very simple app to heroku. https://devcenter.heroku.com/articles/getting-started-with-clojure?singlepage=true

practicalli-john 2021-06-29T21:35:30.257200Z

https://practical.li/clojure-webapps/ may have some interesting pieces for you. Also take a look at metosin/reitit as a data oriented alternative to compojure for routing