Macros in the Cider repl (e.g. ->) - emacs

why is the use of the threading macro ->> producing an error in the cider-repl:
(-> "x..")
// Unable to resolve symbol: -> in this context

this happens when you create a new namespace in the repl and have not yet defined anything in it. so the aliases for the clojure.core functions have not yet been established:
start by creating a new blank namespace:
user> (in-ns 'i-dont-exist-yet)
#namespace[i-dont-exist-yet]
Then try to use the threading macro (or anything from clojure.core):
i-dont-exist-yet> (-> 1)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: -> in this context, compiling:(*cider-repl api*:47:18)
which fails to lookup the symbol -> in the current namespace, though it works if you tell it which namespace to use explicitly:
i-dont-exist-yet> (clojure.core/-> 1)
1
There is a convenience function in clojure.core that will add all the expected refers for your new namespce. You won't need to do this when you create your namespace from a file with the ns macro at the top because ns does this for you (amongst other useful things):
i-dont-exist-yet> (clojure.core/refer-clojure)
nil
i-dont-exist-yet> (-> 1)
1

Related

PrimOps in GHCi

Is it possible to use PrimOps in GHCi? The following does not work:
$ ghci -XMagicHash
GHCi, version 8.10.1: https://www.haskell.org/ghc/ :? for help
Loaded GHCi configuration from /home/stefan/.ghci
λ> let x = 42.0# :: Float#
<interactive>:1:18: error:
Not in scope: type constructor or class ‘Float#’
Perhaps you meant ‘Float’ (imported from Prelude)
Importing Prelude manually does not solve this error.
Update: After importing GHC.Exts the following error shows up:
$ ghci -XMagicHash
GHCi, version 8.10.1: https://www.haskell.org/ghc/ :? for help
Loaded GHCi configuration from /home/stefan/.ghci
λ> import GHC.Exts
λ> let x = 42.0# :: Float#
<interactive>:1:1: error:
GHCi can't bind a variable of unlifted type: x :: Float#
The MagicHash extension just makes it legal to use # in names. If you want to specify types like Float#, you still need to import them. Do import GHC.Exts and try again.
Also, you can't bind them with let. You need to immediately use them and rewrap them.

LISP MAKE-PATHNAME: Illegal :DIRECTORY argument

I download Semantic Network Processor project:
http://digital.cs.usu.edu/~vkulyukin/vkweb/software/snp/snp.html
and following it's read me,
By using CLISP interpreter I change the directory to the folder,
and do the following:
[3]> (load "snp-loader.lisp")
;; Loading file snp-loader.lisp ...
;; Loaded file snp-loader.lisp
T
[4]> (in-package "USER")
<PACKAGE COMMON-LISP-USER>
[5]> (snp-load-everything)
**- MAKE-PATHNAME: Illegal :DIRECTORY argument "D:\\snp-stable\\"**
The following restarts are available:
ABORT :R1 Abort main loop
can anybody tells me what's wrong or how I can fix it in order to make the project run?
In snp-loader.lisp, instead of directory-namestring, you need to call pathname-directory:
(defparameter parm-snp-load-dir
(pathname-directory *load-truename*))
But then another problem occurs later, when defining a method for expectations-on-token. In c-snp-with-vars.lisp, the docstring is malformed, which triggers an error. Join both strings:
(defmethod expectations-on-token ((this-snp c-snp-with-vars) (tok t))
"Overloaded expectations-on-token to process variables and tests.
Get all expectations waiting for the token tok."
`(,#(find-static-expectations this-snp tok)
,#(find-dynamic-expectations this-snp tok)))
Reload the snp-loader.lisp file, and retry (snp-load-everything). It should load properly.
Edit. I contacted the original author; the latest version of the code is now hosted on GitHub at https://github.com/VKEDCO/AI/tree/master/NL/SNP.

Import from module using prefix without exposing non-prefixed things

I'd like to be able to only import namespaced items from a module in Racket. The prefix-in function does allow me to access functions from the module using the prefix, but it doesn't hide the old name.
$ racket
Welcome to Racket v6.6.
> (require (prefix-in tcp: racket/tcp))
> tcp-accept
#<procedure:tcp-accept>
> tcp:tcp-accept
#<procedure:tcp-accept>
Is there a simple way to hide the unprefixed names?
The prefix-in form only imports the prefixed names, not the unprefixed ones. However, the racket language includes the racket/tcp module by default, so it is already imported when the REPL starts up. If you use #lang racket/base, instead, then tcp-accept won’t be imported, so the prefixed import will be the only imported identifier. You can simulate this using the -I racket/base option in the REPL:
$ racket -iI racket/base
Welcome to Racket v6.7.0.4.
> (require (prefix-in tcp: racket/tcp))
> tcp-accept
; tcp-accept: undefined;
; cannot reference undefined identifier
; [,bt for context]
> tcp:tcp-accept
#<procedure:tcp-accept>

How should I configure Vulkan for cider-jack-in?

I'm working through the newly released Vulkan Tutorial in Clojure with CIDER, and I've hit a bit of a snag. The example makefile project works perfectly, but I'm having trouble translating it into Clojure.
My build.boot file just specifies the :source-paths and adds LWJGL as a dependency:
(set-env!
:source-paths #{"src"}
:dependencies
(let [lwjgl-version "3.0.0"]
[['org.lwjgl/lwjgl lwjgl-version]
['org.lwjgl/lwjgl-platform lwjgl-version :classifier "natives-linux"]]))
Then, in src/example/core.clj, I have an extension-count function that uses vkEnumerateInstanceExtensionProperties as demonstrated in the original example:
(ns example.core
(:import (org.lwjgl.vulkan VK10)))
(defn extension-count []
(let [^String layer-name nil
property-count (int-array 1)]
(VK10/vkEnumerateInstanceExtensionProperties layer-name property-count nil)
(first property-count)))
Now, from Bash, I can set the relevant environment variables LD_LIBRARY_PATH and VK_LAYER_PATH as I start up a REPL:
$ VULKAN_SDK_PATH=~/VulkanSDK/1.0.21.1/x86_64 LD_LIBRARY_PATH=$VULKAN_SDK_PATH/lib VK_LAYER_PATH=$VULKAN_SDK_PATH/etc/explicit_layer.d boot repl
boot.user=> (require '[example.core :refer [extension-count]])
nil
boot.user=> (extension-count)
4
As you can see, everything works correctly. But of course, when I use cider-jack-in by C-c M-j instead, I get an UnsatisfiedLinkError because CIDER isn't setting those variables:
boot.user> (import (java.util.function Consumer)
(org.lwjgl.system Configuration))
org.lwjgl.system.Configuration
boot.user> (Configuration/setDebugStreamConsumer
(reify Consumer
(accept [_ message]
(println message))))
nil
boot.user> (require '[example.core :refer [extension-count]])
nil
boot.user> (extension-count)
[LWJGL] Failed to load a library. Possible solutions:
a) Set -Djava.library.path or -Dorg.lwjgl.librarypath to the directory that contains the shared libraries.
b) Add the JAR(s) containing the shared libraries to the classpath.
[LWJGL] Enable debug mode with -Dorg.lwjgl.util.Debug=true for better diagnostics.
java.lang.UnsatisfiedLinkError: Failed to locate library: libvulkan.so.1
Am I supposed to be setting java.library.path or org.lwjgl.librarypath, as suggested in the above error message, instead of LD_LIBRARY_PATH? I can set either of those variables from profile.boot:
(System/setProperty
"java.library.path"
(str (System/getProperty "user.home") "/VulkanSDK/1.0.21.1/x86_64/lib"))
Now when I try C-c M-j again, it works:
boot.user> (require '[example.core :refer [extension-count]])
nil
boot.user> (extension-count)
4
However, this still doesn't let me set VK_LAYER_PATH, which will be fairly important in the future:
We will start using validation layers in Vulkan and you need to tell the Vulkan library where to load these from using the VK_LAYER_PATH variable:
test: VulkanTest
LD_LIBRARY_PATH=$(VULKAN_SDK_PATH)/lib VK_LAYER_PATH=$(VULKAN_SDK_PATH)/etc/explicit_layer.d ./VulkanTest
How can I set these environment variables for cider-jack-in? I'd prefer not to have to manually configure CIDER's dependencies for a standalone repl in a separate terminal and then connect to it using cider-connect, but if there's no other option here, I guess that's what I'll have to do.

I cannot run tests in clojure/midje

I run tests with:
lein midje :autotest
And I get error:
Exception in thread "main" java.lang.Exception: No namespace: sprint-is.json-export found
File is in: sprint-is/src/sprint_is/json_export.clj
It contains code:
(ns sprint-is.json-export)
(require [[noir.response :as response]])
(defn serialize [value] (response/json value))
It throws this error even when I don't have no test file. When I create test file, I get similar error:
No namespace: sprint-is.test.json-export found
Test is in: sprint-is/test/sprint_is/json_export.clj
And contains:
(ns sprint-is.test.json-export
(:require [sprint-is.json-export :as json-export]))
(fact "module can serialize scalar values"
(json-export/serialize 123) => 123)
When I try to import it from REPL, it cannot find the namespaces too. I tried to rename file, move files, rename directories, remove ns (it compiles but it doesn't work), asked on Clojure IRC. I compared the code with other projects (including those working on my computer) and it seems same.
Source code is here: https://bitbucket.org/jiriknesl/sprintis
You have a compilation error in one of your namespaces, I suspect sprint-is.json-export
On bitbucket, you have this:
(ns sprint-is.json-export)
(require [[noir.response :as response]])
(defn serialize [value] (response/json value))
which won't compile because noir.response and response are not defined.
you should have:
(ns sprint-is.json-export
(:require [noir.response :as response]))
(defn serialize [value] (response/json value))
If you insist on using require outside of the ns macro, you can do the following, but be aware this is not idiomatic usage.
(ns sprint-is.json-export)
(require '[noir.response :as response])
(defn serialize [value] (response/json value))