Can someone explain vslice()'s error_control to me?
- Posted by petelomax Oct 23, 2014
- 1236 views
I am assuming that someone saw aku's version from 2005:
global function vslice(sequence s, atom colno) sequence ret ret = {} for i = 1 to length(s) do ret = append(ret, s[i][colno]) end for return ret end function
and thought they'd "enhance" it.
public function vslice(sequence source, atom colno, object error_control=0) sequence ret integer substitutes, current_sub if colno<1 then crash("sequence:vslice(): colno should be a valid index, but was %d",colno) end if ret = source if atom(error_control) then substitutes = -(not error_control) else substitutes = length(error_control) current_sub = 1 end if for i=1 to length(source) do if colno>=1+length(source[i]) then if substitutes=-1 then crash("sequence:vslice(): colno should be a valid index on the %d-th element, but was %d", {i,colno}) elsif substitutes=0 then return ret[1..i-1] else substitutes -= 1 ret[i] = error_control[current_sub] current_sub += 1 end if else ret[i] = source[i][colno] end if end for return ret end function
I understand missing columns => crash, as the default behaviour and personally I'd have left it at that.
I don't get why anyone might want a short slice, an example of that being useful for something might help.
I certainly don't get why anyone would want to interpolate successive elements of error_control within/as substitutes for missing elements of source, and potentially get a partially-short slice back, and I guess that you are expected to test length(res)=length(source) to figure out whether the latter happened. Or is one expected to pass repeat(default,length(source)) to error_control to ensure logical behaviour?
Pete