clojure

Clojure: how to create a record inside a function?

久未见 提交于 2019-12-23 23:45:52
问题 In clojure, I would like to create a record inside a function. I tried: (defn foo [] (defrecord MyRecord [a b]) (let [b (MyRecord. "1" "2")])) But it causes an exception: java.lang.IllegalArgumentException: Unable to resolve classname: MyRecord Any idea? 回答1: The key points You should only use defrecord at top level. 1 So, if you do need a custom record type, you should define it outside of foo (at some point in your code which gets processed before foo 's definition). Otherwise, you could

Clojure let binding forms

佐手、 提交于 2019-12-23 23:25:48
问题 I'm trying to understand how let bindings work in closure in regards to maps. From my understanding let is followed by a vector where the first item is the symbol I want bound, followed by the value I want to bind it to. So (let [a 1 b 2] a) would give a the value of 1. So if I declare map such as (def people {:firstName "John" :lastName "Doe"}) And I want to bind the key firstName then this would be the correct form for a simple "Hello Person" (let [personName (:firstName people)] (str

How to set the status code in Compojure?

拜拜、爱过 提交于 2019-12-23 21:53:18
问题 I am writing a small website in Clojure and Compojure. I would like to set the HTTP response status for each request based on the data found or not found. The last call is the html5 macro that returns to the handler the html that needs to be sent back to the browser. Is it possible to set the HTTP response status somehow here? (ns myapp.views.layout (:require [hiccup.page :refer (html5 include-css include-js)])) (defn layout [title & content] (html5 (head title) (body content))) 回答1: If you

Slurping http://foobar.mp3 that redirects to http://fizzbar.mp3 in Clojure

不羁的心 提交于 2019-12-23 20:53:01
问题 I am trying to programatically download mp3 files from this rss feed. When I open an url such as: http://menlochurch.podbean.com/mf/feed/5gv2gb/170219_jortberg.mp3 it redirects to an url like: http://s62.podbean.com/pb/67f34563539acbe87b9566ecc5738d57/58aeff8e/data4/fs145/948579/uploads/170219_jortberg.mp3 If I curl the first url it downloads an empty file. If I curl -L the first url it correctly downloads the file. If I curl the second url it correctly downloads the file. If I slurp the

my own interpose function as an exercise

╄→尐↘猪︶ㄣ 提交于 2019-12-23 20:25:39
问题 I'm resolving a 4Clojure exercise, this exercise asks you to build your own interpose function. My answer follows: (fn my-interpose ([separator input] (my-interpose separator input nil)) ([separator input result] (if (empty? input) (reverse (rest result)) (my-interpose separator (rest input) (cons separator (cons (first input) result)))))) I'm doing these exercises to learn the language as I read a Clojure book. I will like to know the opinion about my code of people with an experience in the

Questions about Lists and other stuff in Clojure

左心房为你撑大大i 提交于 2019-12-23 20:23:48
问题 I have a few questions concerning Lists, classes and variables in clojure. This may seem quite stupid but how do I access elements in a List ? I'm coding a program that allows you to manipulate a phonebook; You can add an entry, delete one or print the information about one. This leads me to two questions : Is there a way to create a class "entry" that would contain "name" "adress" "phone number" variables ? or is that impossible in clojure (and functional programming in general ?) If I can't

How to get name of argument in Clojure?

杀马特。学长 韩版系。学妹 提交于 2019-12-23 20:18:08
问题 I would like to get the name of a var defined outside a function, from within a function. The name should be the name that I used at the scope of the original definition, not any nested bindings where I'm actually trying to use the name. So I would like to do something like (academic example): (defn f1 [x1] (println "hello, you passed var name >>" (get-var-name x1) "<<") (defn f2 [x2] (f1 x2)) (defn f3 [x3] (let [zzz x3] (f2 zzz)) (def my-var 3.1414926) (f3 my-var) user> hello, you passed var

What causes “java.lang.IllegalArgumentException: No value supplied for key”?

≡放荡痞女 提交于 2019-12-23 19:53:05
问题 I have code of the shape (let [{foo :foo} (make-foo)] ...) This code occasionally emits a java.lang.IllegalArgumentException: No value supplied for key: {:foo "foo" :other "other"} . I've seen Clojure : "java.lang.IllegalArgumentException: No value supplied for key:" when I changed require, however I haven't changed the require of my program since it last worked. What are the possible causes for the "No value supplied for key" exception? 回答1: This happens when you try to create a map from an

Validating multiple polymorphic values using Prismatic Schema

亡梦爱人 提交于 2019-12-23 19:39:44
问题 A little while ago I asked about simple polymorphic schemas, and the answer there worked well for a time. Now the map I wish to validate has an additional value which is dependent upon another key's value. A contrived example of the object: {:type :foo {:type :bert {:type :foo :foo-param :bar :bert-size :medium :foo-param :bar :method :baz :method :baz :method :bang :baz-rate :max} :baz-rate :max} :bangness :considerable} The discriminators here are :type and :method , each of which has its

trying to get leiningen user profile to run clojure-1.5.0

荒凉一梦 提交于 2019-12-23 19:27:43
问题 I have both clojure-1.4.0 and clojure-1.5.0 installed and working correctly. In my various project.clj files, I can set one or the other as a dependency, and leiningen picks up the stated version correctly. When I am not in a project directory, then lein repl always runs clojure-1.4.0. I tried to change the default by putting the following profiles.clj in my ~/.lein directory: {:user {:dependencies ^:replace [[org.clojure/clojure "1.5.0"]] :plugins [[lein-pprint "1.1.1"]]}} but lein repl ,