In Clojure, what is the best way to determine the size of a map? Import java library "java.lang.instrument.Instrumentation" and use .getObjectSize. However, the library cannot be imported. Please comment how to import the library correctly.
; Load the instrumentation namespace to get access to its vars
(require '[java.lang.instrument.Instrumentation :as inst])
; Find the size of an object
(println "The size of a map is"
(inst/getObjectSize {:ts 20200101170000219 :bid 108.730000 :ask 108.786000})
"bytes")
I have not used that Java library before, but note that many collections in Clojure are implemented using many Java objects, i.e. typically a "root" object that points to many other objects. If you want to know the size of all of these, and/or create drawings showing their structure, I have created a library called cljol that can do this: https://github.com/jafingerhut/cljol
The README shows examples of use and the output you can expect. There is also a link in the top level README to a gallery of other images created using the library, for a few Clojure data structures.
Related
I like that boot lets me put all my source files in the current directory. However, I would still like to develop code that refers to a parent namespace, to make it easier to import into other projects. For example, I'd like to make all my namespaces start with the same prefix, but still have their source files reside in the current directory, like this:
(ns polysyndeton.conjunctions ...) ; in ./conjunctions.clj
(ns polysyndeton.disjunctions ...) ; in ./disjunctions.clj
(ns polysyndeton.util ...) ; in ./util.clj
How can I tell boot or Clojure that any namespace starting with polysyndeton. should be found in the current directory?
I don't think it possible (without workarounds that are not recommended). Clojure uses the directory structure corresponding to the namespace segments to find source files on the classpath. So you must adhere to this structure.
Also see: What are common conventions for using namespaces in Clojure?
You can achieve this easily with a symlink. Note that this will map ANY strings like polysyndeton.polysyndeton.polysyndeton.{...}.util to the file util.clj:
ln -s . polysyndeton
I have just begun playing with ClojureScript and I'd like to collect all CSS files into a single folder (out/css). I found leiningen-less and with the following config I get the compiled CSS files into the correct location:
:less {:source-paths ["src/less"]
:target-path "out/css"}
I can't find any documentation on how I can handle the ordinary CSS files (e.g. the file for resetting defaults, css/reset.css). Basically I want the equivalent of cp css/*css out/css.
I did find lein-resource but it does a bit more than I require (pass things through stencil) and more importantly it through an UnsupportedOperationException on my with what I thought would a be a valid configuration:
:resource {:resource-paths ["css" {:target-path "out/css"}]}
Please englighten me!
For your particular use case just rename reset.css to reset.less. less should be able to read CSS without problems.
For more advanced frontend tooling maybe consider adding something like make/grunt/etc. More complexity but more power & flexibility.
I think better and easy solution would be that you write a function that uses clojure.java.io library functions and integrate them with lein-less "compiler" fork, so this is my internal function proposal:
(defn your-fn[]
(remove-folder "./out") ;; => you have to do how to make that fn following io lib doc
(copy-folder "./css ./out") ;; ;; => you have to do how to make that fn following io lib doc
(run-compiler :javascript
{:project-root "your-project-root/"
:source-paths ["less"]
:target-path "out"})))
PS: note that you need to call this fn from your clojurescript compilation process, BTW I didn't know if there is a way for that :)
I need to create Table structure and fill data using Haru library in C++. Can anyone provide me sample example which create table structure using Haru library.
I while ago I needed to produce some data tables as part of another project using Haru PDF. To simplify the creation of complex tables I wrote a small utility module to be used with the Haru PDF library that greatly simplifies the creation of complex tables. The module allows full customization of the table and supports for example cell spanning (both row and column). It also allows for the separation of layout and look&fell by a theme concept.
A simple usage example (just to give an idea) would be
int num_rows=5;
int num_cols=4;
char *table_title="Example 1: Basic table with default theme";
hpdf_table_t t = hpdf_table_create(num_rows,num_cols,table_title);
hpdf_table_set_content(t,content);
hpdf_table_set_labels(t,labels);
HPDF_REAL xpos=100;
HPDF_REAL ypos=630;
HPDF_REAL width=400;
HPDF_REAL height=0; // Calculate height automatically
hpdf_table_stroke(pdf_doc,pdf_page,t,xpos,ypos,width,height);
It is out of scope for this answer to discuss the code in more details but it should be fairly self documenting.
The module also allows both purely programmatic tables but also the creation of entirely data driven table creation (all layout and look & feel is taken from a structure). This allows a light-way model-view-controller approach to make maintenance easier. To fully use this relies on the client implementing callback functions that the module will call to get the corresponding data.
Since this was never intended to be released as a separate utility I haven't written up (yet) full documentation but I put together a quick standalone example which shows some of the features. The resulting PDF from running the example is included at github. However, all public API are fully Doxygen commented which should give some ideas on how it fits together.
You can find the module at (https://github.com/johan162/hpdf_table)
Update: Due to surprisingly many visits/questions I have taken the time to make a new release (1.4.0) with a completely rewritten documentation and reference which explains all the functionality of the library. The release also fixes all known small bugs as well as several new features.
You can use the draw_graph function in the encoding_list.c demo as an example. It is part of the source tar ball.
Say I wanted to factor out some common code between my client-side *.cljs and my server-side *.clj, e.g. various data structures and common operations, can I do that ? Does it make sense to do it ?
I wrote the cljx Leiningen plugin specifically to handle Clojure/ClojureScript code sharing for a Clojure data visualization library.
95% of non-host-interop code looks the same, and cljx lets you automatically rewrite that last 5% by specifying rewrite rules using core.logic.
Most of the time, though, it's simple symbol substitutions; clojure.lang.IFn in Clojure is just IFn in ClojureScript, for instance.
You can also use metadata to annotate forms to be included or excluded when code is generated for a specific platform.
Update: as of clojure 1.7, check out Clojure reader conditionals or cljc. I've used cljc with great success to share a lot of code between server and browser very easily.
Great question! I've been thinking a lot about this as well lately and have written a few apps to experiment.
Here's my list of what types things you might want to share and pros/cons of each:
Most of my client cljs files contains code that manipulates the dom. So, it wouldn't make sense to share any of that with server
Most of the server side stuff deals with filesystem and database calls. I suppose you might want to call the database from the client (especially if you're using one of the no-sql db's that support javascript calls). But, even then, I feel like you should choose to either call db from client or call db from server and, therefore, it doesn't make much sense to share the db code either.
One area where sharing is definitely valuable is being able to share and pass clojure data structures (nested combinations of lists, vectors, sets, etc) between client and server. No need to convert to json (or xml) and back. For example, being able to pass hiccup-style representations of the dom back and forth is very convenient. In gwt, I've used gilead to share models between client and server. But, in clojure, you can simply pass data structures around, so there's really no need to share class definitions like in gwt.
One area that I feel I need to experiment more is sharing state between client and server. In my mind there are a few strategies: store state on client (single page ajax type applications) or store state on server (like legacy jsp apps) or a combo of both. Perhaps the code responsible for updating state (the atoms, refs, agents or whatever) could be shared and then state could be passed back and forth over request and response to keep the two tiers in synch? So far, simply writing server using REST best practices and then having state stored on client seems to work pretty well. But I could see how there might be benefits to sharing state between client and server.
I haven't needed to share Constants and/or Properties yet, but this might be something that would be good to reuse. If you put all your app's global constants in a clj file and then wrote a script to copy it over to cljs whenever you compiled the clojurescript, that should work fine, and might save a bit of duplication of code.
Hope these thoughts are useful, I'm very interested in what others have found so far!
The new lein-cljsbuild plugin for Leiningen has built-in support for sharing pure Clojure code.
Wrote a quick bit of code to copy a subset of my server clojure code over to my clojurescript code, renaming as .cljs before building:
(ns clj-cljs.build
(use
[clojure.java.io]
)
(require
[cljs.closure :as cljsc]
)
)
(defn list-files [path]
(.listFiles (as-file path))
)
(defn copy-file* [from to]
;(println " coping " from " to " to)
(make-parents to)
(copy from to)
)
(defn rename [to-path common-path f]
(str to-path common-path (.replaceAll (.getName f) ".clj" ".cljs"))
)
(defn clj-cljs* [files common-path to-path]
(doseq [i (filter #(.endsWith (.getName %) ".clj") files)]
(copy-file* i (file (rename to-path common-path i)))
)
(doseq [i (filter #(.isDirectory %) files)]
(clj-cljs* (list-files i) (str common-path (.getName i) "/") to-path)
)
)
(defn build [{:keys [common-path clj-path cljs-path js-path module-name]}]
(clj-cljs* (list-files (str clj-path common-path)) common-path cljs-path)
(cljsc/build
cljs-path
{
:output-dir js-path
:output-to (str js-path module-name ".js")
}
)
)
(defn build-default []
(build
{
:clj-path "/home/user/projects/example/code/src/main/clojure/"
:cljs-path "/home/user/projects/example/code/src/main/cljs/"
:js-path "/home/user/projects/example/code/public/js/cljs/"
:common-path "example/common/" ; the root of your common server-client code
:module-name "example"
}
)
)
This question predates cljc, but since I stumbled upon it, I thought I would mention Clojure reader conditionals.
I am trying to go through the process of creating a jar file from a simple clojure file. Below is my clojure code:
(ns app.first (:gen-class))
(refer 'clojure.core)
(defn -main [& args] (println "this program worked!"))
I am using these instructions to create the jar file: http://en.wikibooks.org/wiki/Clojure_Programming/Tutorials_and_Tips
I see the error "java.io.FileNotFoundException: Could not locate app/hello__init.class or app/hello.clj on classpath: (NO_SOURCE_FILE:0)" when I try to complete the (compile 'app.first) step.
The only difference between my attempt and the link is the name of my file (first.clj instead of hello.clj).
Can anyone see where I am going wrong? Or for that matter, all I want to do is learn how to create a jar from a clojure file, so if anyone knows of a better/easier way to do that, let me know.
It's better to use Leiningen for such tasks - it allows to maintain dependencies, and packs all necessary components into jar file
I'm rusty on this, but I heard about other people with similar problems.
I think it's helpful to remember that the classpath you indicate points to the root of your class tree, and package names end up creating subdirectories within that tree. Awkwardly stated, but I hope you get the idea. Thus, I think you need to do some kind of gymnastics with creating directories to match the "app.first" -> "/app/first" hierarchy.
Sorry, that's as close as I come to a sensible and useful answer. Hope this helps you.
EDIT:
The Prime Directive of Computer Science: It only works if you do everything right! I spent almost 10 minutes fiddling with this but was finally successful.
Here's what I needed to do to get your program to compile:
created a directory app, and within that, first.clj with your code.
checked for the *compile-path* by doing (pr *compile-path) within Clojure. It said "classes".
created a second directory classes parallel to app.
in the shell, did export CLASSPATH=.:./classes
in Clojure, did (compile 'app.first)
... and I found a bunch of class files in classes. JARring those should be a snap.
I found it very helpful to run (doc compile) because that reminded me of the requirement to have a directory to satisfy the requirement for a *compile-path*.