#absInt over vector?

1 messages · Page 1 of 1 (latest)

uneven yarrow
#

Is there a way to run a scalar function on every element of a vector and put the result into a new vector? I'd expect like a @map but I don't see one.

#

(besides doing the for loop)

loud relic
#

zig and functional programming styles are kind of orthogonal

#

zig is pretty solidly imperative

uneven yarrow
#

well, there's a @reduce 🙂

loud relic
#

that applies to vectors, which map quite directly to hardware

#

and unlike arrays, vectors only hold stuff like integers, pointers, floats - not composite types

uneven yarrow
#

I'll be more specific - it'd be nice to have a vectorized std.math.absInt, but there's no such thing. So I'm wondering what the nicest way to deal with this is.

#

there's a vectorized std.math.sign, for example.

loud relic
#

well, there is @fabs, but that only applies to vectors of floats

uneven yarrow
#

indeed

loud relic
#

at any rate, there's maybe a way to do it with @select

errant vale
#

You would select

solar bear
#

sign() does some select() stuff, so you could go off that

errant vale
#

I like to model my vector stuff into data transformations and reducing the movements

uneven yarrow
#

ty

loud relic
#

you'd probably have something like

fn absVector(x: anytype) {
    const Child = @typeInfo(@TypeOf(x)).Vector.child;
    const pred = x > @splat(x.len, @as(Child, 0));
    const y = -x;
    return @select(Child, pred, x, y);
}
#

std.math.absInt could probably be doing this

uneven yarrow
#

yeah, that's approximately what I'm doing now. I guess maybe absInt isn't doing this because of fear of overflow.

#

I suppose you'd just check all the elements first.

loud relic
#

well, Idk about fear

uneven yarrow
#

sure, s/fear/possibility/ 🙂

loud relic
#

ye

uneven yarrow
#

absInt over vector?