Fork me on GitHub

Hi, I’m probably mis-using atoms and have confused myself thoroughly


I have an atom that maps a string id (it’s coming from a database, it was awkward to convert to a keyword) to an array of maps


(def actions_by_card (atom {}))


It gets populated OK, and I can map over it etc… the first element is


(first @actions_by_card)


["2gs89taaDEwd2Ruzi" [{:context {}, :card_id “2gs89taaDEwd2Ruzi      …. more maps   }]]


But I can’t access it directly


(@actions_by_card "2gs89taaDEwd2Ruzi”)


CompilerException java.lang.ClassCastException: clojure.lang.LazySeq cannot be cast to clojure.lang.IFn, compiling:(cavalry_stats/core.clj:137:1)

(get @actions_by_card "2gs89taaDEwd2Ruzi”)
;; -> nil


What am I missing?


It sounds like you populated it incorrectly — it has a lazy sequence in it, instead of a map.


How are you populating it?


Hi guys, I have trouble connecting to nrepl through cider. I have project created with re-natal (re-natal init projectname). Then I go into emacs and jack-in… from there I’m not sure how to start figwheel to see changes on ios (simulator and device)


when I do > re-natal use-figwheel and > lein figwheel ios, figwheel server starts and I have to open up separate terminal to > re-natal xcode. doing that gives me repl on my terminal but I’m not using emacs cider at this point at all…


@jeeq based on you may want to ask #cljsrn if you don't get an answer here


@mordocai Thank you


@jeeq, I generally launch two REPLs. One for Figwheel and one for the server. I’ve no familiarity with re-natal but you should probably start solving this by minimizing the issue and just making sure you can establish any connection and then expand from there.


Will do. Thank you @akiva


Factory functions, how do I create a function that returns a map?


(defn factory [value]
	({:key value}))


I tried ^ but when I call it (factory "value”) I get the error: > ArityException Wrong number of args (0) passed to: PersistentArrayMap


@cameronbourke you return a list. The first element of a list is always considered as a function. in your case this function is the map itself. a map is a function that returns value for a known key. So, your error is that the map complains about missing required argument. e.g

user=> ({:a 1} :a)                                                                                                                               │
what you want to do is simply:
(defn factory [value]                                                                                                                     
 {:key value})                                                                                                                         
user=> (factory 1)                                                                                                                               
{:key 1}


@st Thanks! That makes sense. But isn’t the syntax for defn macro defn <name> [<args>] (<body>)?


nope, syntax is (defn name “documentation” [args] body) or (for multi-arity) (defn name “documentation” ([args1] body1) ([args2] body2)) documentation is optional.


That is really cool how you can have seperate bodies for each argument. Thanks for clarifying that @st


Indeed. remember that documentation string goes just after name of function.


Hi guys. A real beginners question here. Im using cursive with intellij and trying to get some kind of hot reloading env setup.

(defproject natbox "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url ""
  :license {:name "Eclipse Public License"
            :url  ""}
  :dependencies [[org.clojure/clojure "1.8.0"]]
  :profiles {:dev {:dependencies [[org.clojure/tools.namespace "0.2.11"]]
                   :source-paths ["dev"]}})
this is my project.clj… I assume now that I need to make a cursive repl and specify the dev profile… But when I do, I get the following error
Error loading Could not locate clojure/tools/nrepl/server__init.class or clojure/tools/nrepl/server.clj on classpath.
Error loading complete.core: Could not locate complete/core__init.class or complete/core.clj on classpath.
Exception in thread "main" java.lang.ClassNotFoundException:, compiling:(/private/var/folders/9s/txldqs4x1ts59q6qblrn133c0000gn/T/form-init6119612237788758823.clj:1:773)
	at clojure.lang.Compiler.analyzeSeq(
	at clojure.lang.Compiler.analyze(
	at clojure.lang.Compiler.access$300(
	at clojure.lang.Compiler$LetExpr$Parser.parse(
	at clojure.lang.Compiler.analyzeSeq(
	at clojure.lang.Compiler.analyze(
	at clojure.lang.Compiler.analyze(
	at clojure.lang.Compiler$BodyExpr$Parser.parse(
	at clojure.lang.Compiler$FnMethod.parse(
	at clojure.lang.Compiler$FnExpr.parse(
	at clojure.lang.Compiler.analyzeSeq(
	at clojure.lang.Compiler.analyze(
	at clojure.lang.Compiler.eval(
	at clojure.lang.Compiler.eval(
	at clojure.lang.Compiler.load(
	at clojure.lang.Compiler.loadFile(
	at clojure.main$load_script.invokeStatic(main.clj:275)
	at clojure.main$init_opt.invokeStatic(main.clj:277)
	at clojure.main$init_opt.invoke(main.clj:277)
	at clojure.main$initialize.invokeStatic(main.clj:308)
	at clojure.main$null_opt.invokeStatic(main.clj:342)
	at clojure.main$null_opt.invoke(main.clj:339)
	at clojure.main$main.invokeStatic(main.clj:421)
	at clojure.main$main.doInvoke(main.clj:384)
	at clojure.lang.RestFn.invoke(
	at clojure.lang.Var.invoke(
	at clojure.lang.AFn.applyToHelper(
	at clojure.lang.Var.applyTo(
	at clojure.main.main(
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(
	at java.lang.reflect.Method.invoke(
	at com.intellij.rt.execution.application.AppMain.main(
Caused by: java.lang.ClassNotFoundException:
	at clojure.lang.DynamicClassLoader.findClass(
	at java.lang.ClassLoader.loadClass(
	at clojure.lang.DynamicClassLoader.loadClass(
	at java.lang.ClassLoader.loadClass(
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(
	at clojure.lang.RT.classForName(
	at clojure.lang.RT.classForName(
	at clojure.lang.Compiler$HostExpr.maybeClass(
	at clojure.lang.Compiler.macroexpand1(
	at clojure.lang.Compiler.analyzeSeq(
	... 33 more

Process finished with exit code 1
Exception starting REPL: java.lang.InterruptedException


@alexisvincent > The profiles listed above are active during development, Scattered above this is: :user, :dev, :system, and :base


Your :dev profile should be automatic


@dominicm I set +dev and that worked. But Ill try without quickly


I use compojure-api to build a REST API with Swagger documentation. I would like to have the main entry at /api (for instance, it could be /v1 or /v2). I set the :basePath swagger route parameter to /api. And when I went to the swagger doc page, there was a Can't read swagger JSON from . My different api routes didn't begin by a /api.


Hello everyone, I’m new to clojure so I just wanted to introduce myself


@dorianc.b: Hi, glad to have you on board!


You starting to learn for hobby projects or using it for work?


Hobby projects I guess. I just started to learn programming. My first language was Java and I wanted to pick up functional programming so clojure seemed like a good fit since I could still use some of the java classes if I still need them


I just started reading the book Clojure for the brave and the true and I’m really liking the simple syntax.


@dorianc.b: That's a great book to start with. It's really a fun language to learn and use, and this channel is a great place to ask questions when you run into things you aren't sure of - don't be shy, we've all been through the learning curve and there's a ton of stuff to learn.


Anybody using plumatic/schema ?


@abarylko occasionally yes


How would I load an external file in Clojurescript? I'm thinking of using the markdown-clj for easier formatting of significant amount of text, but I'm having some issues figuring out how to load a simple file in Clojurescript. It can't be too difficult, what obvious thing am I missing?


@straistra I'm having a surprisingly hard time getting my use case to work, but in general


kind of a weird and highly specific q, but is there some idiomatic way to merge two lists of maps to get the following result? e.g. ({:id 1 :some-string “abc”} {:id 2 :some-string “def”} {:id 3 :some-string “ghi”}) and ({:id 1 :some-num 123} {:id 2 :some-num 456} {:id 4 :some-num 789}) results in (({:id 1 :some-string “abc”} {:id 1 :some-num 123}) ({:id 2 :some-string “def”} {:id 2 :some-num 456}) ({:id 3 :some-string “ghi”}) ({:id 4 :some-num 789}))


@straistra Oh, I read that wrong. You mean just loading a file not loading external javascript right?


I have a sequence of maps of statistics I’ve gathered from a system. I’d like to add up the elements. Some of the elements are nested maps.


{:total_edit_time 285,
   :messages_sent 1,
   :followups 0,
   :bounces 0,
    {:locked 1, :total_edit_time 285, :drafts_sent 1, :replies 1}},
    {:total_edit_time 285, :drafts_sent 1, :replies 1}},
   :reply_received true}
  {:total_edit_time 1016,
   :messages_sent 0,
   :followups 0,
   :bounces 0,
   :users {:W4byrxBxghnsrezbN {:locked 1, :total_edit_time 1016}},
   :campaigns {:F2etLeSKAopLKHKxa {:total_edit_time 1016}},
   #object[org.joda.time.DateTime 0x661adcfb "2016-09-07T07:31:49.637-07:00"]}


there are two of them.


The code I’ve got that reduces the sequence is really long and wordy. I wondered if there was an elegant way to do it.


Are you just looking for any occurrence of a specific key, regardless of depth?


@mordocai Yeah, just a file. Essentially a text file. It's really easy in clojure, but clojurescript is baffling me.


Does the work in Clojurescript? If so, then I am sorted. But I kind of figured it wouldn't work in javascript. Maybe my assumptions are wrong.


@seancorfield Yes, and the output should have the superset of keys in, i.e., the “user” map


Some of the keys don’t add obviously - I can exclude those in whatever way including stripping them


@straistra I don't think so. I think slurp should work if all you need is to load the whole file at once, but i'm barely into clojurescript and haven't needed it yet


Right now I have a (defn summarize [rec1 rec2] …) function that does every little bit manually, then I use it to reduce over the sequence


but that seems clunky


@mordocai Isn't slurp part of the (or reader, to be specific)? Because slurp doesn't actually work in Clojurescript. At least not without doing some require or whatnot first.


@straistra Okay, it may be then. Are you wanting to do the markdown parsing at compile time or run time? Do you want the file read at compile time or run time? Node or browser?


@mordocai Well, I am running it through Reagent. I guess I could manually type in some javascript, but I don't know any. Can't be too hard...


@straistra Well, since you won't have a file API in the browser, I imagine you need to load the file into a string at compile time one way or another. Not sure how you'd do that exactly. Might try asking with the specifics in #clojurescript


@mordocai Yeah, thanks. It's not an immediate concern, but I was a bit surprised how I hit a sudden wall trying to do this.


@straistra: I'd be surprised if it wasn't a solved problem, I just don't know enough to point you to it really


@val_waeselynck How would you go about defining a schema based on another schema?


@credulous I think you should be able to do that fairly easily with a recursive function that uses reduce-kv on each map... I'm away from my desk right now but I'll try to run up an example when I get back... If you can post a complete example as a Gist or on RefHeap or PasteBin and put the link here, that will help me test.


But basically the reduce-kv would check the value and if it's numeric, use update to add it to an accumulator map (with fnil to provide 0 if the key doesn't exist), and if it's a map, call summarize recursively with the accumulator map to produce a new accumulator, else ignore it (just return the accumulator as-is).


@credulous something like this (not well tested): (defn summarize [data acc] (reduce-kv (fn [acc k v] (cond (number? v) (update acc k (fnil + 0) v) (map? v) (summarize v acc) :else acc)) acc data)) and then (->> {} (summarize rec1) (summarize rec2))


(excuse formatting)


@credulous another option using the specter library:

(->> data (traverse (recursive-path [] MapWalker [map? (multi-path (must :total-edit-time) [MAP-VALS MapWalker])]) (reduce + 0))


That looks like a lot more "magic" but that's kinda the point of Specter I guess...


@seancorfield I don't think it's so crazy. It defines a recursive path named MapWalker that continues navigation if the structure is a map and navigates to both the :total-edit-time key if it exists as well as recursively following the MapWalker path again on each of the map values.


The net effect is navigating to the value of :total-edit-time in any nested map.


I suppose this is more of an intermediate usage of specter, and certainly the internals of specter are pretty magical. However, I think it exposes a pretty clean API.


Note that my suggestion would sum up all keys that had numeric values, not just that one key. It wasn't clear to me what you were really looking for...


Ah, good point. I may have misread the question.


I saw Nathan's talk on Specter but I haven't seen a use case for it in anything I've run across so far.


Personally, I find myself using it whenever I have more nested data structures. It's also pretty good at navigating recursive structures, like a graph/tree.


As with any library, there's some learning curve. But I think once you grok how paths are put together, specter locates data within data structures extremely efficiently and clearly.