clojure

How to convert map to URL query string in Clojure/Compojure/Ring?

风流意气都作罢 提交于 2020-01-14 07:23:09
问题 In Clojure / Compojure, how do I convert a map to a URL query string? {:foo 1 :bar 2 :baz 3} to foo=1&bar=2&baz=3 Is there any utility method to do this in compojure? 回答1: Yes, there is a utility for this already that doesn't involve Hiccup or rolling your own string/join/URLEncoder function: user=> (ring.util.codec/form-encode {:foo 1 :bar 2 :baz 3}) "foo=1&bar=2&baz=3" user=> Compojure depends on ring/ring-core, which includes ring.util.codec, so you already have it. 回答2: Something like:

Generating query clauses with korma and clojure

你说的曾经没有我的故事 提交于 2020-01-13 20:43:15
问题 I am trying to generate korma query conditions based on a map of columns and values that I pass into a function. I am finding that when an empty map is passed to korma's where: (select "things" (where conditions)) Generates queries with an empty WHERE which causes a SQL error: SELECT * FROM things WHERE () LIMIT 10 However using this form: (select "things" (if-not (empty? conditions) (where conditions))) Results in an error: "Wrong number of args (1) passed to: core$where" Is there an

Generating query clauses with korma and clojure

好久不见. 提交于 2020-01-13 20:40:43
问题 I am trying to generate korma query conditions based on a map of columns and values that I pass into a function. I am finding that when an empty map is passed to korma's where: (select "things" (where conditions)) Generates queries with an empty WHERE which causes a SQL error: SELECT * FROM things WHERE () LIMIT 10 However using this form: (select "things" (if-not (empty? conditions) (where conditions))) Results in an error: "Wrong number of args (1) passed to: core$where" Is there an

Exit Recur Loop in Clojure

旧街凉风 提交于 2020-01-13 19:01:11
问题 I'd like break out of the below loop and return the best-min-move when line 10 evaluates to true. I've looked at the output with print statements and when line 10 does evaluate to true, it finds the data that I'm looking for but continues to recur. In Clojure is there a way to stop the loop when a statement evaluates to true? Or should I be using something other than a loop recur? (defn minimax [board max-mark min-mark depth best-score] (loop [board board max-mark max-mark min-mark min-mark

Improving clojure lazy-seq usage for iterative text parsing

喜欢而已 提交于 2020-01-13 12:15:10
问题 I'm writing a Clojure implementation of this coding challenge, attempting to find the average length of sequence records in Fasta format: >1 GATCGA GTC >2 GCA >3 AAAAA For more background see this related StackOverflow post about an Erlang solution. My beginner Clojure attempt uses lazy-seq to attempt to read in the file one record at a time so it will scale to large files. However it is fairly memory hungry and slow, so I suspect that it's not implemented optimally. Here is a solution using

Clojure swing app startup time

元气小坏坏 提交于 2020-01-13 11:28:10
问题 I just started making a GUI app using clojure and seesaw. It does little more that create a JFrame and a couple components. Here's the code. The main function does nothing but call start-gui and exit as soon as it returns. (ns pause.gui (:use seesaw.core)) (native!) ; (javax.swing.UIManager/setLookAndFeel ; "org.pushingpixels.substance.api.skin.SubstanceGraphiteLookAndFeel") (def main-window (frame :title "Pause" :on-close :exit)) (def sidebar (listbox :model [])) (def main-area (text :multi

Difference between calling function and macro inside macro?

狂风中的少年 提交于 2020-01-13 10:03:02
问题 My puzzle is the following example: (defmacro macro1 [x] (println x)) (defn func1 [x] (println x)) (defmacro macro2 [x] `(macro1 ~x) (func1 x)) (defmacro macro3 [x] (func1 x) `(macro1 ~x)) (println "macro2") (macro2 hello) (println "macro3") (macro3 hello) Surprisingly, the output is: macro2 hello macro3 hello hello Why the output of macro2 and macro3 are different? In my understanding, all the calling of macro inside macro could be substituted with function (except for the reason of reuse).

Difference between calling function and macro inside macro?

孤街醉人 提交于 2020-01-13 10:01:12
问题 My puzzle is the following example: (defmacro macro1 [x] (println x)) (defn func1 [x] (println x)) (defmacro macro2 [x] `(macro1 ~x) (func1 x)) (defmacro macro3 [x] (func1 x) `(macro1 ~x)) (println "macro2") (macro2 hello) (println "macro3") (macro3 hello) Surprisingly, the output is: macro2 hello macro3 hello hello Why the output of macro2 and macro3 are different? In my understanding, all the calling of macro inside macro could be substituted with function (except for the reason of reuse).

Enums and Clojure

夙愿已清 提交于 2020-01-13 07:33:13
问题 In the Java/C world, people often use enums. If I'm using a Java library which using enums, I can convert between them and keywords, for example, using (. java.lang.Enum valueOf e..., (aget ^"[Ljava.lang.Enum;" (. e (getEnumConstants)) i) , and some reflection. But in the Clojure world, do people ever need anything like an enum (a named integer) ? If not, how is their code structured that they don't need them ? If yes, what's the equivalent ? I sense I'm really asking about indices (for

Structural Sharing in Clojure

无人久伴 提交于 2020-01-12 19:06:08
问题 I'm unclear about structural sharing in Clojure. Below is a function xconj taken from the Joy of Clojure (Great book BTW). ;;Building a naive binary search tree using recursion (defn xconj [t v] (cond (nil? t) {:val v :L nil :R nil} (< v (:val t)) {:val (:val t) :L (xconj (:L t) v) :R (:R t)} :else {:val (:val t) :L (:L t) :R (xconj (:R t) v)})) If one defines two trees t1 and t2 as shown below. (def t1 (xconj (xconj (xconj nil 5) 3) 2)) (def t2 (xconj t1 7)) It is clear that the Left subtree