Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are takeR, dropR and splitAtR missing from Data.Sequence?

Data.Sequence has takeWhileR and dropWhileR for efficient deconstruction of Seqs from the right. However, takeR, dropR and splitAtR are conspicuously absent. take and drop are implemented in terms of splitAt. So, do finger trees not admit an efficient splitAtR or was this functionality not included for some other reason?

(Separate but somewhat related question: Would a naive dropR implementation in terms of viewR perform decently well?)

This question is based on containers-0.5.6.3.

like image 875
Jannis Limperg Avatar asked Jun 21 '15 16:06

Jannis Limperg


1 Answers

length is O(1), so splitAt suffices to define everything you need, in an efficient way.

 splitAtR i s = splitAt (length s - i) s
 takeR i s = snd $ splitAtR i s
 dropR i s = fst $ splitAtR i s

According to the docs, splitAt costs O(log(min(i,length s-i))), so by symmetry splitAtR costs the same (just an additional +O(1), which we can neglect).

like image 175
chi Avatar answered Sep 28 '22 17:09

chi