6 ms·
Clojure: `every-pred` and `some-fn`
- FPGAhacker 7y ago> Clojure has a bunch of functions with every and some in their names, which can be a bit confusing. In some? and some-> it refers to a value being something other than nil, whereas in some and some-fn it means “the first one that applies” I haven’t used any of the “some” functions much, but it seems to me you could read this was “the first one that is not nil” which would be consistent.
- alexott 7y agoManning’s “Clojure. The essential reference”[1] has quite good coverage of these functions. [1] https://www.manning.com/books/clojure-the-essential-reference https://www.manning.com/books/clojure-the-essential-referenc...
- alexott 7y agoFor interested - this blog is a part of Planet Clojure: http://planet.clojure.in/ http://planet.clojure.in/
- drcode 7y agoThe thing to understand about Clojure is that its creator/maintainer believes software tools should almost never deprecate features. Because of this, there are a few esoteric functions and features that were created in the language along the way that will likely always remain in the language- The two examples given by OP arguably fall into that category, though I'm sure someone in this thread will attempt to argue that every-pred & some-fn are essential features for the core language. Other oddities include juxt, areduce, fnil, nthnext, derive, seque, and by now arguably the whole "ref" memory system (since Software Transactional Memory has pretty much gone by the wayside as a desirable programming feature) Of course, Rich Hickey has also been very good at deciding which features to put into the language in the first place, so the number of legacy oddities is pretty tiny anyway, fewer than most languages.
- thom 7y agoI don't see why these are so weird, logical composition of predicates is really common.
- drcode 7y agoHmm... I guess that's a valid point, though I would never use them for logical composition simply because the names of these functions really hurt readability: They are basically higher-order versions of "and" and "or", maybe if they had those words in their name they would be less cumbersome.
- disconcision 7y agoI think 'every' and 'some' are reasonable analogues, but the choice to make the implementations and hence their suffixes asymmetric wrt 'truthiness handling' is confusing to me, especially since these functions have existed (symmetrically) for a while in various schemes under the names 'conjoin' and 'disjoin'
- drcode 7y agoI agree that I'd be far more likely to use these if they were named "conjoin" and "disjoin".
- TeMPOraL 7y agoThese names are even worse. I know Clojure likes this style, but to me, "conjoin" is just a fancy way of writing "join" if you want another distinct symbol naming the same, very generic concept. "some" and "every" create nicely readable code. "every-pred" and "some-fn" could be called "satisfies-every" and "satisfies-some", respectively.
- fogus 7y agoI wrote both of these functions and conjoin and disjoin were definitely considered but were ultimately dismissed to avoid confusion with `conj` and `disj`. The chosen names were meant to echo `some?` and `every?`.
- wellpast 7y agoThis is great and now these functions are now square in my radar, as well. As far as his example goes, this IS how you model data: (def people [{:name "Elsie" :admin? true :mod? true} {:name "Lin Shiwen" :nickname "Rocky" :mod? true}]) Notice the lack of a "fill-in" static ADT form. No types here -- no static types NOR no ":type" field. Instead, we say what we know about our data items and we omit what we don't know. Just the facts, ma'am. And then Clojure gives us the obscenely simple tools to deal. It will forever confound me why some developers/PLs wish to model data in static, inflexible, a priori structures -- given the overtly clear development efficiency and simplicity of NOT doing so (and not HAVING to do so.)
- jjnoakes 7y agoI find one's point of view on the matter is largely colored by one's experience. For example, I personally love static types. In your example I would prefer modelling people as a list of structs, each one with a required "name" (string), an optional "nickname" (string option), and required boolean flags for mod and admin. Why would I prefer it to be that way? Because a huge number of typos I might introduce in my project are caught before I even try to run something. I've been bitten once too many times by a typo somewhere (like looking up the "nick" property instead of "nickname") and having the program raise an error after it's already ran for a while. I can't stand that.
- bjconlan 7y agoThis can all be covered by more abstractions though via some schema/spec/type validation process. Ide integration is the interesting problem here (and typescript does this beautifully) but I agree horses for courses (and experience). I think the idea for using the keyword to hold the nullability of a field interesting, I thought this was more of a meta attribute in clj? In saying that I actually find this style of keywording way easier to read.
- wellpast 7y agoIt is probably true that a majority today favors static types, but I don't think this a subjective issue if the problem is maximizing developer throughput over time. Working on statically typed code bases that model things the way you are describing substantially increases the effort and time it takes to iterate. So while you may prefer them (per your experience or what else), there is still the fact that this way of development is suboptimal with respect to delivery throughput over time. Of course this is arguable (and is a very heavily argued topic) and the common retort is "to each is own" that isn't sufficient to me. One might prefer to work in assembly language (per experience or predisposition) but there is still a general statement that can be made that this is intrinsically a suboptimal way to work if delivery throughput is your goal. I believe that a similar claim can be made to differentiate working in static ADT v. dynamic Map types when modeling business domains. More food for thought. The typo problems you mentioned are solved by static types but at what cost? And is the CBA there? Especially considering that there are other ways to "solve" the typo problem that do not come with the heavy tax burden of statically typed ADTs.
- 77544cec 7y agoFor those that are interested, I gathered in a gist some functional combinators I have been writing over the years. As a personal name convention, I use '|' (the piping character) as a suffix to name any function returning a function, which includes function combinators. Thus the naming of these functions becomes straighforward: 'every-pred' becomes 'and|' while 'some-fn' turns into 'or|'. Example: (filter (and| pos? even?) nums) (filter (or| neg? odd?) nums) More: https://gist.github.com/TristeFigure/acd689f3c57e840ebb9f8a65a2052e49 https://gist.github.com/TristeFigure/acd689f3c57e840ebb9f8a6...
- dimovich 7y agoThank you for sharing this! Another useful library with "missing" functions is Medley[0] by James Reeves. [0] https://github.com/weavejester/medley https://github.com/weavejester/medley