I could not find any example but this is the sort of thing I want to get working:
proc bitWise*[T](buff: openArray[T], action: expr, mask: openArray[T]): seq[T] =
var c = items #or some custom iterator
result = buff.mapMe(it action c(mask)) # mapMe is an openArray friendly mapIt clone
# how do I pass xor, and, or etc?
var dataMasked = bitWise (data, `xor`, mask)
Thanks.
I think you need to use a Template or an anonymous function to do what you want:
template testA[T](a: T, action: untyped, b: T): T = action(a,b)
echo testA(1, `xor`, 2)
proc testB[T](a: T, action: proc(a,b: T):T , b: T): T = action(a,b)
echo testB(1, proc(a, b: int): int = a xor b, 2)
Usually, you would define action as proc(l,r: T): T. But this does not work for xor because of this paragraph in the manual (see also):
Assigning/passing a procedure to a procedural variable is only allowed if one of the following conditions hold:
* The procedure that is accessed resides in the current module.
* The procedure is marked with the procvar pragma (see procvar pragma).
* The procedure has a calling convention that differs from nimcall.
* The procedure is anonymous.
xor is not in the current module, not marked with {.procvar.}, has the calling convention nimcall and is not anonymous, so it may not be passed to a procedural variable.
You can, however, use a template:
template bitWise*[T](buff: openArray[T], action: untyped, mask: openArray[T]): seq[T] =
var result = newSeq[T]()
for i in 0..<buff.len:
result.add(action(buff[i], mask[i]))
result
echo bitWise([1, 0, 0], `xor`, [1, 0, 1])