@phanthero has joined the channel
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!
andmap
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 :)
also all? would suggest a boolean return value, which is different from andmap
Sure. I wonder how often the return value of andmap is used in a non-boolean way.
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.
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.
my preferred names would be all-pass? / any-pass?
or all-match? / any-match?
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!
I feel like this should take some sort of match pattern
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.