uiua icon indicating copy to clipboard operation
uiua copied to clipboard

Introduce takewhile/dropwhile

Open tmpr opened this issue 6 months ago • 5 comments

A primitive to idiomatically look for the first elements which fulfill some condition would be useful. For example, in the (contrived) example of finding the first elements which are smaller than some number could benefit from this.

tmpr avatar Jun 12 '25 10:06 tmpr

Linear forms of them can be written relatively easily for the times where they're needed: one way, using do

They probably don't deserve primitives

amatgil avatar Jun 22 '25 22:06 amatgil

This is only fast since the loop in the example terminates fast. Filtering for elements < 10'000 has TW! slower than ↙/+⊸<₁₀₀₀₀. For 100k it is even slower. See here.

I argue that for something like this, an operation which is always fast is useful, and a way to do that is with a primitive. An argument for why this is sensible is that multiple languages have such a primitive.

tmpr avatar Jun 23 '25 06:06 tmpr

An alternative thing would be to have some primitive to find the first (or last) index at which some proposition holds. This would then easily be composable with take to get a takewhile.

tmpr avatar Jun 23 '25 07:06 tmpr

I wonder if something like this should be an optimization on an existing pattern rather than its own primitive.

kaikalii avatar Jul 02 '25 02:07 kaikalii

Also the do loop for this sort of thing is pretty simple: ⍢⊸+₁(<100)

kaikalii avatar Sep 26 '25 17:09 kaikalii