clojure

How can I generate the Fibonacci sequence using Clojure?

不想你离开。 提交于 2019-12-23 15:34:02
问题 (ns src.helloworld) (defn fibonacci[a b] (println a b (fibonacci (+ b 1) a + b))) (fibonacci 0 1) I'm new to Functional Programming and decided to start learning Clojure as it's very different from C#. I'd like to broaden my horizons. Here's the error I get: Clojure 1.2.0 java.lang.IllegalArgumentException: Wrong number of args (4) passed to: helloworld$fibonacci (helloworld.clj:0) 1:1 user=> #<Namespace src.helloworld> 1:2 src.helloworld=> Math problems never were my strong suit and I never

Compojure Routes losing params info

这一生的挚爱 提交于 2019-12-23 15:29:05
问题 My code: (defn json-response [data & [status]] {:status (or status 200) :headers {"Content-Type" "application/json"} :body (json/generate-string data)}) (defroutes checkin-app-handler (GET "/:code" [code & more] (json-response {"code" code "params" more}))) When I load the file to the repl and run this command, the params seems to be blank: $ (checkin-app-handler {:server-port 8080 :server-name "127.0.0.1" :remote-addr "127.0.0.1" :uri "/123" :query-string "foo=1&bar=2" :scheme :http :headers

What happened to closure.xml/emit?

送分小仙女□ 提交于 2019-12-23 15:22:49
问题 Many blogs refer to the clojure.xml/emit (or clojure.contrib.lazy-xml/emit) function, but it seems to be absent from the 1.2 documentation. Was it deprecated? What has replaced it? Can it be used to write Clojure-encoded XML (e.g.: {:tag :address :content {:tag :street ...} } )? UPDATE : I looked at the source code for clojure.contrib.lazy-xml/emit (by Chris Houser) and, although it too is not "official", it looks like a more stable solution than clojure.xml/emit . BTW, I have "discovered"

Chain call in clojure?

懵懂的女人 提交于 2019-12-23 15:15:41
问题 I'm trying to implement sieve of Eratosthenes in Clojure. One approach I would like to test is this: Get range (2 3 4 5 6 ... N) For 2 <= i <= N Pass my range through filter that removes multiplies of i For i+1 th iteration, use result of the previous filtering I know I could do it with loop/recur , but this is causing stack overflow errors (for some reason tail call optimization is not applied). How can I do it iteratively? I mean invoking N calls to the same routine, passing result of i th

Clojure: Inconsistent results using assoc-in

天大地大妈咪最大 提交于 2019-12-23 14:03:22
问题 Can someone explain what's the reasoning behind the following results using (assoc-in) ? (assoc-in {:foo {:bar {:baz "hello"}}} [:foo :bar] "world") => {:foo {:bar "world"}} (assoc-in {:foo {:bar nil}} [:foo :bar :baz] "world") => {:foo {:bar {:baz "world"}}} (assoc-in {:foo {:bar "hello"}} [:foo :bar :baz] "world") => ClassCastException java.lang.String cannot be cast to clojure.lang.Associative clojure.lang.RT.assoc (RT.java:702) Apparently I can replace a map and even nil with another data

Getting an exception when trying to run lein

爷,独闯天下 提交于 2019-12-23 13:08:23
问题 I get the following exception when trying to run lein in linux. Exception in thread "main" java.lang.NoClassDefFoundError: clojure.core.protocols$fn__5393 at java.lang.Class.initializeClass(libgcj.so.10) at clojure.core.protocols__init.load(Unknown Source:16) at clojure.core.protocols__init.<clinit>(Unknown Source) at java.lang.Class.initializeClass(libgcj.so.10) at java.lang.Class.forName(libgcj.so.10) at clojure.lang.RT.loadClassForName(RT.java:1578) at clojure.lang.RT.load(RT.java:399) at

How do we test whether something is a reference?

£可爱£侵袭症+ 提交于 2019-12-23 13:07:48
问题 For now I'm using this: (instance? clojure.lang.IDeref x) ...but I suspect there might be a better/more idiomatic way to do this. 回答1: This is incorrect, you are checking if the object x implements the IDeref interface, which simply means you can dereference the object with the @ symbol. What you want is this: (instance? clojure.lang.Ref x) EDIT: (Adjusting for comments). You can do what you suggested but this has the disadvantage of classifying objects made by other users that extend IDeref

Is clojure.lang really just implementation details?

陌路散爱 提交于 2019-12-23 12:56:06
问题 In Clojure, some tasks (such as instantiating a PersistentQueue or using deftype to implement a custom data type that is compatible with the clojure.core functions) require knowledge of the classes and/or interfaces in clojure.lang. However, according to clojure.lang/package.html: The only class considered part of the public API is clojure.lang.IFn. All other classes should be considered implementation details. Are these statements incorrect or outdated? If so, are there plans to correct them

How to reconnect to slime/swank-clojure session?

╄→尐↘猪︶ㄣ 提交于 2019-12-23 12:48:13
问题 It seems that whenever I disconnect from clojure slime session, I cannot reconnect again. I am using leiningen to start the swank session (with lein-swank plugin). So, every time I quit emacs (I know I shouldn't) or reboot/logout I have to restart both slime and swank. Is there a way to re-connect to a slime/clojure-swank session? 回答1: Yes, but you have to tell swank not to close the connection: lein swank 4005 "localhost" :dont-close true Note that you have to supply the port and host name

set, let, macros, nuts

吃可爱长大的小学妹 提交于 2019-12-23 12:34:40
问题 I am trying to build a quick toc from an html content. (to make it short) The code is dead simple: (defn toc [content] (doseq [i (take 5 (iterate inc 1))] (let [h (str "h" i)] (println ($ content h))))) where content is the html content, and $ is a macro required from clojure-soup While ($ content "h1") works, and returns a list of all the tags. Simple: ($ content (str "h" 1)) just won't make it whatever I do. How do I force (str "h" 1) to be properly eval-ed before the macro is called ?