TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Schema for Clojure(Script) Data Shape Declaration and Validation

130 pointsby sorenmacbethover 11 years ago

14 comments

jamiiover 11 years ago
I&#x27;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.<p>It&#x27;s still work in progress but there are working examples as of <a href="https://github.com/jamii/strucjure/commit/e0e56a25c1b880c38259f2bf59768afc7350fa9c" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jamii&#x2F;strucjure&#x2F;commit&#x2F;e0e56a25c1b880c382...</a><p><pre><code> (using strucjure.sugar ;; define a pattern (def peano-pattern (graph num ~(or ~succ ~zero) succ (succ ~num) zero zero)) (comment ;; desugars to {&#x27;num (-&gt;Or (-&gt;Node &#x27;succ) (-&gt;Node &#x27;zero)) &#x27;succ (list &#x27;succ (-&gt;Node &#x27;num)) &#x27;zero &#x27;zero}) ;; define a view over that pattern (def peano-&gt;int (view peano-graph {&#x27;succ (fnk [num] (inc num)) &#x27;zero (fnk [] 0)})) (peano-&gt;int &#x27;zero) ;; =&gt; 0 (peano-&gt;int &#x27;(succ (succ zero))) ;; =&gt; 2 (peano-&gt;int &#x27;(succ (succ succ))) ;; =&gt; throws MatchFailure ) </code></pre> It&#x27;s similar to the old ideas at <a href="http://scattered-thoughts.net/blog/2012/12/04/strucjure-motivation/" rel="nofollow">http:&#x2F;&#x2F;scattered-thoughts.net&#x2F;blog&#x2F;2012&#x2F;12&#x2F;04&#x2F;strucjure-moti...</a> but significantly simpler. I&#x27;m hoping to be able to release at least the core functionality in a few weeks.
评论 #6337654 未加载
评论 #6338322 未加载
danneuover 11 years ago
The opening paragraphs really resonated with me including that code example that can be replaced with any function I wrote yesterday.<p>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&#x2F;comment.<p><pre><code> (defn alpha-map-sum-combining-thing &quot;TODO: Think of better name. Example input: {:a 1 :b 2} Example output: {[:a :b] 3}&quot; [alpha-map] ...) </code></pre> Sometimes I can spot abstractions and patterns and protocols that unify a namespace of functions so that their inputs&#x2F;outputs are obvious, but often I can&#x27;t.<p>This kind of tool is essential for me.
评论 #6336294 未加载
评论 #6339844 未加载
评论 #6337716 未加载
评论 #6336934 未加载
ambrosebsover 11 years ago
core.typed author here.<p>It might be easy to consider Schema as &quot;competitor&quot; to core.typed. In fact, it&#x27;s the opposite: once they play nicely together they will form a formidable bug-fighting, finely-documenting team. :)<p>core.typed has accurate compile time checking, and Schema gives an expressive contracts interface for runtime checking.<p>Once they understand each other, you can start pushing and pulling between static and dynamic checking by using both libraries to their strengths.<p>Currently, core.typed requires all vars have top level annotations. This is partly because there is no way to recover type information once inside a function body. However, if we have an entire namespace using Schema liberally, we can use schemas to recover information!<p>This means we can lean on schemas for most annotations, and rely on core.typed to catch arity mismatches, bad arguments to clojure.core functions, potential null-pointer exceptions and many more nasties at compile time.<p>Then you might start adding static annotations or removing schemas, depending on the kind of code your dealing with. You might do some &quot;static&quot; debugging to ask whether a schema is needed to prevent a type error. core.typed would also let you know when your contracts are insufficient to rule out type errors. Really, you&#x27;re free to use both tools as you&#x27;d like.<p>Schema looks very nice, thanks for open sourcing it Prismatic folks!
评论 #6343001 未加载
ariaover 11 years ago
Author here. Happy to answer any questions or comments.
评论 #6335705 未加载
评论 #6335175 未加载
评论 #6335222 未加载
评论 #6335926 未加载
评论 #6335596 未加载
评论 #6340461 未加载
olenhadover 11 years ago
This is really brilliant, and damned useful. Reminds me of how type declarations provide self documenting code in Haskell. Thanks alot!
评论 #6335228 未加载
dsabaninover 11 years ago
Great stuff, thanks for releasing this. Definitely going to use it soon.
评论 #6335220 未加载
JackMorganover 11 years ago
What timing! I have been making this exact thing myself.<p>Would you be interested in pull requests making your api simpler? Maybe allow parameters to not have to have shapes? Perhaps allowing a syntax that allows a simple way to shapes in the meta alongside the ability to change the signature for people who don&#x27;t want to couple too tightly to the library? When I showed mine to my local user group last month, that was one of their biggest requests (that I was in the middle of on mine but would be happy to attempt in yours).<p>Check my (simpler and more immature) library here <a href="https://github.com/steveshogren/deft" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;steveshogren&#x2F;deft</a>
评论 #6343017 未加载
评论 #6339756 未加载
评论 #6339760 未加载
RichMorinover 11 years ago
Interesting work. BTW, there&#x27;s a typo: &quot;shee here&quot; should be &quot;see here&quot;.
评论 #6335758 未加载
dustingetzover 11 years ago
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?
评论 #6336653 未加载
minerover 11 years ago
Very interesting. I have a somewhat related project called Herbert, which attempts to define a schema language for EDN.<p><a href="https://github.com/miner/herbert" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;miner&#x2F;herbert</a>
mrcactu5over 11 years ago
Newbie here, mostly familiar with OOP.<p>What is the difference between how Clojure and other functional programming languages declare types.
评论 #6336250 未加载
jared314over 11 years ago
This looks like it occupies the same space as core.contracts and core.typed. Is the main benefit, over those two, cljs support?
评论 #6338887 未加载
krosaenover 11 years ago
pretty cool - I&#x27;ve used something similar for python JSON validation:<p><a href="https://code.google.com/p/jsonvalidator/" rel="nofollow">https:&#x2F;&#x2F;code.google.com&#x2F;p&#x2F;jsonvalidator&#x2F;</a><p>but love the idea of something for validating nested data structures as well.
评论 #6338805 未加载
rsamvitover 11 years ago
This is excellent. I used to occasionally use assertions to accomplish the same thing.