clojure

Learning functional/Clojure programming - practical exercises? [closed]

陌路散爱 提交于 2019-12-18 09:56:27
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . I'm learning functional programming with Clojure. What practical exercises can you recommend? Online repositories with solutions would be perfect. One idea I can think of is going through all the popular algorithms on sorting, trees, graphs etc. and implementing them in Clojure myself. While it could work, it

Learning functional/Clojure programming - practical exercises? [closed]

守給你的承諾、 提交于 2019-12-18 09:56:01
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . I'm learning functional programming with Clojure. What practical exercises can you recommend? Online repositories with solutions would be perfect. One idea I can think of is going through all the popular algorithms on sorting, trees, graphs etc. and implementing them in Clojure myself. While it could work, it

Need to force realization of lazy seqs before/after element-wise imperative operations?

可紊 提交于 2019-12-18 09:36:52
问题 If I perform a side-effecting/mutating operation on individual data structures specific to each member of lazy sequence using map , do I need to (a) call doall first, to force realization of the original sequence before performing the imperative operations, or (b) call doall to force the side-effects to occur before I map a functional operation over the resulting sequence? I believe that no doall s are necessary when there are no dependencies between elements of any sequence, since map can't

Why does Datomic yield the same temporary ID twice in a row when iterating?

走远了吗. 提交于 2019-12-18 09:16:00
问题 This will produce two different ids, which is great: #db/id[:db.part/user] #db/id[:db.part/user] but anything like the following (I tried a lot of ideas so far) will produce the same id twice, which is not what I want: (repeatedly 2 (fn [] #db/id[:db.part/user])) (for [n [1 2]] #db/id[:db.part/user]) All yield something like (#db/id[:db.part/user -1000774] #db/id[:db.part/user -1000774]) where the number produced is the same for each call. What I actually want is for the calls to NOT produce

In Clojure, how to destructure all the keys of a map?

久未见 提交于 2019-12-18 07:52:08
问题 In clojure, it is possible to destructure some keys of a map like this: (let [{:keys [cpp js]} {:cpp 88 :js 90}] (println js); 90 (println cpp); 88 ) Is there a way to destructure all the keys of a map? Maybe something like: (let [{:all-the-keys} {:cpp 88 :js 90}] (println js); 90 (println cpp); 88 ) 回答1: Not really, and it wouldn't be a good idea. Imagine: (let [{:all-the-keys} m] (foo bar)) Are foo and bar globals? Locals? Keys you should extract from m? What should this code do if m

Clojure - Calculate with big numbers

我与影子孤独终老i 提交于 2019-12-18 05:43:56
问题 I want to calculate !1000 in clojure, how can I do this without getting a integer-overflow exception? My factorial code is right now: (reduce * (range 1 1001)) . 回答1: You could use the *' operator which supports arbitrary precision by automatically promoting the result to BigInt in case it would overflow: (reduce *' (range 1 1001)) 回答2: Put N at the end of the number which makes it a bigint, (reduce * (range 1N 1001N)) 回答3: Coerce the parameters to clojure.lang.BigInt (reduce * (range (bigint

How to use leiningen to develop using local jars?

我与影子孤独终老i 提交于 2019-12-18 04:45:11
问题 I realize that this question is pretty much the exact question found here. However, seeing as that question is 1.5 years old (or so), I would like to revisit it. How does one add local dependencies using leiningen? Surely this capability must exist by now? 回答1: Create a private Maven Repository, and then, add the following to your project.clj :repositories {"local" ~(str (.toURI (java.io.File. "your_local_repository")))} 回答2: If the jars are based on your own projects, you can use lein

use-cases for BigInt versus BigInteger in Clojure

喜夏-厌秋 提交于 2019-12-18 04:43:10
问题 I'm looking for guidance on when to use Clojure BigInt versus Java BigInteger in Clojure. Both work just fine, and I am assuming that the main reason to use BigInt is to take advantage of operators like + and = , which have to be accessed via the Java instance methods .add and .equals , for instance. But there are few operators, such as isProbablePrime , that I can only access from BigInteger. It seems pretty easy to shift from BigInt to BigInteger or vice versa, but the presence of both

Clojure: Why a function should be `declare` if it is called before definition in the source code

有些话、适合烂在心里 提交于 2019-12-18 04:42:16
问题 In Clojure, if you call a function before its definition, e.g. (foo (bar 'a)) (defn bar [] ...) it is not compiled. One should add (declare bar) before (foo (bar 'a)) . Why Clojure is designed as this? I mean, in most languages, except C/C++, such as Java, Python, PHP, Scala, Haskell or even other Lisps, especially in dynamic-type languages, function declaration is not needed, that is, function definition could be put either before or after a call. I feel it uncomfortable to use. 回答1: Clojure

Expand a vector into the arguments of a function

荒凉一梦 提交于 2019-12-18 04:30:06
问题 Is there a way to expand a vector of values into the arguments of a function? e.g. something like this: (defn addnums [a b] (apply + (flatten [a b]))) (def args [[1 2 3] [1 2 3]]) (addnums *args) 回答1: You can just use apply again: (apply addnums args) 来源: https://stackoverflow.com/questions/3849084/expand-a-vector-into-the-arguments-of-a-function