clojure

Using clojure.contrib functions in slime REPL

蓝咒 提交于 2019-12-18 19:00:03
问题 I want to use the functions in the clojure.contrib.trace namespace in slime at the REPL. How can I get slime to load them automatically? A related question, how can I add a specific namespace into a running repl? On the clojure.contrib API it describes usage like this: (ns my-namespace (:require clojure.contrib.trace)) But adding this to my code results in the file being unable to load with an "Unable to resolve symbol" error for any function from the trace namespace. I use leiningen 'lein

How can I access the last result in Scala REPL?

爷,独闯天下 提交于 2019-12-18 18:49:45
问题 In python REPL I can do things like: >>> [1,2,3,4] [1, 2, 3, 4] >>> sum(_) 10 In clojure REPL I can do this: user=> "Hello!" "Hello!" user=> *1 "Hello!" Is there is something like this in Scala REPL? 回答1: Yes, you can use dot notation to refer to the last result: scala> List(1,2,3,4) res0: List[Int] = List(1, 2, 3, 4) scala> .sum res1: Int = 10 回答2: You can refer to the previous output as res N for some N . You've probably noticed that in the Scala REPL, results are printed in the form res N

Clojure's maps: are keys and vals in same order?

我的未来我决定 提交于 2019-12-18 18:47:15
问题 Is it ok to rely on (= m (zipmap (keys m) (vals m))) in Clojure 1.3+? Having this behavior makes for slightly more readable code in some situations, eg (defn replace-keys [smap m] (zipmap (replace smap (keys m)) (vals m))) vs. (defn replace-keys [smap m] (into {} (for [[k v] m] [(smap k k) v])) 回答1: I can confirm (officially) that the answer to this is yes. The docstrings for keys and vals were updated in Clojure 1.6 to mention this (see http://dev.clojure.org/jira/browse/CLJ-1302). 回答2: Yes,

with Clojure threading long running processes and comparing their returns

家住魔仙堡 提交于 2019-12-18 16:55:40
问题 I have two different function on two very large sets of data that need to be processed, in the end coming down to two Boolean values. those values need to then be anded together for the final result. my question is what is the best way to create threads so the two long functions can run at the same time. my thoughts were something like, (def f (future longProcessOne(data_one))) (def g (future longProcessTwo(data_two))) (and @f @g) but I was looking for input on a better way to go about this.

Why Clojure idiom prefer to return nil instead of empty list like Scheme?

强颜欢笑 提交于 2019-12-18 14:48:06
问题 From a comment on another question, someone is saying that Clojure idiom prefers to return nil rather than an empty list like in Scheme. Why is that? Like, (when (seq lat) ...) instead of (if (empty? lat) '() ...) 回答1: I can think of a few reasons: Logical distinction . In Clojure nil means nothing / absence of value. Whereas '() "the empty list is a value - it just happens to be a value that is an empty list. It's quite often conceptually and logically useful to distinguish between the two.

Missing form parameters in Compojure POST request

瘦欲@ 提交于 2019-12-18 14:32:25
问题 I'm having problems getting the form parameters in the following Compojure example: (ns hello-world (:use compojure.core, ring.adapter.jetty) (:require [compojure.route :as route])) (defn view-form [] (str "<html><head></head><body>" "<form method=\"post\">" "Title <input type=\"text\" name=\"title\"/>" "<input type=\"submit\"/>" "</form></body></html>")) (defroutes main-routes (GET "/" [] "Hello World") (GET "/new" [] (view-form)) (POST "/new" {params :params} (prn "params:" params)) (route

Missing form parameters in Compojure POST request

假如想象 提交于 2019-12-18 14:32:12
问题 I'm having problems getting the form parameters in the following Compojure example: (ns hello-world (:use compojure.core, ring.adapter.jetty) (:require [compojure.route :as route])) (defn view-form [] (str "<html><head></head><body>" "<form method=\"post\">" "Title <input type=\"text\" name=\"title\"/>" "<input type=\"submit\"/>" "</form></body></html>")) (defroutes main-routes (GET "/" [] "Hello World") (GET "/new" [] (view-form)) (POST "/new" {params :params} (prn "params:" params)) (route

Missing form parameters in Compojure POST request

隐身守侯 提交于 2019-12-18 14:32:07
问题 I'm having problems getting the form parameters in the following Compojure example: (ns hello-world (:use compojure.core, ring.adapter.jetty) (:require [compojure.route :as route])) (defn view-form [] (str "<html><head></head><body>" "<form method=\"post\">" "Title <input type=\"text\" name=\"title\"/>" "<input type=\"submit\"/>" "</form></body></html>")) (defroutes main-routes (GET "/" [] "Hello World") (GET "/new" [] (view-form)) (POST "/new" {params :params} (prn "params:" params)) (route

Clojure: sequence back to vector

大憨熊 提交于 2019-12-18 14:17:59
问题 How can I cast a sequence back to vector after a sequence producing operation (like sort)? Does using (vec..) on a sequence that was a vector is costly? One (bad?) possibility is creating a new vector out of sequence: (vec (sort [1 2 3 4 5 6])) I am asking because I need random access (nth ..) to huge sorted vectors - which are now huge sequences after the sort, with horrible O(n) random access time 回答1: From my own tests (nothing scientific) you may be better with working directly on arrays

Executing multiple statements in if-else without nullpointer exception

大憨熊 提交于 2019-12-18 13:52:46
问题 I'm trying to dig a little deeper into clojure and functional programming. At some point of my code I have a (def server (spawn-server)) . Now I want a short function for the REPL to check the state of this socket. This is what I have at the moment: (defn status [] (if server ( (println "server is up and running") (println "connections:" (connection-count server)) ) (println "server is down"))) If server is nil, everything works fine, but this is the output on the REPL if the server is