clarity-lang / reference

The Clarity Reference
146 stars 34 forks source link

Slice function to extract subsequence #42

Open njordhov opened 3 years ago

njordhov commented 3 years ago

Extracting a subsequence in Clarity is inefficient as it requires iterating over all elements to build a new sequence. It also cannot be implemented as a general library function, but has to be implemented for each type of sequence and entry type, which may lead to duplicated code and less comprehensible code.

A slice function can be used as the basis for not only to get the nth item as in #39, but much other sequence functionality, such as to get the first, second, and last item, the rest of a sequence without the first item, butlast without the last item, taken items from a sequence, drop the n items, partition into subsequences, contains, split, and others.

The implementation can be optimized through structural sharing instead of relying on cloning of data from the original sequence.