ClojureScript to check if a property exists in a js object - clojurescript

How do I write the following in clojurescript?
obj = {"a" : 4};
"a" in obj;

exists? was added to check for undefined in ClojureScript :
(ns my.ns
(:require-macros [cljs.core :refer [exists?]]))
(if (exists? js/jQuery)
(println "jQuery"))
(println "no jQuery"))
One can also use aget and nil? to avoid calling JavaScript functions :
(def scope (js-obj))
(aset scope "var1" "Value")
(aget scope "var1") ;; "Value"
(aget scope "anotherVar") ;; nil
(nil? (aget scope "anotherVar")) ;; true

Following the accepted SO way to check if a js object property exists using the method "hasOwnProperty" We can translate it as follows:
(def foo (js-obj "bar" "baz"))
(.hasOwnProperty foo "bar")
;; => true
(.-bar foo)
;;=> "baz"
(.hasOwnProperty foo "car")
=> false
(.-car foo)
;;=> nil

Related

How to pass a symbol to a function to create a function in clojure

As a minimal example of what I want to do:
(defn mkfn [func]
(fn func [a] (print "I am a function")))
(mkfn 'x) ; => #function[user/mkfn/func--10871]
(type x)
(x)
The last two both result in:
Syntax error compiling at (conjure-log-12628.cljc:1:1).
Unable to resolve symbol: x in this context
I'm not sure why this doesn't work since fn takes symbols as input and 'x is a symbol. I'm also not sure how to accomplish this task.
For that matter:
user=> (def (eval 'y) 3)
Syntax error compiling def at (conjure-log-12628.cljc:1:1).
user=> (def 'y 3)
Syntax error compiling def at (conjure-log-12628.cljc:1:1).
First argument to def must be a Symbol
First argument to def must be a Symbol
user=> (type 'y)
clojure.lang.Symbol
Other things that don't work:
(defn mkfn [func]
(fn (sympol func) [a] (print "i am a function")))
(symbol "y") ; => y ; a symbol
(def (symbol "y") 3) ; => an err
You will probably need a macro. It seems that you want to call that function by the provided name, so you also have to replace fn with defn.
And you have to be careful about a number of arguments, because function x with argument vector [a] must be called with one argument, and not like (x).
(defmacro mkfn [func]
`(defn ~func [~'a]
(print "I am a function")))
(mkfn x)
=> #'user/x
(x 1)
I am a function=> nil
There is also other way, using intern, so you can completely avoid writing macros:
(intern *ns* 'x (fn [a] (print "I am a function")))
=> #object...
(x 1)
I am a function=> nil
Example with intern:
(defn mkfn [func]
(intern *ns* func (fn [a] (print "I am a function"))))
=> #'user/mkfn
(mkfn 'y)
=> #'user/y
(y 1)
I am a function=> nil
As for your errors, def is a special form, so it has different evaluation rules. It doesn't evaluate the first argument, which has to be a symbol- and (unevaluated) (eval 'y), 'y or (symbol "y") aren't symbols, while y is.
You gonna need a macro for that since you need code writing code.
(defmacro mkfn [func]
`(fn ~func [~'a] ...))
There 2 ways of doing it, either function plus eval or macro. If you really want to programatically create a new function with your chosen name, the macro solution is the way to go.
The function + eval solution is instructive, but you'll have to either quote the function name when you call it (via a 2nd eval) or save the created function in another variable when you create it.
If you are interested in writing macros, please see this other question first: How do I write a Clojure threading macro?
For the function + eval, we can start with my favorite template project and add the following:
(ns tst.demo.core
(:use demo.core tupelo.core tupelo.test))
(defn maker-eval
[fn-sym]
(let [ll (list 'fn 'anon-fn [] (str "I am " fn-sym))]
(spyx :eval ll)
(eval ll)))
(verify
(let [anon-fn-1 (maker-eval 'aaa)]
(is= "I am aaa" (anon-fn-1))) ; need the temp local variable
(let [anon-fn-2 (maker-eval 'bbb)]
(is= "I am bbb" (anon-fn-2))) ; need the temp local variable
)
and we can see the creation and use of the function, along with printed output:
:eval ll => (fn anon-fn [] "I am aaa")
:eval ll => (fn anon-fn [] "I am bbb")
For the macro version, we type
(defn maker-macro-impl
[fn-sym]
(let [ll `(defn ~fn-sym [] (str "I am " (str (quote ~fn-sym))))]
(spyx :macro ll)
ll))
(defmacro maker-macro
[fn-sym] (maker-macro-impl fn-sym))
(verify
(let [anon-fn-3 (maker-macro-impl 'ccc)]
(is= anon-fn-3 (quote
(clojure.core/defn ccc [] (clojure.core/str "I am " (clojure.core/str (quote ccc)))))))
(maker-macro ddd)
(is= (ddd) "I am ddd"))
and see printed:
:macro ll => (clojure.core/defn ccc [] (clojure.core/str "I am " (clojure.core/str (quote ccc))))
Note that the local variable anon-fn-3 was only used to test the maker-macro-impl function, but was not needed to call the newly-created function ddd
at the end of the unit test.

How to represent non-standard java objects when encoding to JSON in Clojure?

I have a standard clojure map of things. The keys are keywords, and the values are arbitrary values. They can be nil, numbers, strings, or any other kind of JVM object/class.
I need to know how to encode this map into JSON, so that "normal" values map to the usual JSON values (e.g. keywords -> strings, integers -> JSON numbers, etc.), while values of any other class map to the string representations of those values, like this:
{
:a 1
:b :myword
:c "hey"
:d <this is an "unprintable" java File object>
}
gets encoded thus:
{ "a": 1, "b": "myword", "c": "hey", "d": "#object[java.io.File 0x6944e53e foo]" }
I want to do this because my program is a CLI parsing library and I'm working with the caller of the library to build this map up, so I don't exactly know what types of data will be in it. However, I would like to print it to the screen all the same to aid the caller in debugging. I have tried to naively give this map to cheshire, but when I do, cheshire keeps choking with this error:
Exception in thread "main" com.fasterxml.jackson.core.JsonGenerationException: Cannot JSON encode object of class: class java.io.File: foo
Bonus: I am trying to keep my dependency counts down and I have already vetted cheshire as my JSON library, but full marks if you can find a way to do the above without it.
With cheshire you can add an encoder for java.lang.Object
user> (require ['cheshire.core :as 'cheshire])
nil
user> (require ['cheshire.generate :as 'generate])
nil
user> (generate/add-encoder Object (fn [obj jsonGenerator] (.writeString jsonGenerator (str obj))))
nil
user> (def json (cheshire/generate-string {:a 1 :b nil :c "hello" :d (java.io.File. "/tmp")}))
#'user/json
user> (println json)
{"a":1,"b":null,"c":"hello","d":"/tmp"}
you can also override the print-method for some objects you're interested in:
(defmethod print-method java.io.File [^java.io.File f ^java.io.Writer w]
(print-simple (str "\"File:" (.getCanonicalPath f) "\"") w))
the method gets called by the print subsystem every time it needs to print out the object of this type:
user> {:a 10 :b (java.io.File. ".")}
;;=> {:a 10,
;; :b "File:/home/xxxx/dev/projects/clj"}
Cheshire includes Custom Encoders that you can create and register to serialize arbitrary classes.
OTOH if you want to read the JSON back and reproduce the same types back in Java, you'll need to add some metadata too. A common pattern is to encode the type as some field like __type or *class*, like this, so that the deserializer can find the right types:
{
__type: "org.foo.User"
name: "Jane Foo"
...
}
Unless I'm missing something, there is no need for JSON here. Just use prn:
(let [file (java.io.File. "/tmp/foo.txt")]
(prn {:a 1 :b "foo" :f file})
=> {:a 1,
:b "foo",
:f #object[java.io.File 0x5d617472 "/tmp/foo.txt"]}
Perfectly readable.
As Denis said, you'll need more work if you want to read back the data, but that is impossible for things like a File object anyway.
You can get the result as a string (suitable for println etc) using a related function pretty-str if you prefer:
(ns tst.demo.core
(:use tupelo.test)
(:require
[tupelo.core :as t] ))
(dotest
(let [file (java.io.File. "/tmp/foo.txt")]
(println (t/pretty-str {:a 1 :b "foo" :f file}))
))
=> {:a 1,
:b "foo",
:f #object[java.io.File 0x17d96ed9 "/tmp/foo.txt"]}
Update
Here is a technique I frequently use when I need to coerce data into another form, esp. for debugging or unit tests:
(ns tst.demo.core
(:use tupelo.core tupelo.test)
(:require
[clojure.walk :as walk]))
(defn walk-coerce-jsonable
[edn-data]
(let [coerce-jsonable (fn [item]
(cond
; coerce collections to simplest form
(sequential? item) (vec item)
(map? item) (into {} item)
(set? item) (into #{} item)
; coerce leaf values to String if can't JSON them
:else (try
(edn->json item)
item ; return item if no exception
(catch Exception ex
(pr-str item))))) ; if exception, return string version of item
result (walk/postwalk coerce-jsonable edn-data)]
result))
(dotest
(let [file (java.io.File. "/tmp/foo.txt")
m {:a 1 :b "foo" :f file}]
(println :converted (edn->json (walk-coerce-jsonable m)))
))
with result
-------------------------------
Clojure 1.10.1 Java 14
-------------------------------
Testing tst.demo.core
:converted {"a":1,"b":"foo","f":"#object[java.io.File 0x40429f12 \"/tmp/foo.txt\"]"}

Clojurescript namespace as argument

Say I have the following Clojurescript code:
(ns one)
(defn foo [] 1)
(ns two)
(defn foo [] 2)
(ns other)
(defn thing [the-ns] (the-ns/foo))
; now I want to see 1
(other/thing one)
; now I want to see 2
(other/thing two)
How can I achieve this with Clojurescript?
one and two has the same "interface".
PS I know I can pass a function as an argument, but that doesn't answer the question. (e.g. the namespace might have many functions, and I don't want to pass them all)
 Tried ns-resolve
boot.user=> (ns one)
nil
one=> (defn foo [] 1)
#'one/foo
one=> (ns two)
nil
two=> (defn foo [] 2)
#'two/foo
two=> (ns other (:require [cljs.analyzer.api :as api]))
nil
other=> (defn thing [the-ns] (let [f (api/ns-resolve the-ns 'foo)] (f)))
#'other/thing
other=> (other/thing 'one)
java.lang.NullPointerException:
other=> (one/foo)
1
other=> (two/foo)
2
(Yes, there's no trace after java.lang.NullPointerException:, and I go on to show the initial namespaces resolve in the REPL session,)
If I move away from the contrived example, and try this in my Clojurescript project, I get this trace:
#object[Error Error: No protocol method IDeref.-deref defined for type null: ]
Error: No protocol method IDeref.-deref defined for type null:
at Object.cljs$core$missing_protocol [as missing_protocol] (http://0.0.0.0:8000/index.html.out/cljs/core.js:311:9)
at Object.cljs$core$_deref [as _deref] (http://0.0.0.0:8000/index.html.out/cljs/core.js:2164:17)
at cljs$core$deref (http://0.0.0.0:8000/index.html.out/cljs/core.js:4945:18)
at Function.cljs.analyzer.api.ns_resolve.cljs$core$IFn$_invoke$arity$3 (http://0.0.0.0:8000/index.html.out/cljs/analyzer/api.js:346:51)
at cljs$analyzer$api$ns_resolve (http://0.0.0.0:8000/index.html.out/cljs/analyzer/api.js:322:37)
at Function.cljs.analyzer.api.ns_resolve.cljs$core$IFn$_invoke$arity$2 (http://0.0.0.0:8000/index.html.out/cljs/analyzer/api.js:332:37)
at cljs$analyzer$api$ns_resolve (http://0.0.0.0:8000/index.html.out/cljs/analyzer/api.js:318:37)
at eval (eval at <anonymous> (http://0.0.0.0:8000/index.html.out/weasel/repl.js:30:495), <anonymous>:1:108)
at eval (eval at <anonymous> (http://0.0.0.0:8000/index.html.out/weasel/repl.js:30:495), <anonymous>:9:3)
at eval (eval at <anonymous> (http://0.0.0.0:8000/index.html.out/weasel/repl.js:30:495), <anonymous>:14:4)
You can use ns-resolve function to find a var in a namespace.
(ns one)
(defn foo [] 1)
(ns two)
(defn foo [] 2)
(ns other)
(defn thing [the-ns]
(let [f (ns-resolve the-ns 'foo)]
(f)))
(demo.other/thing 'one) ;; returns 1
(demo.other/thing 'two) ;; returns 2
But for this kind of polymorphic behavior, using protocols or multi-methods is more suitable.
UPDATE
The above code works only in Clojure, because ns-resolve does not exists in ClojureScript. In fact, ClojureScript does not have Vars.
But we can manually get the function from namespace object. We also need to mark functions with export metadata flag to prevent function names get "munged":
(ns demo.one)
(defn ^:export foo [] 1)
(ns demo.two)
(defn ^:export foo [] 2)
(ns demo.other)
(defn thing [the-ns]
(let [f (aget the-ns "foo")]
(f)))
(other/thing demo.one)
(other/thing demo.two)

Reflect on Clojure Function Name

How do I use reflection in Clojure to extract the function name of a symbol that evaluates to a function?
(require '[clojure.reflect :as r])
(defn foo [x] x)
(r/reflect foo)
=> {#clojure.reflect.Field ... 'gobbledygook}
Something about (map :name (:members (r/reflect foo)))?
No, please do not use str because it behaves differently. In my example, here is what I've got:
user=> (defn foo [x] x)
#'user/foo
user=> (str foo)
"user$foo#7cb20059"
Instead, use combination of meta and var calls:
(-> foo var meta)
This code returns a map of variable's metadata. The key :name holds the function name as a string:
(-> foo var meta :name)
foo
If you also want to have an ns's name, do the following:
user=> (-> foo var meta :ns ns-name)
user
It gets the namespace object and returns its name with the ns-name function as a string.
Those methods do not depend on Clojure or REPL version and thus are more maintainable.
Looks like I can use str:
(str foo)
=> "#'my-project/foo"

Is there a way to make a function var dynamic after definition?

Post updated to make it relevant to the course of events (responses and eliminating clutter).
Thank you very much for your time and help !
In some previous version of Clojure every var could be bound with a "binding" form.
Nowadays you get "Can't dynamically bind non-dynamic var" if not defined as dynamic.
In some contexts making a function var dynamic after definition could be useful (stubbing/mocking in testing).
Don't try:
(def ^{:dynamic true} log-call #'log-call)
It will eventually cause StackOverflowError since you are defining a function that calls itself (thank you for your explanation).
The updated question:
The approach suggested does not seem to work.
Forms called from the binding form don't get the binding defined.
Could you please help to figure out what I'm missing??
Here is the updated code:
(def all-expenses [{:amount 33.0 :date "today"}
{:amount 44.0 :date "yesterday"}])
(defn fetch-all-expenses [])
(defn fetch-expenses-greater-than [threshold]
(let [all (fetch-all-expenses)]
;calling from a nested form does not see the dynamically bound definition!
(println "2) from fetch-expenses-greater-than:" (fetch-all-expenses))
all))
(defn wrap [f]
(fn [& args] (apply f args)))
(def ^{:dynamic true} fetch-all-expenses (wrap fetch-all-expenses))
(binding [fetch-all-expenses (constantly all-expenses)]
(let [filtered (fetch-expenses-greater-than 15.0)]
(println "1) from inside binding:" (fetch-all-expenses))));calling from binding form OK!
The result of executing the in the repl is:
2) from fetch-expenses-greater-than: nil
1) from inside binding: [{:date today, :amount 33.0} {:date yesterday, :amount 44.0}]
nil
If I change the definition of fetch-all-expenses to
(defn ^:dynamic fetch-all-expenses [])
The result is as expected:
2) from fetch-expenses-greater-than: [{:date today, :amount 33.0} {:date yesterday, :amount 44.0}]
1) from inside binding: [{:date today, :amount 33.0} {:date yesterday, :amount 44.0}]
nil
It is possible to make a Var dynamic after it's been defined, but this will have no effect on code compiled before this change (it will still use the root binding). Use with-redefs to install custom replacements for functions during tests and the like.
The reason for this is that whether a Var is marked dynamic or not determines the way code using this Var is compiled. If it is not dynamic, such code will just get the root binding directly, saving some work; if it is dynamic, it will go through the somewhat more complex process of checking whether there is a thread-local binding in place for it.
So, there is no way to cause already compiled code use a custom function installed with binding after marking the Var holding the function dynamic. However, those calls still go through the Var, they just happen to go directly to the root binding, so you can use custom replacement functions for testing and the like if you install them as root bindings for the appropriate Vars. with-redefs encapsulates all the necessary logic for doing this cleanly.
Let's see how this works at the REPL:
;; define a non-dynamic Var:
(def foo 0)
;; this will throw an exception complaining about the attempt
;; to bind a non-dynamic Var:
(binding [foo 1]
foo)
;; let's define a function using foo;
;; we'll use it further down:
(defn bar []
foo)
;; now let's mark the Var dynamic:
(.setDynamic #'foo)
;; this will now evaluate to 1:
(binding [foo 1]
foo)
;; however, this will still return 0:
(binding [foo 1]
(bar))
(def ^{:dynamic true} log-call #'log-call) This statement says: "Create a var log-call and bind it to the var log-call. So when you try to use log-call var it will keep refering itself forever and hence StackOverflow exception.
You can try something like this:
(defn wrap [f]
(fn [& args] (apply f args)))
(def ^{:dynamic true} log-call (wrap log-call))
(def ^{:dynamic true} fetch-all-expenses (wrap fetch-all-expenses))
Thank you very much for your answer #MichałMarczyk. That explains it.
With code using the var before making it dynamic:
(def foo 0)
(defn bar []
foo)
(.setDynamic #'foo)
(binding [foo 1]
;; prints foo 1 . bar 0
(println "foo" foo ". bar" (bar)))
With code using the var after making it dynamic:
(def foo 0)
(.setDynamic #'foo)
(defn bar []
foo)
(binding [foo 1]
;; prints foo 1 . bar 1
(println "foo" foo ". bar" (bar)))
And yes!....with with-redefs instead of binding everything works as expected. That's exactly what I was needing.