How to see the clojure program output in console in Eclipse - clojure

When running a clojure core.clj as Clojure Application will the following method gets executed and "Test" gets printed?
(defn -main
[& args]
(println "Test")

I know a few of my friends who use Counterclockwise plugin on Eclipse.
Here is a link to their docs. It also has a step by step way to Create your first project

Related

How can I make emacs compile my c++ code and run it in a new window?

Is there a way I can make emacs compile and run my code in an external console window with the command M-x compile (what I mean by external console window is that I want it to run my code in a new console window like it does when I run my C++ code in visual studio).
I want emacs to automatically open up a new console window and run the executable after M-x compile like this:
Normally with Emacs, when you want some obscure (i.e. some simple, or special to your use case) automation, you just write a command, which does what you want.
Here is such an example. Following Elisp code calls a function and-run, when compilation is finished (through using hook compilation-finish-functions).
(defun and-run (&rest _)
"run after comilation, an not elaborated example function"
(interactive)
(let ((exe "./test.out")
(prevent-closing (concat "echo \"Press Enter to continue\"" ";"
"read"))
(terminal "xterm -e"))
(when (file-exists-p exe)
(let ((my-command (concat exe ";" prevent-closing)))
(shell-command (format "%s %s" terminal (shell-quote-argument my-command)))))))
(add-hook 'compilation-finish-functions #'and-run)
Since I'm no Windows user you have to make some small adaptions to this function, in order to be able to run it on Windows.
Note: there is also a compilation-start-hook, if you e.g. want to compare file-modification times of your exe.

Different slurp behavior with "lein run" and running the uberjar file

I am currently trying to learn Clojure and as part of my practical training, I am
implementing the very basic behavior of some of the well known Unix tools like grep,
cat, ls and so on.
While implementing cat, I stumbled upon some seemingly strange behavior of
slurp. When I run the following code with lein run some-file.txt while some-file.txt
lies within the current directory, the content is printed to STDOUT as expected.
(ns cat.core
(:gen-class))
(defn -main
"Reads the content of its arguments representing filenames and outputs the
content in succession."
[& filenames]
(doseq [filename filenames]
(println "Reading" filename) ; Just for debugging purposes
(print (slurp filename))))
However, if I uberjar the project with
lein compile
lein uberjar
and then cd to target/uberjar to run the standalone JAR with
java -jar cat-0.1.0-SNAPSHOT-standalone.jar some-file.txt (with the text file present in that directory),
nothing but my debug message gets printed. What I find so strange is that there is no error
message being shown, so it seems to me that the file can be found. If I run the
JAR with a file that does not exist as parameter, I get an exeption that the specified
file cannot be found (as expected).
Because I am working on a Windows machine, a colleague suggested that perhaps Windows
shadow files might be a problem. So I tested the program under Linux again and the
same behavior occured. So this seems to be a "problem" with my Clojure understanding / my project
settings.
My question is: Why is slurp's (or the program's) behavior different when running
with lein run and when running the standalone JAR with java -jar cat-0.1.0-SNAPSHOT-standalone.jar and what can I do to solve this?
As the file parameter is not a resource that is compiled into the JAR, there is no
need for (slurp (clojure.java.io/resource filename)) if I am not mistaken.
print does not flush the output buffer. You need to use flush after your print statement, or use another println, which flushes on newline:
(defn -main
"Reads the content of its arguments representing filenames and outputs the
content in succession."
[& filenames]
(doseq [filename filenames]
(print (slurp filename))
(flush)))

conditional compilation for test / deploy build with raspberry pi

