any?

瀏覽次數:265 次
跳到第一則未讀訊息

coltnz

未讀,
2015年4月25日 晚上9:32:092015/4/25
收件者:clo...@googlegroups.com
Any reason why we don't have `any?`. Googled without much luck.
Trivially done as `comp boolean some` not doubt, but I know I use it more than not-any at least.
It's particularly useful as a composable `or`.


Timothy Baldridge

未讀,
2015年4月25日 晚上9:42:522015/4/25
收件者:clo...@googlegroups.com
But it's not really like `or

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



--
“One of the main causes of the fall of the Roman Empire was that–lacking zero–they had no way to indicate successful termination of their C programs.”
(Robert Firth)

Timothy Baldridge

未讀,
2015年4月25日 晚上9:43:552015/4/25
收件者:clo...@googlegroups.com
bleh, hit reply too fast. Or also returns the first true value, so (or false nil 42) returns 42. 

I guess I don't see when I'd use 'any?'

Ambrose Bonnaire-Sergeant

未讀,
2015年4月25日 晚上9:57:012015/4/25
收件者:clojure
Do you mean any? as in

(defn any? [p c]
  (boolean (seq (filter? p c))))

Thanks,
Ambrose

Devin Walters

未讀,
2015年4月25日 晚上10:06:282015/4/25
收件者:clo...@googlegroups.com
I think Ambrose is getting at what the original poster is after. I've written this a number of times, but seem to recall doing something like `(def any? (complement not-any?))`, though I'd need to go back and look.

`not-any?` is just `(comp not some)` under the covers, so I guess I'm not sure why `any?` would be out of bounds in core.

Cheers,
Devin

Alex Miller

未讀,
2015年4月25日 晚上10:21:262015/4/25
收件者:clo...@googlegroups.com
I think 'some' fills this role. Given truthy values, why do you need to use boolean with it?

Colin Taylor

未讀,
2015年4月26日 凌晨1:08:582015/4/26
收件者:clo...@googlegroups.com
So I was thinking of:

user=> (def any? (comp boolean some))
#'user/any?
user=> (any? true? [false true false])
true
user=> (any? even? [1 2 3])
true
; touch "3"
user=> (any? #(.exists %) [(file "1") (file "2") (file "3")])
true

Some motivations

- my main one is Java interop where use of non booleans would be weird
- similarly it is nicer for APIs to return booleans than a value that is kinda arbitrary and potentially non deterministic. I might recall Rich saying something like that..?
- the symmetry of `any` joining `not-any`, `every` and `not-every`.
訊息已遭刪除

Mark Engelberg

未讀,
2015年4月26日 凌晨4:24:242015/4/26
收件者:clojure
The main use case for the non-boolean aspect of `some` is when you use it to find the first element of a sequence that is in a particular set.

(some #{....} [....])

The idea is that when you get a "hit", the return value is the specific element of the *set* that matched.  This is a useful trick for turning values into something canonical that can be compared with your canonical values using identical?

=> (def a (with-meta [1 2] {:canonical true}))
=> (some #{a} [(list 1 2)])
[1 2]
=> (meta (some #{a} [(list 1 2)]))
{:canonical true}
=> (identical? a (some #{a} [(list 1 2)]))
true


On Sun, Apr 26, 2015 at 12:02 AM, Isaac Zeng <ndtm...@gmail.com> wrote:
some

James Reeves

未讀,
2015年4月26日 上午8:56:042015/4/26
收件者:clo...@googlegroups.com
On 26 April 2015 at 06:08, Colin Taylor <colin....@gmail.com> wrote:
So I was thinking of:
user=> (any? true? [false true false])
true
user=> (any? even? [1 2 3])
true
; touch "3"
user=> (any? #(.exists %) [(file "1") (file "2") (file "3")])
true

Those examples all work with `some` as well:

user=> (some true? [false true false])
true
user=> (some even? [1 2 3])
true
user=> (some #(.exists %) [(file "1") (file "2") (file "3")])
true

Some motivations

- my main one is Java interop where use of non booleans would be weird

That's a valid reason, but it's not a common enough case to justify its own function, IMO. Also, in cases where the type matters, having to add an explicit `boolean` onto the front makes the code clearer.
 
- similarly it is nicer for APIs to return booleans than a value that is kinda arbitrary and potentially non deterministic. I might recall Rich saying something like that..?

The `some` function isn't unusual. A lot of boolean logic functions in Clojure return a "truthy" or "falsey" value, rather than true or false explicitly.

user=> (and true 1)
1
user=> (and nil true)
nil
user=> (or true 1)
1
 
- the symmetry of `any` joining `not-any`, `every` and `not-every`.

That's true, but then you have two functions, `some` and `any?`, that do the same thing under most circumstances. That strikes me as somewhat confusing, especially as `any?` is essentially a niche-case operation.

- James

Colin Taylor

未讀,
2015年4月26日 下午5:57:492015/4/26
收件者:clo...@googlegroups.com、ja...@booleanknot.com
Oh uggh wheres the delete post button.  I'll blame the 3 mth old with reflux for that.
回覆所有人
回覆作者
轉寄
0 則新訊息