Fork me on GitHub

I have some user config values where I want to let the user define key maps - I was going to store it as a map in an rc file that I am pulling into my runtime via (read-string (slurp "/path/to/file")) - however, none of the following seems to work out to invoke the call in code when read like this: `


{:a some-call-in-my-ns
 :b #(println "test")


if I try ((:a file-def)) or resolve or apply it doesn't seem to work


ah, seems to just need to be wrapped in eval


@m131 :a and :b are bound to functions in this case, not the result of calling those functions. do you want {:a (some-call-in-my-ns)}?


read-string shouldn't resolve symbols to functions in your ns, it only reads

Clojure 1.9.0
+user=> (read-string "+")
+user=> (type (read-string "+"))
+user=> (= '+ (read-string "+"))
+user=> ('+ 1 1)


This is the source code of library kce-clj. I want to use this function write-file. I'm confused on the writer. What argument should I pass in as writer? Here is my try: `


(defn- write-to-org-file
  (with-open [w ( "")]
    (orgmode/write-file w "")))


Hi, when using s/fdef to spec a function how do you specify :args for a function that doesn’t take any arguments?


@andersmurphy I think you don't need to specify it

Alex Miller (Clojure team)12:09:43

you don’t have to specify it, but (s/cat) works


So if I omit :args stest/check fails with a :no-args-spec message. But if I use the :args empty? it seems to work. (s/cat) works too. Thanks for the help. 🙂

Alex Miller (Clojure team)14:09:06

Ah, for check you will need it (because it uses the args generator to invoke). I will note that in fp, 0 arg functions are unusual because there is exactly one input and therefore exactly one output (and thus it can be a constant instead) OR its stateful, in which case a spec may be weird.


Yeah that’s a good point. I don’t see it coming up often. It was an impure function where part of the output was random. Probably didn’t need a spec. But I’m using spec everywhere at the moment to get a feel for it 😄. Thanks again for the help.


