This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-08-31
Channels
- # announcements (3)
- # aws-lambda (1)
- # babashka (122)
- # beginners (241)
- # calva (28)
- # cider (7)
- # clara (7)
- # clj-kondo (43)
- # clojars (5)
- # clojure (326)
- # clojure-europe (60)
- # clojure-italy (2)
- # clojure-nl (4)
- # clojure-spec (21)
- # clojure-uk (4)
- # clojurescript (162)
- # cursive (30)
- # datomic (3)
- # editors (5)
- # emacs (4)
- # figwheel-main (1)
- # fulcro (24)
- # gratitude (3)
- # helix (7)
- # honeysql (20)
- # improve-getting-started (1)
- # introduce-yourself (11)
- # jobs (3)
- # joker (2)
- # kaocha (15)
- # lsp (21)
- # lumo (2)
- # meander (3)
- # off-topic (34)
- # re-frame (6)
- # reagent (1)
- # releases (4)
- # rum (2)
- # shadow-cljs (37)
- # spacemacs (16)
- # tools-deps (16)
- # vim (23)
- # xtdb (32)
It returns an error: Execution error (IOException) at sun.tools.attach.HotSpotVirtualMachine/ (HotSpotVirtualMachine.java:75). Can not attach to current VM
did you add the flags at https://github.com/clojure-goes-fast/clj-memory-meter#usage
I've added in another project.clj! Sorry and thank you 🙂
How can I replace a plus sign via regex? I am getting a weird error when I try to do #"+"
try this #"\+"
or (re-pattern "+")
Amazing that worked 😄 thanks
+ means "1 or more" in regex so it's a character with semantics and needs escaping
(defprotocol Dog
(bark [dog] [dog x] "dog barks")
(eat [dog] "dog eats"))
(defrecord Pitbull [name]
Dog
(bark [dog] (println (str name "goes woof woof")))
(bark [dog n] (repeatedly n #(bark dog)))
(eat [dog] (println (str name "eats and grunts"))))
(defn calling-function [x]
(eat (Pitbull. "Pete")))
only when you explicitly call bark
No implementation of method: :bark of protocol: #'com.clojure.core/Dog found for class: java.lang.String
you didn't pass it a dog instance
(bark (->Pitbull "Pete"))
(it is better to use the constructor function than the Java interop to create a record instance)
->Pitbull
not Pitbull.
I am trying to call bark [dog] [dog x] "dog barks")
which is in defprotocol , when it will print "dog barks" ?
when you call it with a dog
(bark (->Pitbull "Pete"))
this on i got, I wanted to call, (bark [dog] [dog x] "dog barks")
which is in defprotocol, how can I call directly
I feel like I'm saying the same thing over and over, but when you call it with a dog
(bark [dog] [dog x] "dog barks")
is not a function
it's a method declaration. "dog barks" here is a docstring
sorry if I was misunderstanding the question
If you are asking how to have a "default" implementation of stuff you can reference - somewhat like inheritance - then the simplest answer is to make your default stuff its own functions
if you have a dispatch mechanism that isn't just the type of a thing (you choose bark because its a pitbull) then a multimethod is more appropriate than a protocol
but truthfully its best to start out just only using plain functions with defn
until you have a concrete use for doing dynamic dispatch like this
most uses of records and protocols (with some caveats) would be served just as well with regular functions and regular, untagged, maps
You can also extend a protocol to java.lang.Object and that'll act as a default
How to check the sampler?
Especially to target one function that is currently ran?
Of Heap Histogram or Per thread allocations? And they're constantly changing in seconds
ah i was assuming you were looking to catch a memory leak. yeah i don't think this will help
those measures might be inaccurate because of GC
@kishorekaranam99 what is your end goal?
Find the amount of memory consumed by a function which is fetching some data.
during computation or after it returns?
and what is the reason for that? optimisation?
Yes. To see if the function takes more/less memory based on the stuff it does. Ex. It fetches the records and converts them to the camel and etc. So, want to know if removing these stuff changes anything or not by comparing the memory consumed.
not sure if I follow. The function suppose to do what it suppose to do. If you remove something from it — outcome might bring some unexpected side effects like broken program’s logic
It fetches the record and converts that data into a camel case and adds some other content to it. Can edit this to only fetch the records and nothing else. But first have to see if there is any change in doing so.
and what about outer code which is using that function? is it expected to not have camel cased data?
if yes - then just remove that conversion
but I’m guessing right now. Could you share the code of the function?
How much does it make a difference?
Day 3 and I'm struggling to understand how to reference libraries in Clojure. I was able to use pdfboxing in a project without any issues after including it in the dependencies. As well as extracting text from a PDF, I need to save a page of the PDF as an image. (The alternative would be adding images and text to an existing PDF file - creating a new one isn't an option - and I can't find a Clojure library that will allow me to do that.) Thinking I would try pdf-to-image (https://github.com/pablocastromontero/pdf-to-image), which wraps PDFBox, I included it in the dependencies and ran lein deps. It now shows up in the repository, but this ns form:
(ns pdf-test.core
(:require [pdfboxing.text :as text])
(:require [pdf-to-image])
(:gen-class))
gives a "Could not locate pdf_to_image__init.class, pdf_to_image.clj or pdf_to_image.cljc on classpath. Please check that namespaces with dashes use underscores in the Clojure file name." Looking at the repository the path and the file names use dashes rather than underscores, e.g. "C:\Users\dan\.m2\repository\pdf-to-image". Do I have to manually change these to use underscores? Or do I have to qualify the imported function in some way e.g. 'pdf-to-image.convert', or is it something else altogether?@clojure529 by the way "lein deps" is only needed in exceptional circumstances - every lein task (repl, run, compile, check, test ...) will run it implicitly as a subtask. "installing a dep" is how you get libraries in other ecosystems but that's not how lein works really . You declare a dep and on running a task the deps are either found in cache or retrieved and cached.
I mention these things because lein becomes a lot less frustrating and more useful when you internalize this model
also you don't need multiple :require
forms. you can just do (:require [pdfboxing.text :as text] [pdf-to-image.core :as image])
(i'd use newlines as well but just doing inline here for brevity
https://purelyfunctional.tv/article/100-most-used-clojure-expressions/ I'm looking to run a similar analysis to determine most frequently called functions in a codebase. Does anyone know how I could accomplish this? Is there a library or something that would make it easy?
Thanks @dpsutton and @noisesmith for those tips. So the part after the dot is a reference to the actual module in the original? That would make sense. The namespace now works, but I get:
Syntax error (ClassNotFoundException) compiling at (pdf_to_image\core.clj:3:1).org.apache.pdfbox.util.PDFImageWriter
Investigating, it seems that there have been https://pdfbox.apache.org/2.0/migration.html in PDFBox since this lib was written and PDFImageWriter has been removed. I will take a look at PDFRenderer class that replaces it, but given my lack of Java that might be a bridge too far at this stage. Ostensibly back to square one, but I learned a couple of things.@clojure529 it should be possible to access the version of PDFBox that pdf-to-image needs by declaring the right deps in your project.clj
> the part after the dot is a reference to the actual module in the original?
modules are something completely different (and not relevant here). require
asks for a namespace to be loaded if it was not loaded already, the .
is part of the namespace (when looking up namespaces, foo.bar.baz is looked for in the directory foo/bar/ in the file baz.clj)
for example, it's valid to have both a.b.c
and a.f.c
- both end in c, but they are not otherwise related, and the full name includes the prefix
or as a more concrete and common example, many libs have namespaces with a .core
suffix, and foo.core
and bar.core
are unrelated, they likely come from totally different libraries
I have seen the code (server/run-server #'app-routes {:port port})
while practising rest api in clojure, Why do we need to use #' to app-routes
? As per my reading these var
is used for those symbol which will change in the future, correct me if I am wrong!
that's correct - it gives you a point of indirection to support reloading while running the app
@U01J3DB39R6 See https://clojure.org/guides/repl/enhancing_your_repl_workflow#writing-repl-friendly-programs
will take a look @U04V70XH6, Thank you
invoking a var derefs the var and invokes the function it refers to
From https://www.youtube.com/watch?v=gIoadGfm5T8 this (require '[clojure.tools.deps.alpha.repl :refer [add-libs]])
no longer works as repl
is not there anymore. What's the new way to use add-libs
? Does this work https://github.com/lambdaisland/classpath?
Besides what Alex said, see also https://github.com/seancorfield/dot-clojure/blob/b564127173dd8bf3790224ece94ab4b27c466252/deps.edn#L161-L183
@UP82LQR9N In particular, pay attention to all those caveats about breaking/changing/alpha/work-in-progress. It's a case of "here be dragons" and not something I'd recommend to folks who just want a simple, reliable workflow.
the latest tools.deps.alpha branch is add-lib3
(btw, #tools-deps is probably better for this specific question in the future)
latest commit there is 83a477b305839c697666401508c395cfed29506a
should really probably be moved into its own repo as its an additive thing
In a reframe app, if I have some mock data with components (e.g. [mention "<mailto:[email protected]|[email protected]>"], and I prevent resolving the symbol in the mock data with ', how do I get a subsequent 'get' to resolve it? Presently, its just plopping <mention><mailto:[email protected]|[email protected]></mention> into the html but its never resolving to the component
I've started a fighweel.main repl from CIDER in Emacs on Windows, is there a ready option or template, or recommended workflow to make a production build for deploy?
if you are using lein
the standard thing is to use lein uberjar
to create a deployable artifact, I'm confused by your question though because I'm not sure how that relates to your REPL
I'm trying to learn to create a ClojureScript web application, I could start a figwheel.main browser REPL, but I'm not sure about the workflow to create the optimized version for deploy
OK, then see above, if your template was set up properly lein ubjarjar
should set you up
that gives you *something*-standalone.jar
which you can then run via java
I think I'm looking for a way to run this from my environment (docs example): "clojure -m figwheel.main -O advanced -bo cruel", probably I should set up or find a build configuration to refer from the IDE ...
OK so you aren't using lein, in that case you need to check your deps.edn to see if a packaging task is set up, if not I recommend depstar https://github.com/seancorfield/depstar
Add this to project.clj
:aliases {"fig" ["trampoline" "run" "-m" "figwheel.main" "-b" "dev" "-r"]
"bo" ["trampoline" "run" "-m" "figwheel.main" "-O" "advanced" "-bo" "dev"]}
then you can lein bo
to compile
@UP82LQR9N they use clojure
to run the app, they are not using lein
I use lein, the clojure line was an example from http://figwheel.org
clj is similar. add to :aliases
:bo {:main-opts ["-m" "figwheel.main" "-O" "advanced" "-bo" "dev"]}
then you can do clj -M:bo
@ULD86RW5N than once agian, if you are using lein all you need is lein uberjar
, that makes a jar, use java to run the jar
that's literally the whole thing
if your project was set up properly then it will compile to javascript as part of the packaging
Ok, it says "Created C:\...\hello-world.core\target\hello-world.core-0.1.0-SNAPSHOT-standalone.jar"
the jar is an archive, that should run a server which servers your app to a browser
@UP82LQR9N Yes there is an alias called "fig:min" in project.clj, so that would perhaps be lein fig:min to create the build from the command line
or you can use the js files from the archive in another context
Ok, so the Cljs app is packaged inside the jar? I did not intend to deploy a Java web app but that could be good to know about
the cljs app is comprised of the js files plus the html template plus server side code (if any) - if all you need is the html and the js files that's all there, but how to use that is very much dependent on how you plan to run the app, it doesn't have a generic solution as far as I know
I was a little confused about the ":profiles" concept, there is only a ":dev" profile present in the project.clj, but maybe that is relevant for the release build also.
the project.clj is merged over all the built in default configs, each lein task has a profile (repl, compile, jar, uberjar, test ...) and lein plugins will usually add one or more profiles as well
cljs/figwheel can give you one compiled/optimized .js file you also need other html/css files. if your web server is written in .clj then it makes sense to package everything into one .jar file
otherwise html/css/js files are just files you need to deploy to your web server specific folders
Thanks. Yes that is the next step to transfer the page and scripts to the remote server.
So figwheel is a plugin maybe, and the :aliases are tasks? The :dev profile declares a dependency of figwheel-main, is that profile merged always or when running certain tasks.
the :dev
profile is merged for most tasks, other than compile
or run
lein help sample
shows an annotated project.clj with info about what each part is for / how to use it
lein help profiles
also has a lot of content
figwheel is the cljs->js app written in clojure, succeed by figwheel-main, and lein-figwheel is the lein plugin which enhances lein support for figwheel.
Ok, so the :aliases are shorthands for lein arguments to run the fighweel.main app then, and the app is an extension of the regular cljs compiler. What is the :dev :dependencies [[com.bhauman/figwheel-main "0.2.12"]] for?
because you shouldn't need any figwheel code during your app runtime - it's a dev tool
and that profile ensures you can use it in dev tasks but it doesn't get packaged
well, that's what profiles are, and the :dev
profile is the one for dev specific tasks
but yes, precisely
There are two jar files in the target directory, one named "-standalone" which is larger.
right - the standalone contains all the recursive deps needed to run the jvm clojure code
they are both zip files (jar is a type of zip), you can use a zip tool to look inside (or even an editor if your editor is any good)
Yes I think I found the compiled javascript in public/cljs-out/dev-main.js, there is also a "dev" folder there with a lot of dev-related files
So it looks like a dev build inside the jar, maybe there is not a release build configuration defined or/and I should run some kind of cleanup before.
@ULD86RW5N https://github.com/bhauman/figwheel-main-template is the template I use to set up a figwheel-main project. The template includes a dev and min build profile. The min profile creates a single javascript file using advanced compilation and minification. I typically add a live profile that is similar to min which creates the JavaScript file in the docs directory. I publish the resources files and the JavaScript file on GitHub pages, so do not need to create an uberjar (as there is no server-side component). https://practical.li/clojurescript/web-design-basics/clojurebridge-london-website/github-pages-deploy.html
I have started using a GitHub action to deploy the artifacts to GitHub pages, although I haven't documented this yet https://github.com/practicalli/practicalli.github.io/blob/live/.github/workflows/deploy.yml
Thank you, that sounds like the scenario I had in mind currently, publishing a simple web app to a static page server. I'll look more into the figwheel-main-temple and also the GitHub automations feature sounds interesting. I'm still confused about the profile semantics and evaluation but maybe it will get clearer.
In the usage example for figwheel-main template the project folder is named hello-world.core? What does the .core suffix in the project name tell?
lein new figwheel-main hello-world.core -- +npm-bundle --reagent # or --rum, --react or nothing
core is an arbitrary name, it's a default when you don't give a multi part namespace to lein on project creation
that is, if you do lein new some-template foo
you get src/foo/core.clj containing foo.core, if you do lein new some-template foo.bar
you get src/foo/bar.clj with foo.bar
this is because single segment namespaces are a bad habit (and also cause weird bugs)
why would you want foo.core? it's a default that's there because it's generic, it was picked because it doesn't carry meaning
It was just because the example said that on https://github.com/bhauman/figwheel-main-template
right - that's for doc / walkthrough purposes. saying hello-world.core is the same as saying hello-world as far as lein is concerned, but it might help the reader know that something other than core could be picked
Leiningen project typically used project-name.core as the default namespace, possibly as a tribute to clojure.core namespace (and some Lisp tradition). It not essential to change it, but it's encouraged to have meaningful names, e.g landing-page or website, etc. This is the GitHub repository I use for the Practicalli landing page https://github.com/practicalli/practicalli.github.io
Could you select the min build in Spacemacs/cider to create a release version from the IDE
I can run lein fig:build and fig:test, but the fig:min gives a Compile Exception Illegal char <:> at index 2 ...
. lein uberjar seems to work. Could be a path name problem?
Ok, I had to delete resources\public\cljs-out\ folder manually before doing the fig:min build
So the development build and the production build are mixed into the same output folder, and also into to the uberjar
Ok, it actually works to load the page from a plain web server. The cljs-out/dev folder with 2.5 MB worth of script files was a bit frightening but it does not seem to be used by the dev-main.js script.
The large number of files created by the dev build allows figwheel-main to selectively load in just the parts that change. I haven't needed to delete these files, as the min build creates a self-contained file. In the Practicalli project repository, I created a live build, based on min, that outputs the file to a different location
The readme explains how to generate the builds from the command line. I only run the dev build in the editor, other builds are on the command line
I see. The min build produced the cljs-out/dev folder also, I don't know what the content is used for in that case. To output the production build to a separate location would feel more tidy and make it easier to understand what to deploy.
Any ideas on what would be causing this after moving a lein project to deps.edn.
Exception in thread "main" Syntax error compiling . at (aleph/http/core.clj:251:3).
Found an old thread on this from last year and the person fixed by using clojure:openjdk-8-tools-deps-1.10.1.502
I’ve tried to switch java versions but that hasn’t resolved it. Any thoughts?
I found a similar error reported (by searching the Zulip archive) and the "solution" was "I believe this is on Aleph’s side and I can resolve by excluding aleph and just specifying the latest aleph 0.4.6 dep on my own. So it’s like it’s just an issue with the version of Aleph that Yada depends on. The other thing that seems to resolve it is just using the alpha version of Yada. Would the preferred solution be to just use alpha?" (it was in the context of Yada).
So I think I would ask: What version of Aleph are you using? What version of JDK are you using? What version of Clojure itself and what version of the CLI?
Also: are you using a library that depends on Aleph or are you using Aleph itself directly?
Hi @U04V70XH6, I’m going to play with this and see how I can exclude aleph (using it through luminus-aleph
).
I remember looking into this for someone else who had the exact same error (I have never used Aleph myself) but unfortunately I don't remember what the solution was...
That’s using the solution I saw you posted: clojure -M -e ‘(println “Java” (System/getProperty “java.version”) “Clojure” (clojure-version))’
The stacktrace from that error might provide more insight as to what is behind the error -- look for a "Caused by" line further down.
Doing this fixed the error
{aleph/aleph {:mvn/version "0.4.6"}
luminus-aleph {:mvn/version "0.1.6" :exclusions [aleph/aleph]}
in general there's no guarantee it can be resolved, but you can start with the resolve
function
user=> (resolve '+)
#'clojure.core/+
there's also requiring-resolve
if you want the compiler to look up and require the namespace too
e.g. :foo ['who " kicks the ball"], then sort of hydrate them with variables in context of a log message
oh i see. you are using it the same way. can you write out a form with how you would use it? a bit more concrete of an example?
resolve and friends only work with vars, which are only the top level global names created with def
names bound via let, or via function application are "locals" and don't have any kind of first class queryable representation
(def messges
{:login ['who " has logged in at " 'time]
:logout ['who " has logged in at " 'time]
:changed-password ['who " has logged in at " 'time]
:app-restart ["The application has restarted at " 'time]
})
(def log-message {:who "ryan" :type :login :time #inst "2020-02-02T02:02:32Z"})
(defn humanize-log-message [log-mesage]
(get messages (get log-message :type)))
That's the jist of what I'm shooting forso what you actually want is templating
I'd break this into two tasks, the first one (use :type
to pick a template) you basically have already
the second part, using the keys of the data to fill in that template, can be done via simple walk/replace code in your case, or you could pull in a logging lib like selmer if you think you'll want full featured templating
(apply str (map #(get log-message % %) (get messages (:type log-message))))
"ryan has logged in at Sat Feb 01 18:02:32 PST 2020"
I considered walk, but there's a guarantee of no depth here
I changed your definitions slightly to make that work:
(def messages
{:login [:who " has logged in at " :time]
:logout [:who " has logged in at " :time]
:changed-password [:who " has logged in at " :time]
:app-restart ["The application has restarted at " :time]})
of course symbol / keyword conversion is also trivial
@rdonahue since this is #beginners I'll point out that the key thing that makes it work (get coll key default)
works since only things you want substituted will be keys in that map
and in this case of course both the key and default are the same value
Ok, sooooo if I wanted to do one little thing extra, say make the inst's a little more compact, is my best bet to transform the map on the way in to that function? Maybe with specter, which I'm already using for nested map traversal?
the inst is a data type that has its own dedicated formatter
you can replace the current replacement function (the one using get
) to a more complex thing that also does conversions based on data type
beginner question, how do people get a value from a map. currently i always do (get map :key)
. is there an “idiomatic” way?
both (map :key)
and (:key map)
work
of course naming anything map
is pathological
for context in this code `
(not (= "labeled" (get params :action))
that’s how i do it all the time. is there a better way(not= "labeled" (:action params))
oh so interchangeably works
well - this is specific to keywords / symbols but yes
ok i see. that’s simpler
hash maps invoke get when used as functions, as do keywords and symbols
so (params :action)
would also work, but if one of the two is a data literal, I prefer to put that one in the calling position
but ("foo" {"foo" "bar"})
would blow up - strings are not callable
oooohh got it
but if one of the two is a data literal, I prefer to put that one in the calling position <-- can you pls give an example?
({:a 0} foo); returns 0 if foo is :a, otherwise nil
(:a foo) ; attempts to look up :a
that way you don't get a dumb "can't be cast to IFn" error if foo isn't an expected datatype
so {:a 0}
is the data literal in ({:a 0} foo)
right? sorry new to programming
right
and if you do this ({:a 0} foo)
it will return nil
because there is no foo
key in the dictionary right?
well who knows what foo is
sorry, I shouldn't have specified "data literal" but the more general "if the binding is obvious and visible"
it’s fine 🙂
so foo
here stands for some value that gets passed in, or could change, or otherwise isn' t immediately resolved and visible
so maybe it's a function arg or a ring map parameter we destructured or whatever
oh ok so foo
in that code is a placeholder
right - the convention is that foo, bar, baz, quux are used as placeholders where the actual named thing is not the important aspect
"metasyntactic variables"
ah yeah get it now, cause of course you have to pass in a key e.g. :foo
and not foo
ok thanks @noisesmith big help