Is predicate a fn returning Boolean or just Truthy / Falsy?

Hi Clojuverse community,

Quick terminology question, if you have a bit of time to answer please.

It’s clear to me that a function that returns true / false is a Predicate and if it is named function the name should have question mark (zero?).

What about functions that returns truthy / falsy?

For example #(if (and (string? % ) (> (count %) 5)) % nil) that returns truthy input (string) or nil. Do we call that a Predicate? Should I name it long-string or long-string?.

Thank you :wink:

Rud

1 Like

Here’s your answer: Clojure - Frequently Asked Questions

More specifically, a trailing ? indicates that a predicate strictly returns a boolean result (true or false)

In practice, I find the predicate distinction of ? (only true|false) to be an artificial distinction in a language with truthiness. Maybe it matters for interop where you must pass a host’s true/false value, but for general usage, I haven’t really cared for a decade.

3 Likes

Ouh, RTFM ;-). Sorry about asking something from FAQ.

So the official answer is that both Truethy/Falsy function is Predicate and true/false function is also a Predicate that should have question mark when named.

Thank you.

Thank you @joinr . Does it mean that you like to add question mark to any Predicate (Boolean and Truthy/nil) or do you mean that don’t use question mark in your naming convention?

Thanks!

I tend to leverage the question mark for communication purposes or intent, not focused too much on return type. So yeah, I am a bit more relaxed in practice (or at least as relaxed as clojure’s notion of truth).

1 Like

If you use ? with predicates that return non-Boolean results, just be aware of contexts like some? which accept false but not nil – so there is a possibility that users of your code may assume that the result of your predicate is “some-safe”. It’s definitely an edge case, but it’s a subtle one.

Also, as a data point, a few predicates with ? introduced into clojure.core initially returned truthy/falsey and that was considered a bug and they were fixed to return strictly Boolean results.

5 Likes

Valid point. I don’t recall a situation where I would pass a predicate result to another predicate…like applying some? to the result of a predicate (although I could see maybe a situation where you’re mapping a predicate instead of using it like a filter…). I also am enough of a fossil that by the time some? snuck into the core, I had gone so long without it (or doing my own stuff), I never adopted it as an idiom :slight_smile: I keep finding new stuff in core like that.

1 Like

There’s also if-some and when-some :slight_smile:

No worries, I think it was a good question, and I wasn’t really sure if there was some guidelines about it, so I ended up in the FAQ (the style guide doesn’t cover this, I think). As @joinr said, it’s probably better to use the question mark if it clarifies the intent of your predicate, despite it not necessarily returning a boolean.

Yeah, you’re right. I actually read the community style guide before I asked :wink:

Yeah, but Sean also has a point that question mark initially introduced in clojure.core returning truthy/falsey was considered a bug. I think I’ll stick with question mark only for boolean returning functions but I’ll add “Predicate: some description” into Docstrings for truthy/falsey returning functions.

Thank you all :smiley:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.