Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use maps created by the edn reader in clojurescript?

This prints :bar in Clojure as I would expect:

(println (:foo (clojure.tools.reader.edn/read-string "{:foo :bar}")))
;=> :bar

But this prints nil in ClojureScript:

(println (:foo (cljs.reader/read-string "{:foo :bar}")))
;=> nil

To make things stranger, this prints :bar in ClojureScript as I would expect:

(let [data (cljs.reader/read-string "{:foo :bar}")]
  (println ((first (keys data)) data )))
;=> :bar

How do I access a value in a map that was created by the reader? Is this a character encoding thing?

Edit

Here's the namespace as requested in the comments:

(ns clojuresite.homepage
  (:require-macros [hiccups.core :as hiccups])
  (:require [hiccups.runtime :as hiccupsrt]
            [cljs.nodejs :as node]
            [cljs.reader :as reader]))
like image 703
bmaddy Avatar asked Oct 03 '22 20:10

bmaddy


2 Answers

Try this:

(.log js/console
  ((keyword "foo") (cljs.reader/read-string "{:foo :bar}")))

If that works, and generates ﷐:bar, you have old generated code hanging around and you should run a lein cljsbuild clean.

There was a change in 0.0-1877 that switched keywords, in the generated javascript, from ﷐:foo to cljs.core.Keyword.

like image 74
Jared314 Avatar answered Oct 13 '22 11:10

Jared314


the return value of println is nil and perhaps the output from the print is likely going into another buffer if your are using emacs or being logged somewhere else. Perhaps you are seeing the return value of println instead of it's output.

ClojureScript:cljs.user> (ns cljs.user (:require [cljs.reader :as edn]))
nil
ClojureScript:cljs.user> (println (:foo (cljs.reader/read-string "{:foo :bar}")))
:bar
nil

vs:

ClojureScript:cljs.user> (:foo (cljs.reader/read-string "{:foo :bar}"))
:bar
like image 31
Arthur Ulfeldt Avatar answered Oct 13 '22 10:10

Arthur Ulfeldt