exercism / r

Exercism exercises in R.
https://exercism.org/tracks/r
MIT License
27 stars 38 forks source link

Add `strain` exercise #366

Closed ErikSchierboom closed 1 month ago

colinleach commented 1 month ago

The exemplar is longer than it needs to be, though it took me a while to see how to improve it (too much Julia going on in my old, tired head recently). Suggested alternative:

keep <- function(input, fun) {
  if (is.null(input)) return(c())
  input[sapply(input, fun)]
}

I kind of feel there should be a one-liner using ifelse, but that struggles with a NULL on one branch and a vector on the other.

colinleach commented 1 month ago

After writing my previous comment, I realized there is no need to test for null input. Only the input[sapply(input, fun)] is needed, so it is a one-liner.

A reminder that I should never underestimate R!

ErikSchierboom commented 1 month ago

@colinleach That is really cool, but it goed against the instructions:

Keep your hands off that filter/reject/whatchamacallit functionality provided by your standard library! Solve this one yourself using other basic tools instead.

colinleach commented 1 month ago

OK, I see your point. A bit of a grey area in a context where this sort of thing is such a core part of the language, but not worth arguing about.