I am building an application for the raspberry pi and use pi4j as a dependency for Software PWM on GPIO. I'd like to test my code on my local machine though, so I would like to compile my code without the pi4j dependency and skip method calls to the library.
Example code:
(ns led-server.model
(:require [clojure.tools.logging :as log])
(:import [com.pi4j.wiringpi SoftPwm Gpio])) ;; pi4j dependency, only compiles on rPi
(defn- soft-pwm-write [pin value]
(let [ival (Math/round (double (* value RANGE)))]
(SoftPwm/softPwmWrite pin ival) ;; call to pi4j. This is what I want to skip
(log/info "pin" pin "set to" ival))
)
pi4j requires the wiringPi C library, which is only available on the raspberry pi (which makes sense). For testing on my dev machine it would be sufficient to see the log printout. I don't want to comment out the :import and method calls for testing, I would like a more elegant solution.
Apart from the question if conditional compilation is the best approach here, it is not difficult to compile files conditionally with leiningen: put the files you want to compile conditionally in a folder different than src/clj, and define it as a source folder in a profile:
:profiles {
:native {:source-paths ["src/native/clj/"]}
:mock {:source-paths ["src/mock/clj/"]}
Then, run leininig with profile:
lein with-profiles +mock repl
See more at https://github.com/technomancy/leiningen/blob/master/doc/PROFILES.md
In your situation, you could define a protocol, provide 2 implementations and make sure you load only the classes relevant to your environment.

When and how many times can a clojure file be evaluated with leiningen?

I found while adding warnings to code that writes macros that the body of a file was being executed twice during compilation. Is there a reason for this? Is this specific to leiningen? I can not reproduce this with (compile ...).
Simplified version:
(ns foo.core
(:require foo.bar))
;; empty
(ns foo.bar)
(println "hello")
$ lein compile :all
Compiling foo.core
hello
Compiling foo.bar
hello
Further testing shows that the namespace is reloaded on top of itself during compile:
(ns foo.bar)
(declare wasd)
(println wasd)
(def wasd 2)
$ lein clean
$ lein compile :all
Compiling foo.core
#<Unbound Unbound: #'foo.bar/wasd>
Compiling foo.bar
2
In a more complicated case I have this happening during compile, and then once every time when run or started a repl from lein. I am not sure why. This is all with clojure 1.6 and leiningen 2.5.0.
Leiningen knows nothing about the structure of your project in terms of how namespaces relate to each-other. Consequently, when compiling a project, lein simply boots a JVM and forces each namespace to be loaded one at a time. This means that, as you noticed, namespaces will be reloaded causing the double evaluation behaviour you are noticing.
In comparison, (clojure.core/compile) simply loads the targeted resource with clojure.core/*compile-files* bound. This will cause the targeted resource and all the resources it requires to be loaded and compiled to class-files. However, it will not traverse your entire project structure compiling all resources as Leiningen's compile operation does.
The reason why you're seeing println output during compile time is because println is called during namespace evaluation. You should have a -main fn or some other entry point for your program that calls on println.
(defn -main [& _]
(println "Won't see on compile.")
(println "lein run -- is printing from -main"))
I think your project is throwing an Unbound Exception because you're trying to reference the wasd variable with println before a value is assigned to it. The variable was declared, but nothing was assigned to it before the println fn tried to get the value.

The auto-complete-clang-async doesn't work on Emacs as expected

While referring to this webpage, I installed auto-complete-clang-async.el to Emacs.
Apparently, auto-completion works when I create a new file.(shown below)
But it doesn't work when I saved the file and open it again on emacs. Does anyone know why it happens and how it should be fixed?
Any help would be appreciated. Thanks in advance.
I use Emacs24.3.1 on Ubuntu12.04, which is accessed from Windows 8.1 machine via ssh.
clang's version is 3.5. clang-complete binary is build with llvm-config-3.5.
Here is my emacs configuration in .emacs.d/init.el.
(defun add-to-load-path (&rest paths)
(let (path)
(dolist (path paths paths)
(let ((default-directory
(expand-file-name (concat user-emacs-directory path))))
(add-to-list 'load-path default-directory)
(if (fboundp 'normal-top-level-add-subdirs-to-load-path)
(normal-top-level-add-subdirs-to-load-path))))))
(add-to-load-path "elisp" "conf" "public_repos" "elpa" )
(require 'auto-complete-clang-async)
(defun ac-cc-mode-setup ()
(setq ac-clang-complete-executable "~/.emacs.d/clang-complete")
(setq ac-sources '(ac-source-clang-async))
(ac-clang-launch-completion-process)
)
(defun my-ac-config ()
(add-hook 'c-mode-common-hook 'ac-cc-mode-setup)
(add-hook 'auto-complete-mode-hook 'ac-common-setup)
(global-auto-complete-mode t))
(my-ac-config)
I put the .el file in .emacs.d/elisp directory.
There is clang-complete executable in .emacs.d/ directory.
Even though I don't understand why it happens, I've discovered the solution.
When auto-completion doesn't work properly, you have to save your current buffer. (Command: C-x C-s)
Then the functionality will be effective again.