clojure

Clojure: summing values in a collection of maps

大兔子大兔子 提交于 2020-01-11 09:18:12
问题 I am trying to sum up values of a collection of maps by their common keys. I have this snippet: (def data [{:a 1 :b 2 :c 3} {:a 1 :b 2 :c 3}] (for [xs data] (map xs [:a :b])) ((1 2) (1 2)) Final result should be ==> (2 4) Basically, I have a list of maps. Then I perform a list of comprehension to take only the keys I need. My question now is how can I now sum up those values? I tried to use "reduce" but it works only over sequences, not over collections. Thanks. ===EDIT==== Using the

What was the reasoning behind ClojureScript not needing Clojure's defstruct?

让人想犯罪 __ 提交于 2020-01-11 05:19:13
问题 defstruct is not supported in ClojureScript - it would appear to be by design. Now it may be that this is effectively a deprecated part of the Clojure language, and the designers of ClojureScript were just hoping everyone had moved on. (But this is my speculation). My question is: What was the reasoning behind ClojureScript not needing Clojure's defstruct? 回答1: defstruct is effectively deprecated in the language, in favor of defrecord . We are supposed to move on in (JVM-based) Clojure, so I

How do I access a specific element in a vector of vectors clojure

江枫思渺然 提交于 2020-01-11 04:55:12
问题 If I have a vector defined as (def matrix [[1 2 3][4 5 6]]) How in clojure do I access a random element in a vector of vectors? I keep seeing people say online that one of the benefits to using a vector over a list is that you get random access instead of having to recurse through a list but I haven't been able to find the function that allows me to do this. I'm used to in c++ where I could do matrix[1][1] and it would return the second element of the second vector. Am I stuck having to loop

Using lein project with /lib doesn't work

爱⌒轻易说出口 提交于 2020-01-11 03:19:14
问题 When using lein 2.2, trying to put jar files in /lib does not work. I tried and it doesn't seems to work but plenty of docs out there says this way still works. 回答1: The lib directory functionality was removed in Leiningen v2.0, in favor of a repository (repeatability). To add free floating jars to a project, you will need to either deploy your dependency to Clojars or a Maven repository. The Maven repository can be as simple as a directory in your project folder. Please see the answer to

What are the leiningen default repositories?

依然范特西╮ 提交于 2020-01-11 01:56:51
问题 Leiningen (https://github.com/technomancy/leiningen) looks into some default repositories to satisfy the dependencies specified in your project.clj. I want to browse these repositories to see what's available out-of-the-box in leiningen. What are these repos and where can I look them up for my specific version of leiningen. 回答1: As of the time of writing, leiningen uses the following default repositories: "central" - http://repo1.maven.org/maven2 "clojure" - http://build.clojure.org/releases

Creating Android apps without Java

落爺英雄遲暮 提交于 2020-01-10 22:27:44
问题 I'd like to start creating Android apps but I don't like Java. I read that scala can be used to do it. Are there another option?(Clojure?) I'm a Python/Django developer so it would be great to learn a pretty different language. 回答1: At this point Scala is the one that is most mature..I wanted to try groovy myself but its not even out of alpha.. Plus Scala on android has docs..:) 回答2: It's not hard to do with Mirah (formerly Duby), a very young language based on Ruby that compiles to bytecode

What are the namespace gotchas for clojurescript when coming from clojure?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-10 10:34:10
问题 I'm trying to understand the namespacing model in clojurescript. I understand that javascript doesn't come built in with namespace support, so its been an add on via the google closure library. However, I don't understand how clojurescript deals with these differences. Can someone please explain? Lets say for example I want to use the google charts api and whenever possible would like to use advanced compilation. What do I use in the closure/build call, how would the ns form look like and

Should I use a function or a macro to validate arguments in Clojure?

早过忘川 提交于 2020-01-10 08:52:08
问题 I have a group of numeric functions in Clojure that I want to validate the arguments for. There are numerous types of arguments expected by the functions, such as positive integers, percentages, sequences of numbers, sequences of non-zero numbers, and so on. I can validate the arguments to any individual function by: Writing validation code right into the function. Writing a general purpose function, passing it the arguments and expected types. Writing a general purpose macro, passing it the

Can I clean the repl?

走远了吗. 提交于 2020-01-10 06:52:18
问题 I have played with a lot of code in a repl console, how can I clear it? I would like a fresh one without restarting it. Can that be done? 回答1: If you want to clear the current namespace of all temporary variables and functions you declared you can use this one liner (or make a function of it) : (map #(ns-unmap *ns* %) (keys (ns-interns *ns*))) or (ns myutil) (defn ns-clean "Remove all internal mappings from a given name space or the current one if no parameter given." ([] (ns-clean *ns*)) (

How can I call a java static method in clojure?

倖福魔咒の 提交于 2020-01-09 10:00:59
问题 I wish to call class on the String class. How can I access this static method? 回答1: You can call a static method using (ClassName/methodName arguments) . However class is not a static method, it's a java keyword and you don't need it in clojure. To get the Class object associated with the String class, just use String . 回答2: An example is worth 100 words: (. String (valueOf 1)) 回答3: Class doesn't have a "class" method, nor a "class" member. The symbol String is mapped to the class java.lang