1. Can someone explain vslice()'s error_control to me?

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

new topic     » topic index » view message » categorize

2. Re: Can someone explain vslice()'s error_control to me?

Sorry but I can't explain this "enhancement". In my opinion, not only is it overly complex, it is also a poor method to help users, and its poorly coded as well. In a word ... inappropriate.

I would have thought that this function should either return a valid "column" (vertical slice) or a meaningful error value. For example, if it returned the index of the first "row" that didn't have the requested column, it could be useful to the caller.

public function vslice(sequence source, integer colno) 
sequence ret 
 
    if colno < 1 then 
        return 0  -- Invalid column number requested 
    end if 
 
    ret = repeat(0, length(source))  -- Allocate space for column. 
 
    -- Look at each 'row' and pick out the value of the requested 'column'. 
    for row = 1 to length(source) do 
        if colno > length(source[row]) then 
                return row  -- Error. This row doesn't have the requested column. 
        end if 
 
        ret[row] = source[row][colno] 
 
    end for 
    -- All good. Return the vertical slice. 
    return ret 
end function 
new topic     » goto parent     » topic index » view message » categorize

3. Re: Can someone explain vslice()'s error_control to me?

DerekParnell said...

I would have thought that this function should either return a valid "column" (vertical slice) or a meaningful error value. For example, if it returned the index of the first "row" that didn't have the requested column, it could be useful to the caller.

I agree that your version seems like the logical progression of Aku's code that Phil posted. I cannot imagine any useful benefit to this "error_control" parameter. Can it be removed?

-Greg

new topic     » goto parent     » topic index » view message » categorize

4. Re: Can someone explain vslice()'s error_control to me?

I would like the original developer who added this modification to justify the reasoning behind it first, before we remove it...

So, I tracked down when this change was introduced, and by whom, to here: http://scm.openeuphoria.org/hg/euphoria/annotate/11f1a51b5793/include/sequence.e#501

It doesn't look like that's gonna happen, so I'm ok with just removing this.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu