clojure

Clojure: How to find out the arity of function at runtime?

孤者浪人 提交于 2019-12-20 08:56:07
问题 Given a function object or name, how can I determine its arity? Something like (arity func-name) . I hope there is a way, since arity is pretty central in Clojure 回答1: The arity of a function is stored in the metadata of the var. (:arglists (meta #'str)) ;([] [x] [x & ys]) This requires that the function was either defined using defn , or the :arglists metadata supplied explicitly. 回答2: Sneaky reflection: (defn arg-count [f] (let [m (first (.getDeclaredMethods (class f))) p (

Server programming with Clojure

我与影子孤独终老i 提交于 2019-12-20 08:49:32
问题 How to implement 10k connections echo server in Clojure? clojure.contrib.server-socket is not the answer since it crates a new OS thread for every connection. 回答1: The great thing about Clojure is you have all these great libraries out there for the JVM like netty, which are highly optimized, configurable, and well thought out. Something like this should get you going: (ns netty (:gen-class) (:import [java.net InetSocketAddress] [java.util.concurrent Executors] [org.jboss.netty.bootstrap

Where to put specs for Clojure.Spec?

…衆ロ難τιáo~ 提交于 2019-12-20 08:46:39
问题 So, I'm diving deeper and deeper into Clojure.Spec . One thing I stumbled upon is, where to put my specs . I see three options: Global Spec File In most examples, I found online, there is one big spec.clj file, that gets required in the main namespace. It has all the (s/def) and (s/fdef) for all the "data types" and functions. Pro: One file to rule them all Contra: This file can be big Single Responsibliy Principle violated? Specs in production namespaces You could put your (s/def) and (s

How does functional programming apply to simulations?

↘锁芯ラ 提交于 2019-12-20 08:46:18
问题 Besides the general question in the title, How do functional programmers and functional languages approach the domain of simulations, which seem to be most naturally handled by object-oriented languages? Are there open-source examples of complex simulations written in a (mostly) functional style? What changes of perspective would an OO-programmer need, in order to approach simulations from a functional paradigm? I'm asking this while learning how Clojure's creator Rich Hickey specifically

Compojure routes with different middleware

北战南征 提交于 2019-12-20 08:27:58
问题 I'm currently writing an API in Clojure using Compojure (and Ring and associated middleware). I'm trying to apply different authentication code depending on the route. Consider the following code: (defroutes public-routes (GET "/public-endpoint" [] ("PUBLIC ENDPOINT"))) (defroutes user-routes (GET "/user-endpoint1" [] ("USER ENDPOINT 1")) (GET "/user-endpoint2" [] ("USER ENDPOINT 1"))) (defroutes admin-routes (GET "/admin-endpoint" [] ("ADMIN ENDPOINT"))) (def app (handler/api (routes public

How to print elements in a vector of vectors

有些话、适合烂在心里 提交于 2019-12-20 07:51:07
问题 I have a vector of vectors, and I want to print elements in each vector I tried the pprint but did not work as wanted This is the vector of vectors I wish to print: [["+" "+" "+" "#" "!" "-" "#" "#" "#" "-" "-" "-" "-"] ["!" "#" "+" "+" "+" "#" "+" "+" "+" "-" "#" "#" "-"] ["#" "#" "#" "#" "+" "#" "+" "#" "+" "#" "-" "#" "#"] ["+" "+" "+" "#" "+" "+" "+" "#" "+" "#" "-" "-" "-"] ["+" "#" "+" "#" "#" "#" "#" "+" "+" "-" "#" "#" "-"] ["+" "#" "+" "+" "+" "+" "+" "+" "#" "-" "-" "-" "-"] ["+" "#

Clojure - Splitting a vector

纵然是瞬间 提交于 2019-12-20 07:13:56
问题 If I have two arguments [[1 2] [3 4]] and [5 6], how can I get to [[1 5] [2 6] [3 5] [4 6]]. I thought I may have to use for so I tried, (for [x [[1 2] [3 4]]] (for [xx x] (for [y [5 6]] [xx y]))) But it returned ((([1 5] [1 6]) ([2 5] [2 6])) (([3 5] [3 6]) ([4 5] [4 6]))) Any help would be much appreciated. Thanks 回答1: (mapcat #(map vector % [5 6]) [[1 2] [3 4]]) or using for : (for [c [[1 2] [3 4]] p (map vector c [5 6])] p) 回答2: If I understood your question correctly, your solution is

Arithmetic expression simplifier in Clojure

馋奶兔 提交于 2019-12-20 06:27:32
问题 I want of create simplifier of arithmetic expressions in Clojure, and I am new to this language. So for ex.: in: "2x + 6y - (12 + (5x - 3y)) + 4" simplified: "- 3x + 9y - 8". So my attempt is to parse expression with regexp into hierarchical vector of nested expressions like this: ["5x-3y" "12 + <?>" "2x + 6y - <?> + 4"] ;; <?> is evaluated item from previous step and then evaluate them in sequence. I am feeling like it's hack, some advice would be helpful. 来源: https://stackoverflow.com

why does clojure's map println only works in repl?

前提是你 提交于 2019-12-20 05:45:32
问题 I use lein new app test-println to create a clojure app and launch the repl with lein repl , then I enter (map println [1 2 3 4 5 6]) and get the expected result: test-println.core=> (map println [1 2 3 4 5 6]) 1 2 3 4 5 6 (nil nil nil nil nil nil) However if I add (map println [1 2 3 4 5 6]) to the end of src/test_println/core.clj : (ns test-println.core (:gen-class)) (defn -main "I don't do a whole lot ... yet." [& args] (println "Hello, World!") (map println [1 2 3 4 5 6])) lean run prints

File is downloaded instead of being displayed in the browser

你。 提交于 2019-12-20 05:19:16
问题 I have created a brand new app from the luminus app template using lein new luminus my-app +postgres +auth +cljs +swagger . In the generated file src/clj/my_app/routes/home.clj the following compojure route is created: (GET "/docs" [] (response/ok (-> "docs/docs.md" io/resource slurp))) When I try to access localhost:3000/docs the file is simply downloaded instead of displayed in the browser. It happens both with Chrome and Safari. It seems related to ring.util.http-response/ok since I can