In the Common Lisp world it's common for predicates to return a useful
value instead of T, when applicable.  It seems possible the same principle
could apply to clojure.

>From CLtL2 <http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node69.html>:

Often a predicate will return nil if it ``fails'' and some useful value if
it ``succeeds''; such a function can be used not only as a test but also
for the useful value provided in case of success. An example is
member<http://clhs.lisp.se/Body/f_mem_m.htm>
.





On Thu, Apr 17, 2014 at 10:20 AM, Colin Yates <colin.ya...@gmail.com> wrote:

> My 2p - I interpret the contract as being boolean.  Truthy values are
> 'polymorphically' equivalent*1 so sure.  The concern would be people
> relying on the implementation and treating the values as none-truthy (i.e.
> in your example relying on the fact it is a string being returned, so (=
> "someString" (is-nsf-code? "someString")))
>
> I should reduce it to 1p really as I am only contributing in an abstract
> design sense, not in the specifics of the coding standards you mentioned.
>
> *1 If this doesn't hold (i.e. caller depends upon receiving a
> java.lang.Boolean or associated primitive) then I think the caller has
> bigger problems.
>
>
> On Thursday, April 17, 2014 5:33:42 PM UTC+1, Sean Corfield wrote:
>>
>> The library coding standards[1] say:
>>
>> * Use '?' suffix for predicates.
>>   - N.B. - predicates return booleans
>>
>> and the community Clojure style guide[2] says:
>>
>> * The names of predicate methods (methods that return a boolean value)
>> should end in a question mark. (i.e.even?).
>>
>> Both of these imply that if you have a function that returns a boolean
>> (and that is intended for use as a predicate), it should be named to end in
>> '?'. Fair enough.
>>
>> My question is about the reverse implication:
>>
>> * Should a function whose name ends in '?' return a (strict) boolean
>> value?
>>
>> Looking at the docstrings of a random selection of functions found by
>> (apropos "?"), they all seem to return specifically true or false. I did
>> not do an exhaustive check.
>>
>> Is the intent that foo? implies a result of true or false - or could foo?
>> return any truthy / falsey value (and therefore any Clojure value).
>>
>> Concrete example that spurred this discussion from some code at work:
>>
>> (defn is-nsf-code?
>>   "Given an error code, return truthy if it is NSF."
>>   [code]
>>   (#{"BE1" "BE2"} code))
>>
>> Clearly the result here could be nil or a string but it's definitely
>> meant to be used as a predicate. Similarly:
>>
>> (defn nsf?
>>   "Given the result of an SBW sale, return true if it failed with NSF."
>>   [result]
>>   (and (= "failure" (:result result))
>>        (some is-nsf-code? (:errors result))))
>>
>> Again, the result could be false or nil or a string but is meant to be
>> used as a predicate.
>>
>> As an aside, for core.typed, we annotate the first as [String -> Boolean]
>> with ^:no-check so it type checks as a true/false predicate and then we
>> annotate the second as [SBWResult -> (Nilable Boolean)] and that's all
>> fine... but is it "good style"?
>>
>> [1] http://dev.clojure.org/display/community/Library+Coding+Standards
>> [2] https://github.com/bbatsov/clojure-style-guide#naming
>>
>> Sean Corfield -- (904) 302-SEAN
>> An Architect's View -- http://corfield.org/
>>
>> "Perfection is the enemy of the good."
>> -- Gustave Flaubert, French realist novelist (1821-1880)
>>
>>
>>
>>  --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to