Hello, how to possible put in the split function (clojure.string/split text-here #"<<regex variable depend on input>>" a variable to the regex part?


you can use re-pattern

👍 4



is it common to see spec used for external input validation?


@lockdown- You mean for things like REST API parameters? Or web form input parameters? We use it for that all the time.


(let [params (s/conform ::api/some-spec (:params req))]
  (if (s/invalid? params)
    (make-response (s/explain-data ::api/some-spec (:params req)))
    (process params)))
Something like that.

😎 8
👍 12
Caio Guedes13:09:44

Cool! 😄 @seancorfield I’m curious about write spec… If I would write a specific spec for each form or not, or just use the same spec that describe my entity domain…


That would depend on several things. First, do your web forms map 1:1 onto your domain model? If not, you need different specs. Second, web form input is always strings so you either need a spec that accepts string and "succeeds" if those strings can be converted to the appropriate data types -- and then actually parse the strings into data, or you need to write that coercion into your specs (which we do) which means they take strings as inputs and produce various data types -- and those spec probably aren't suitable for your domain model (which likely should not have strings in several fields!).


So we have "API specs" that accept strings and produce strings, numbers, Booleans, dates, etc; and then we have "domain specs" that use exactly the data types we need in the domain. We often use the latter to guide our persistence functions (using s/form on the domain specs to get the list of keys in maps and then transform those to SQL entity names -- replacing - with _ -- so we can auto-generate CRUD functions from domain specs via macros).


(That requires your database schema and your domain model are 1:1 which is not the case for all our tables so we also have custom transformation code for going between the domain model and the database and back)

Caio Guedes18:09:38

This is really interesting. I will give a try in this way. Thanks so much!


I think spec is good for that, yes.


How do you guys englishify the output of explain-data


@jaihindh.reddy I’ve recently started using and some simple functions for validating maps where each key is a field in a form

👏 8

this seems more practical. Expound generate weird responses sometimes.


@jaihindh.reddy We have a custom mapper function from explain-data to our domain (to produce specific numeric error codes -- and we have localized error messages based on those codes).

👍 8

That custom mapper understands the kinds of spec failures we can have, and has some heuristics built into it for our domain.


If I have two maps and want to join them by a map key's value, is there a easy way to do that?

(def m1 {:a 1 :stuck_out_tongue: 2 :c "match"})
(def m2 { :e "match" :f 1})
;; result would be {:a 1 :stuck_out_tongue: 2 :c "match" :f 1}


In this case if m1 :c contents matches m2 :c contents then add key :f to m1


@sjharms are you looking for merge?

=> (merge {:a :foo} {:a :bar :b :baz})
{:a :bar, :b :baz}


I think I might be looking at merge or merge-with but I am interested in comparing the key's value when merging


@sjharms are you interested in anything other than taking one over the other?


Potentially a set of keys. I think what I am asking is, can I join maps like I can join SQL tables? Ie (select * from m1,m2 where m1.c = m2.c)


Out of curiosity, how does one document the shape/structure of their data structures when just using maps? In languages w/ classes, the class definition is mostly self documenting, but w/ maps, do you just rely on the “create” function? Show an example in comments? Examine it in the REPL?

Alex Miller (Clojure team)21:09:51

The clojure.set ns has some basic relational algebra fns for sets of maps


In particular clojure.set/join looks useful


Thank you, that is what I am looking for


@risinglight spec is often used for documenting data structures in Clojure, as well as other run-time checking that you can opt into.

Cheese Cake Aficionado22:09:45

eyy you're here, neat

Cheese Cake Aficionado22:09:56

im the person you just told about this on discord fyi


ah, i thought so. nice!

Cheese Cake Aficionado22:09:35

that is one cursed emoji

💯 12

spec looks really awesome, but also has a learning curve. I was wondering what other simpler options might be until I get around to getting good at spec

Cheese Cake Aficionado22:09:12

does anyone know of a good tutorial for getting started with seesaw?


@sasha504 have you checked out the wiki? a REPL tutorial is linked there

Cheese Cake Aficionado22:09:36

I followed that but I'm not sure how to put it all together


@risinglight I think anything that lets you describe, document, and/or validate the shape/structure of your data structures is going to have a learning curve. Spec is built-in and well-documented (and is increasingly going to be "the way" that this is done -- reflected in books and code). Prior to spec, folks might have pointed you at Schema -- which has a similar learning curve -- or even core.typed (which has an even steeper learning curve).


Is there somebody can help test following code? I have different result on my machine. When I run lein repl under project root, and execute following test code works fine. But in Emacs CIDER [C-M-x] evaluate code report error: ParseException Unparseable date: "Tuesday, November 22, 2016 10:50:09 AM" java.text.DateFormat.parse ( test code:

(import 'java.util.TimeZone)
(import 'java.text.SimpleDateFormat)

(defn- parse-dt-
  [dt tz_id]
  (let [tz  (java.util.TimeZone/getTimeZone tz_id)
        fmt (doto (java.text.SimpleDateFormat. "EEEEEEEEEEEE, MMMMMMMMMMMM dd, yyyy hh:mm:ss aaa")
              (.setTimeZone tz))]
    {:timestamp (.parse fmt dt)}))

(def tz_id (.getID (java.util.TimeZone/getDefault)))
(def tz (java.util.TimeZone/getTimeZone tz_id))

(parse-dt "Tuesday, November 22, 2016 10:50:09 AM" "UTC")
(parse-dt- "Tuesday, November 22, 2016 10:50:09 AM" tz_id)

Cheese Cake Aficionado23:09:37

I'd like to convert a atom of a list to JSON, how could I do this?

Cheese Cake Aficionado23:09:50

(def list (atom ["Jeff" "John" "Ann"]))
(println (json/write-str list))

Cheese Cake Aficionado23:09:56

and list needs to be an atom as I need to update it


you need to deref your atom with either deref or @ when you pass it to json/write-str

👍 4

Gets the value out of the atom

Cheese Cake Aficionado23:09:24

well it worked, thank you

Cheese Cake Aficionado23:09:55

how can I create and MD5 hash from a string?


there’s not a built-in clojure function for it, but you can either use Java interop or a library

Cheese Cake Aficionado23:09:43

i tried a method I found on stackoverflow but it requires writing java and I dont like it

Cheese Cake Aficionado23:09:52

what library would you recommend


just googling I found this one seems popular ¯\(ツ)


what are you using MD5 for, if I may ask?

Cheese Cake Aficionado23:09:49

who doesn't like a good ol fashioned checksum

Cheese Cake Aficionado23:09:52

pear.core=> (:require 'digest)
pear.core=> (digest/md5 "hello")

CompilerException java.lang.RuntimeException: No such namespace: digest, compiling:(/tmp/form-init3043024428222783456.clj:1:1) 
pear.core=> (digest/md5 "hello")

CompilerException java.lang.RuntimeException: No such namespace: digest, compiling:(/tmp/form-init3043024428222783456.clj:1:1)


when in a REPL, you’ll want to use the form (require 'digest) - no :


the keyword form (:require ...) is only used inside of (ns pear.core ...) forms

Cheese Cake Aficionado23:09:06

ok, now it doesnt work in my actual program

Cheese Cake Aficionado23:09:10

(ns pear.core
  (:require [ :as json])
  (:require 'digest))


(ns pear.core
  (:require [ :as json]


you don’t have to wrap it in brackets like [digest] but I do out of habit


it makes it easier to add an :as d or something later

Cheese Cake Aficionado23:09:53

(ns pear.core
  (:require [ :as json])


what’s the error?

Cheese Cake Aficionado23:09:03

Caused by: java.lang.RuntimeException: No such var: clojure.core/digest


:thinking_face: that doesn’t make much sense to me. did you reload your ns?

Cheese Cake Aficionado23:09:54

im just calling lein run each time

Cheese Cake Aficionado23:09:40

sorry, im not used to JVM or functional languages


can you paste the whole error?


it’s okay 😄 clojure is weird even for people who have experience in both!


:thinking_face: it’s working for me when I require it into a REPL

Cheese Cake Aficionado23:09:19

yeah it worked in REPL


just confirming, in your code above you’re missing a ) at the end of the (ns ... form


was that a copy+paste error or is that how it is in your file?

Cheese Cake Aficionado23:09:21

heres what i have now

Cheese Cake Aficionado23:09:31

(ns pear.core
  (:require [ :as json])

Cheese Cake Aficionado23:09:57

put a paren in the wrong place lmao

Cheese Cake Aficionado23:09:21

i hav map, how can I access item in it


a few different ways. I like to use this site for a quick reference on those kinds of things: