Live data from Hacker News

Schema for Clojure(Script) Data Shape Declaration and Validation

blog.getprismatic.com

21–30 of 58 posts

Re: Schema for Clojure(Script) Data Shape Declaration and Validation

#21
post #19

The opening paragraphs really resonated with me including that code example that can be replaced with any function I wrote yesterday. A lot of my time in Clojure is spent re-reading and repl-evaluating my function implementations just to remind myself what its symbols look like. Even code I wrote an hour ago. Often I stoop to the level of caching my findings in a docstring/comment. (defn alpha-map-sum-combining-thing…

I do this a lot:

"Often I stoop to the level of caching my findings in a docstring/comment."

I am not ashamed of this. When my co-workers have to read what I wrote, this kind of information makes clear what the function does. If I don't do this, then they will eventually figure it out by running it at the REPL and examining the input and the output, but if I document that in the docstring, I have saved them a lot of time. I also make it possible for them to actually read the code -- otherwise they can't really read it, expect by repeating it at the REPL.

Re: Schema for Clojure(Script) Data Shape Declaration and Validation

#22
I feel your argument against just using Scala was weak, but you guys are obviously smart and feel building Schema was the right choice. So could you elaborate a bit on the decision to stick with Clojure when your problem domain lends itself to types?

Re: Schema for Clojure(Script) Data Shape Declaration and Validation

#23
post #19

The opening paragraphs really resonated with me including that code example that can be replaced with any function I wrote yesterday. A lot of my time in Clojure is spent re-reading and repl-evaluating my function implementations just to remind myself what its symbols look like. Even code I wrote an hour ago. Often I stoop to the level of caching my findings in a docstring/comment. (defn alpha-map-sum-combining-thing…

I do this a lot: "Often I stoop to the level of caching my findings in a docstring/comment." I am not ashamed of this. When my co-workers have to read what I wrote, this kind of information makes clear what the function does. If I don't do this, then they will eventually figure it out by running it at the REPL and examining the input and the output, but if I document that in the docstring, I have saved them a lot of…

Yeah, you're right. I would love if every Clojure function came with an example input and output.

What I meant to suggest is that my docstring example is something that could be expressed in code.

Re: Schema for Clojure(Script) Data Shape Declaration and Validation

#24

I feel your argument against just using Scala was weak, but you guys are obviously smart and feel building Schema was the right choice. So could you elaborate a bit on the decision to stick with Clojure when your problem domain lends itself to types?

My guess would be they want to continue using a Lisp-style language.

Re: Schema for Clojure(Script) Data Shape Declaration and Validation

#26
post #19

The opening paragraphs really resonated with me including that code example that can be replaced with any function I wrote yesterday. A lot of my time in Clojure is spent re-reading and repl-evaluating my function implementations just to remind myself what its symbols look like. Even code I wrote an hour ago. Often I stoop to the level of caching my findings in a docstring/comment. (defn alpha-map-sum-combining-thing…

This has been my experience also, even as I'm simultaneously learning clojure and building my own webapp.

Re: Schema for Clojure(Script) Data Shape Declaration and Validation

#27
I've been working on a similar idea for replacing types. If you can specify how to take apart a type, name the parts and put them back together you can then get validators, pattern matching, recursive-descent parsing with backtracking, generic traversals, lenses and generators more or less for free. By using simple data-structures to describe the types and compiling them to clojure at the call site you can have first-class types without being penalised in performance.

It's still work in progress but there are working examples as of https://github.com/jamii/strucjure/commit/e0e56a25c1b880c382...

    (using strucjure.sugar
      ;; define a pattern
      (def peano-pattern
        (graph num ~(or ~succ ~zero)
               succ (succ ~num)
               zero zero))
      (comment ;; desugars to 
        {'num (->Or (->Node 'succ) (->Node 'zero))
         'succ (list 'succ (->Node 'num))
         'zero 'zero})

      ;; define a view over that pattern
      (def peano->int
        (view peano-graph {'succ (fnk [num] (inc num))
                           'zero (fnk [] 0)}))

      (peano->int 'zero) ;; => 0
      (peano->int '(succ (succ zero))) ;; => 2
      (peano->int '(succ (succ succ))) ;; => throws MatchFailure
    )
                           
It's similar to the old ideas at http://scattered-thoughts.net/blog/2012/12/04/strucjure-moti... but significantly simpler. I'm hoping to be able to release at least the core functionality in a few weeks.

Re: Schema for Clojure(Script) Data Shape Declaration and Validation

#28
post #27

I've been working on a similar idea for replacing types. If you can specify how to take apart a type, name the parts and put them back together you can then get validators, pattern matching, recursive-descent parsing with backtracking, generic traversals, lenses and generators more or less for free. By using simple data-structures to describe the types and compiling them to clojure at the call site you can have first…

Very cool. Also check out the implementation of encapsulation in John Shutt's Kernel Lisp: http://web.cs.wpi.edu/~jshutt/kernel.html

Re: Schema for Clojure(Script) Data Shape Declaration and Validation

#29
post #19

The opening paragraphs really resonated with me including that code example that can be replaced with any function I wrote yesterday. A lot of my time in Clojure is spent re-reading and repl-evaluating my function implementations just to remind myself what its symbols look like. Even code I wrote an hour ago. Often I stoop to the level of caching my findings in a docstring/comment. (defn alpha-map-sum-combining-thing…

Thanks! I definitely have personally run into the issue a lot and our Clojure teams at Prismatic struggled with commenting discipline. We find schemas are easier to maintain and apply, plus they save a lot of time during dev and testing.

Re: Schema for Clojure(Script) Data Shape Declaration and Validation

#30
post #23

Earlier quoted context omitted.

I do this a lot: "Often I stoop to the level of caching my findings in a docstring/comment." I am not ashamed of this. When my co-workers have to read what I wrote, this kind of information makes clear what the function does. If I don't do this, then they will eventually figure it out by running it at the REPL and examining the input and the output, but if I document that in the docstring, I have saved them a lot of…

Yeah, you're right. I would love if every Clojure function came with an example input and output. What I meant to suggest is that my docstring example is something that could be expressed in code.

This would let you run tests, too: https://en.wikipedia.org/wiki/Doctest

On the other hand, why not just make it statically typed and list the types for each function?

Post reply on HN