any?

265 wyświetleń
Przejdź do pierwszej nieodczytanej wiadomości

coltnz

nieprzeczytany,
25 kwi 2015, 21:32:0925.04.2015
do 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

nieprzeczytany,
25 kwi 2015, 21:42:5225.04.2015
do 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

nieprzeczytany,
25 kwi 2015, 21:43:5525.04.2015
do 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

nieprzeczytany,
25 kwi 2015, 21:57:0125.04.2015
do clojure
Do you mean any? as in

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

Thanks,
Ambrose

Devin Walters

nieprzeczytany,
25 kwi 2015, 22:06:2825.04.2015
do 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

nieprzeczytany,
25 kwi 2015, 22:21:2625.04.2015
do clo...@googlegroups.com
I think 'some' fills this role. Given truthy values, why do you need to use boolean with it?

Colin Taylor

nieprzeczytany,
26 kwi 2015, 01:08:5826.04.2015
do 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`.
Wiadomość została usunięta

Mark Engelberg

nieprzeczytany,
26 kwi 2015, 04:24:2426.04.2015
do 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

nieprzeczytany,
26 kwi 2015, 08:56:0426.04.2015
do 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

nieprzeczytany,
26 kwi 2015, 17:57:4926.04.2015
do clo...@googlegroups.com, ja...@booleanknot.com
Oh uggh wheres the delete post button.  I'll blame the 3 mth old with reflux for that.
Odpowiedz wszystkim
Odpowiedz autorowi
Przekaż
Nowe wiadomości: 0