r/Clojure • u/yogthos • Mar 01 '17
Clojure's missing piece (Specter 1.0 release)
http://nathanmarz.com/blog/clojures-missing-piece.html10
u/flightlessbird Mar 01 '17
This is great. Lens like functionality (I like the calling them optics) is a great fit as a library, and every immutable language needs a good implementation. Great stuff.
6
u/freakhill Mar 02 '17
I have been using specter for a long time but have been burnt a few times by deprecated documentation not marked as such, and broken APIs. I am quite happy it finally reached 1.0
Do you intend to follow R.H.'s idea of "growing software"? I am concerned whether I can expose your APIs to my users.
6
u/nathanmarz Mar 02 '17
Maintaining backwards compatibility is the #1 priority for the project moving forward.
5
u/daveliepmann Mar 02 '17
Congratulations on the release, Nathan.
Since this is a proselytizing document, I suggest that the introduction of novel syntax without explanation could be improved. For instance, in the first example in the README:
(transform [MAP-VALS ALL MAP-VALS even?] inc data)
What are these all-caps entities? From where should I infer the rules and consequences of this novel syntax? I assume they're like keywords; is there a list of them somewhere?
Another point is relevant for a lot of Clojure library authors: please don't :refer :all these new functions. Namespaces are the signposts in this new land your users are exploring. It's not super necessary for simple examples like the above transform, but consider the blind insertion of filterer or walker into README examples. You're forcing people to stop their train of thought and ask: Are these clojure.core functions I'm not familiar with? Where do I inspect the source of this strange function? One assumes it works <like this>...but does it?
Also, maybe this is a formatting issue, but a lot of end-parens are on their own line in the blog post.
4
u/0x800703E6 Mar 02 '17 edited Mar 02 '17
What are these all-caps entities?
They're vars. You can find them in the API docs. I would like to know, why they're all-caps.
(sr/transform [sr/map-vals sr/all sr/map-vals even?] inc data)would feel much nicer. (it's really unfortunate, that specter clashes with spec).
9
u/a_kaydash Mar 02 '17
I'm very glad Specter exists, I find it's navigation concept both powerful and intuitive. There's not always a need to use Specter but when data structure modification gets even slightly involved Specter is indispensable. Thank you, Nathan.
The majority of responses in this thread are surprising to me. Someone lays out their reasoning for the solution they've implemented and willingly contributed, yet the thread is filled with comments about the tone in which a reader interpreted the article, or uninformed comments about how the library doesn't solve a real problem. If you have no use for the library because you've found your one true path, that's great, maybe consider not writing about it in the margins though.
3
u/theoriginalmatt Mar 02 '17
This is great news.
Specter is one of my favorite libraries in any language, and the first library I add as dependency to most Clojure and ClojureScript projects.
It works especially great with a library like re-frame, which stores all state in a global atom. Instead of having to figure out how to get data out of the global db variable, and then how to put it back again, all I now have to worry about is how I want the data to change.
It allows you to decouple your data transformation functionality from the structure of the data - if the data structure changes, you only update your navigation paths, and don't touch your transformation code. That leads to very clear, easy to read code.
What I really like about it is that the more you use it, the more powerful it becomes, as you discover new ways in which the navigation paradigm can be used. You start using it for nested data structures, but then you realise you can use sub sequence navigation to move items around in vectors, then you realise you can dynamically build up paths to create dynamic queries, then you find out you can use it to zip around trees, and suddenly you are using it almost everywhere.
Just wanted to put this up here and thank the author, great work on reaching version 1.0.
2
u/hessammehr Mar 03 '17
Nathan, I've been using Spectre in some of my code (just hobby projects) and it's made life a lot easier. Often I need to manipulate nested maps where, like you said, the code would be pretty unreadable in vanilla Clojure.
Thank you for Spectre!
-4
Mar 01 '17 edited Mar 02 '17
beyond basic use cases Clojure fails to provide an elegant API for immutable programming
Can you give an example of how the same can be accomplished by other languages?
This deficiency in Clojure manifested itself in my projects as unreadable code, complexity, lots of bugs, and bad performance.
Just as in any other language.
Consider this code which increments the even numbers in a map of vector of maps (...) This is totally unreadable.
Doesn't mean it can't be improved.
Specter provides an abstraction called "navigation"
That's awesome. I just think most of the preceding whining "deficiency" talk is nonsense and unfair to the language.
Edit: replaced the offensive word whining with something else that doesn't hurt some people's feelings.
13
u/nathanmarz Mar 01 '17
In a mutable language you don't have to worry about reconstructing the parts of the data structure you didn't change (by definition). The need to reconstruct is a new problem brought about by accepting immutability as a superior approach.
Even if every other language suffered from the same problems, that doesn't mean Clojure doesn't have a deficiency. That said, as mentioned in the post Haskell has lenses which allow for elegant transformations.
3
Mar 01 '17
Characterizing the lack of navigation facilities as a deficiency of Clojure itself is silly. I would agree if implementing such facilities required a huge amount of effort. That's not the case. Clojure provides all the primitives necessary to that end.
In a mutable language you don't have to worry about reconstructing the parts of the data structure you didn't change (by definition). The need to reconstruct is a new problem brought about by accepting immutability as a superior approach.
In a mutable language you don't worry about reconstructing data structure, but you have to worry about mutability. With Clojure you only have to worry about the types resulting from the reconstruction, not with the reconstruction process itself. I'd say the latter is much less of a problem than the former.
Haskell has lenses which allow for elegant transformations.
I think Clojure was designed in a way that anyone can easily extend it and come up with new facilities like lenses for instance. The author of the article seems to miss that point.
9
u/nathanmarz Mar 01 '17
Characterizing the lack of navigation facilities as a deficiency of Clojure itself is silly. I would agree if implementing such facilities required a huge amount of effort. That's not the case. Clojure provides all the primitives necessary to that end.
Actually, Specter was incredibly difficult to implement. It's easy to code the basic functionality, but getting to the level of optimal performance is a whole different story.
In a mutable language you don't worry about reconstructing data structure, but you have to worry about mutability. With Clojure you only have to worry about the types resulting from the reconstruction, not with the reconstruction process itself. I'd say the latter is much less of a problem than the former.
Not sure what you mean by the distinction between "reconstruction types" and "reconstruction process". The fact of the matter is the idiomatic way to transform compound data structures in Clojure is inherently complex and 3-10x slower than optimal.
I think Clojure was designed in a way that anyone can easily extend it and come up with new facilities like lenses for instance. The author of the article seems to miss that point.
Again, Specter was anything but easy. Data structure manipulation is one of the most core things you do with a language. That idiomatic Clojure is both complex and slow for it makes the language deficient.
-4
Mar 02 '17
Data structure manipulation is one of the most core things you do with a language.
Yeah, say that to the creators of C.
it makes the language deficient
I don't buy that. It's like saying Java is deficient because it doesn't come with persistent data structures. That's not a deficiency in itself, it's simply not a concern to core Java, and it's left to 3rd party library developers to provide that. Of course it would be great if it came with that stuff but I don't curse Java for not providing that.
1
u/j3alive Mar 04 '17
The ability to easily edit deeply nested data structures in Clojure often becomes a concern for many Clojure programmers. Especially for new-comers or others who haven't yet trained their brains to work around the deficiency.
7
u/john_atx Mar 01 '17
The author of this article has written an extremely powerful library, which makes dealing with nested data structures in Clojure much simpler. I was very happy when I found this library, Specter, because it solved exactly my pain points in a very easy to learn and use manner. I think this library is very underrated, and I consider it a core library in any project I start up.
3
Mar 01 '17
The author of this article has written an extremely powerful library
I totally agree with you. My point is all the deficiency nonsense is just a distraction.
2
u/john_atx Mar 01 '17
Maybe I'm not smart enough to be a good Clojure programmer, but before Specter, I spent a lot of time trying how to work the mechanics of updating a value in the middle of a deeply nested data structure.
Clojure is my first immutable language, so any other language I've used, updating a value in the middle of a nested data structure was trivial. In Clojure it was very difficult. It made me much less productive vs. other languages. I would call it a deficiency in the core library, that this is hard.
2
Mar 01 '17
Can you give me an example? I thought update-in was supposed to do that job.
ps: it's an honest question, I'm not trying to dispute your argument.
3
1
u/john_atx Mar 01 '17
I don't have a good instance in mind, but basically update-in works fine for one value with a static path. But when you are doing more complicated things where you have to conditionally search for the node(s), Specter makes this much easier.
3
u/hagus Mar 01 '17
What effect are you seeking from the characterization of his language as "whining"? I challenge you to articulate and clarify your concern using nonviolent language, to ensure those with dissenting or divergent opinions feel safe to discuss them openly. We can't learn from one another when unnecessary barriers like this are erected.
-9
Mar 01 '17
Looks like there are some emotionally vulnerable individuals in the audience.
8
u/hagus Mar 02 '17
Emotionally vulnerable is a fine way of describing me. I actually try very hard to let other people's emotions reach me, so that I can better understand the way they communicate and how I'm reacting. For instance, when you accuse me of being a snowflake, after accusing the OP of pointless whining, it would be easy to make a counterattack and score points. Instead I feel concern for how defensive you're feeling, so much so that the challenge to communicate in a nonviolent manner was also met defensively.
The challenge still stands - you will find that to control that defensiveness and empathize with other people's emotions requires you to be made of something far closer to steel, than snow.
-5
Mar 02 '17
the challenge to communicate in a nonviolent manner
Can you hear that? I'm playing the world's smallest violin.
2
u/freakhill Mar 02 '17
Stop being an asshole.
2
u/pihkal Mar 02 '17
He gets off on it because he's a troll. Click on his name to see his other comments.
3
u/RodeoMonkey Mar 01 '17
I can definitely see one.
-8
Mar 01 '17
I'm not surprise considering the time we're living where people get offended for nothing. I call it The Age of Snowflakes :)
4
u/pihkal Mar 02 '17
"Nothing"? You're the one who seemed to take criticism of Clojure personally, and lobbed insults at the author by accusing him of "whining".
In contrast, the author (/u/nathanmarz) graciously replied to you while not taking offense, and you didn't even seem to notice it was him. (Based on saying "The author of the article seems to miss that point" to the actual author of the article.)
The only one offended over nothing here is you.
1
Mar 02 '17 edited Mar 02 '17
You're the one who seemed to take criticism of Clojure personally
LOL, since when pointing out nonsense is the same as taking criticism personally?
the author graciously replied to you while not taking offense
I didn't mean any offense to anyone to begin with. You're overreacting.
The only one offended over nothing here is you.
I'm not offended, look at my smiley face :)
edit: spelling
2
u/pihkal Mar 02 '17
And now we're into the "u mad bro"/"it wasn't serious" phase... I've looked over your comments and you seem to like trolling and stirring up shit while adding little.
Argue your positions if you're willing, but if you have nothing intelligent to say, keep quiet.
0
Mar 02 '17
[deleted]
2
Mar 02 '17
The article was okay until it makes a bogus claim which I describe as "unfair to the language". Why you interpret that as a sign of personal offense is beyond me. Apparently to try make me fit into the description of snowflake. LOL!
1
u/nonrecursive Mar 02 '17
So... I guess you declined to accept the challenge
1
Mar 02 '17
What challenge? To replace "whining" with something else that doesn't offend a bunch of people? I can do that.
1
u/Daegs Mar 02 '17
So, your strategy is to put down and demean anyone that disagrees with you?
People aren't offended by you, they are telling you that you are wrong. That doesn't make them victims, that just makes them capable of forming an intelligent argument, which you respond to with childish insults.
You are the perfect example of projection, you insult others with all your own faults, but you're too dimwitted to see it. Hilarious
0
Mar 02 '17
So, your strategy is to put down and demean anyone that disagrees with you?
Nope.
You are the perfect example of projection, you insult others with all your own faults, but you're too dimwitted to see it.
Actually I'm having a good laugh at all the overreaction over the use of the "whining" word.
32
u/halgari Mar 02 '17
Honest feedback. It's language like "Clojure's glaring hole", "Clojure's missing piece" and "every programmer should use this on every project", saying the code you've written should be learned by all functional programmers, or even the suggestion of doing a ":use all..." at the top of a file that discourages me from wanting to use Specter.
It's cool tech and all, but the article reads in such a way to infer that to be a successful programmer you need a library like this, if that's the case...I guess I've been writing toy code for the past 5 years.
But setting that aside, I'm puzzled by a few aspects of this article: I don't care that a given data-structure retains its type when passed through map. I structure my code in such a way that I transform it once or twice before writing it to disk, so I don't pull apart a map and reassemble it multiple times. This matters because it appears that this is a major feature of Specter, creating maps in the absolute fastest way...but that's never the bottleneck in the software I work on, it's always in the bad algorithms and O-notation oversights of myself and clients I work with.
As I said in the Clojure mailinglist, 90% of the time what I really need from something like Specter is joins, recursive rules, grouping, etc. Very rarely do I need to dive 10 layers deep in a map an tweak one value. Most of the time I need to dive that deep and perform a join with some other part of the data. And that's only when I work with XML. 99% of the time, my hash-maps are one level deep, maybe two. Especially with qualified keywords, my hash-maps are even shallower.
So yeah, if I hit any situation where I needed something like Specter I would have considered it...but articles like this just leave a bad taste in my mouth and removed any motivation I had to investigate the library further. And that's rather sad, since it looks like cool tech.