phanthero
2020-10-29 09:02:47

@phanthero has joined the channel


joshua.e.cottrell
2020-10-29 13:59:18

Is there a #lang racket function like all or every that tests each list item with a predicate and returns #t if they’re all true and #f if any are false? I could write it like: (define (all pred xs) (foldl (lambda (current soFar) (and current soFar)) #t (map pred xs))) (or many other ways; I’m not really interested in golf right now) but I’d rather use a “native” function if possible. Or maybe there’s a way to simplify the above to something more like this (not so much to shorten it but to pass and around as a function): (define (all pred xs) (foldl and #t (map pred xs))) Thanks for the help!


laurent.orseau
2020-10-29 14:00:45

andmap


badkins
2020-10-29 14:47:38

Maybe all? and any? would be better names, but I just read the rationale for the andmap and ormap names in the docs, and it’s reasonable :)


laurent.orseau
2020-10-29 14:52:38

also all? would suggest a boolean return value, which is different from andmap


badkins
2020-10-29 14:58:22

Sure. I wonder how often the return value of andmap is used in a non-boolean way.


dedbox
2020-10-29 15:47:51

I use andmap pretty often to check if a property holds for the elements of a list while caching the final result. It’s handy when the list is a message; for example, validating an HTTP request, then discarding the envelop and keeping just the payload.


dedbox
2020-10-29 15:52:32

I also use the various for forms, like for/and, for/or, for/first , for/last , for/fold , when the property-checking code isn’t compact and doesn’t need its own function.


notjack
2020-10-29 22:47:54

my preferred names would be all-pass? / any-pass?


notjack
2020-10-29 22:48:21

or all-match? / any-match?


badkins
2020-10-29 23:13:09

Naming is so subjective :) I prefer Racket over Ruby in just about every way, but I think Ruby’s Enumerable class got this right with all? and any? - concise and to the point. After reading the rationale for naming andmap and ormap - the names are growing on me!


samdphillips
2020-10-29 23:55:37

I feel like this should take some sort of match pattern


rokitna
2020-10-29 23:59:43

I built my habits in Groovy which has .every and .any and Arc which has all and some, so the names I’ve reached for by default have been all and any for a long time.