Node: Efficient Sequence Functions, Previous: Searching Sequences, Up: Sequences



Efficient Sequence Functions

All sequence functions must dispatch dynamically on the specific type of sequence passed into the sequence function call. This is a very small overhead especially in Lpp. However for absolute efficiency any sequence function can be typed for a specific sequence type and this dispatching overhead will not be incurred. For example in

     length(sequence)
     

if we only expect sequence to be an Lpp String then

     length(the(String, sequence))
     

would cause no sequence dispatching to take place. Or the user could define something like

     inline let length_string(let sequence) {
       return length(the(String, sequence);}
     inline let length_list(let sequence) {
       return length(theOrNil(Cons, sequence);}
     

and then use length_string when it is expected that sequence will be a string and length_list when expecting a list.

Since the whole idea of these specific type calls is absolute efficiency, when calling a sequence function using this method and the function overloads arguments as C++ types or Lpp types then only the C++ types are allowed. For example if the normal sequence function can take an int or an Lpp Integer then in the specific type function arguments only the int will work

     subseq(asThe(String, sequence), 3)          // Works
     subseq(asThe(String, sequence), iL(L(3)))   // Works
     subseq(asThe(String, sequence), L(3))       // Will not even compile
     

notice that the last is not allowed by this rule and will not work or even compile.

Frankly, unless absolute efficiency is desired, the overhead on sequence dispatching is so small that this specific dispatching method is rarely worth doing.