clojure edn function reader

659 views
Skip to first unread message

ronen

unread,
May 27, 2015, 9:02:30 AM5/27/15
to clo...@googlegroups.com
Hey, I'm looking for an edn data read for clojure functions (similar to https://github.com/Datomic/day-of-datomic/blob/master/resources/day-of-datomic/clojure-data-functions.edn)

Is there any known implementation? 

Thanks

Andy Fingerhut

unread,
May 27, 2015, 10:36:20 AM5/27/15
to clo...@googlegroups.com
I am not sure I understand your question.  I will answer some questions with some of the same key words in them, and then you can decide if one of them was close to yours :)

Clojure includes clojure.core/read [1] and clojure.core/read-string, which can read any Clojure code, but they can be dangerous in allowing side effects, including arbitrary code execution, so they are really only intended for reading from trusted sources (e.g. code or data your wrote yourself).

Since Clojure 1.5.1, it also includes clojure.edn/read [2] and clojure.edn/read-string.  Those are intended for reading data in edn format.  They have limitations, of which I don't remember them all, but IIRC at least one is that they cannot read back in records in some of the ways that Clojure code can print them out.  They work well when the data you have is just lists, vectors, sets, maps, and primitive types.

There is also the tools.reader library [3], which has its own implementations of all of the functions above, implemented in Clojure itself.

If none of those is what you are looking for, can you rephrase your question?

Andy




--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ronen

unread,
May 27, 2015, 12:14:40 PM5/27/15
to clo...@googlegroups.com
Ok ill expand the question a bit hoping to make it clearer :)

I'm well aware of read/read-string and the security implications they bring (not interested in those), 

Clojure EDN has support for literal tags and the ability of extending it with custom ones (records for example https://github.com/miner/tagged). 

I have a use case for storing functions in EDN and Iv found this, which seems to be an EDN form containing serialized Clojure functions for Datomic (which is closed source so I can't use its reader). 

My question is if there is any open source implementation, I can't seem to find one.

Thanks

Herwig Hochleitner

unread,
May 28, 2015, 12:39:45 PM5/28/15
to clo...@googlegroups.com
2015-05-27 18:14 GMT+02:00 ronen <nar...@gmail.com>:
Ok ill expand the question a bit hoping to make it clearer :)

Still not clear to me, but I'll try to expand a bit in the hope of showing what is unclear.

Clojure EDN has support for literal tags and the ability of extending it with custom ones (records for example https://github.com/miner/tagged). 

Right, so what additional functionality are you looking for then? Clojure is basically EDN + a bit of additional syntax, like anonymous fns #(), vars #', deref @, syntax-quote ` ~ ~@, discarded expressions #_, read eval #= and recently read cond #? I suppose you're not talking about that additional syntax, because then it wouldn't be EDN anymore.

I have a use case for storing functions in EDN and Iv found this, which seems to be an EDN form containing serialized Clojure functions for Datomic (which is closed source so I can't use its reader). 

As I implied above, Clojure code not utilizing any of the extra syntax is perfectly readable by any EDN implementation. As far as datomic #db/fn is concerned: If you pass clojure code in a string, I'm pretty sure it uses clojure.core/read to read it. If you pass forms, as in the example you posted, I guess it just uses those forms as they were and they will already have been preprocessed by the reader before being passed to the reader function of #db/fn, so again, clojure.core/read (normally).

My question is if there is any open source implementation, I can't seem to find one.

There are many readers capable of reading EDN (clojure.tools.reader, edn-java, haskell's edn, clojure.core/read, cljs reader, ...) some of those will also be able to read the extensions that make clojure syntax.

If you are looking for an encoding of clojure's syntax extensions into pure edn reader tags (as my crystall ball tells me you might be), I haven't encountered such a thing yet, even though it's conceivable.

kind regards

Gary Verhaegen

unread,
May 28, 2015, 1:14:41 PM5/28/15
to clo...@googlegroups.com
Not very clear to me either, but, in the spirit of reducing the field of possible understandings of the question: are you aware of eval? If so, can you reframe your question around it, i.e. what is it missing for your use-case or haw it's not a good fit? (If you are not yet aware of eval, know that it is just as bad as read security-wise.)

Or perhaps you already have a clear idea of what you would want to do with the data structures in your edn file, and you're actually asking about how to register your custom literal reader with the edn reader?
--

Leon Grapenthin

unread,
May 28, 2015, 5:06:37 PM5/28/15
to clo...@googlegroups.com
I don't know of any open source implementation of the Datomic function literal. It shouldn't be too difficult and a great excercise. 

However just using read and eval won't suffice. Datomic creates an anonymous namespace around for each of those functions with the specified requires and imports. Clojures namespace API is very mature and concise so an implementing it should be fun.

ronen

unread,
May 28, 2015, 7:44:04 PM5/28/15
to clo...@googlegroups.com
Gary your last comment hits what I look for exactly:

"If you are looking for an encoding of clojure's syntax extensions into pure edn reader tags (as my crystall ball tells me you might be), I haven't encountered such a thing yet, even though it's conceivable."

My use case is to pass functions in configuration files (thus effecting behavior), sending functions over the wire or storing them is another use case 

Its not enough to include the body, but also include required namespaces, params it expects etc..

Thanks


On Thursday, May 28, 2015 at 8:14:41 PM UTC+3, Gary Verhaegen wrote:
Not very clear to me either, but, in the spirit of reducing the field of possible understandings of the question: are you aware of eval? If so, can you reframe your question around it, i.e. what is it missing for your use-case or haw it's not a good fit? (If you are not yet aware of eval, know that it is just as bad as read security-wise.)

Or perhaps you already have a clear idea of what you would want to do with the data structures in your edn file, and you're actually asking about how to register your custom literal reader with the edn reader?

On Thursday, 28 May 2015, Herwig Hochleitner <hhochl...@gmail.com> wrote:
2015-05-27 18:14 GMT+02:00 ronen <nar...@gmail.com>:
Ok ill expand the question a bit hoping to make it clearer :)

Still not clear to me, but I'll try to expand a bit in the hope of showing what is unclear.

Clojure EDN has support for literal tags and the ability of extending it with custom ones (records for example https://github.com/miner/tagged). 

Right, so what additional functionality are you looking for then? Clojure is basically EDN + a bit of additional syntax, like anonymous fns #(), vars #', deref @, syntax-quote ` ~ ~@, discarded expressions #_, read eval #= and recently read cond #? I suppose you're not talking about that additional syntax, because then it wouldn't be EDN anymore.

I have a use case for storing functions in EDN and Iv found this, which seems to be an EDN form containing serialized Clojure functions for Datomic (which is closed source so I can't use its reader). 

As I implied above, Clojure code not utilizing any of the extra syntax is perfectly readable by any EDN implementation. As far as datomic #db/fn is concerned: If you pass clojure code in a string, I'm pretty sure it uses clojure.core/read to read it. If you pass forms, as in the example you posted, I guess it just uses those forms as they were and they will already have been preprocessed by the reader before being passed to the reader function of #db/fn, so again, clojure.core/read (normally).

My question is if there is any open source implementation, I can't seem to find one.

There are many readers capable of reading EDN (clojure.tools.reader, edn-java, haskell's edn, clojure.core/read, cljs reader, ...) some of those will also be able to read the extensions that make clojure syntax.

If you are looking for an encoding of clojure's syntax extensions into pure edn reader tags (as my crystall ball tells me you might be), I haven't encountered such a thing yet, even though it's conceivable.

kind regards

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to

For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.

ronen

unread,
May 28, 2015, 7:49:22 PM5/28/15
to clo...@googlegroups.com
Leon spot on, maybe ill get to implement such a thing :)
Reply all
Reply to author
Forward
0 new messages