push_element/2 - efficient sliding window

Bjorn Gustavsson <>
Mon Aug 15 09:52:52 CEST 2005


"Richard A. O'Keefe" <> writes:

> With reference to (fixed size) sliding windows,
> I asked whether we really needed built-in support.
> 
> "Ulf Wiger" <> replied:
> 	It's about as justified as erlang:make_tuple/2
> 	and erlang:append_element/2.
> 
> I guess that's a NO, then.  (:-)
> 

I agree. In retrospect, we should not have added those two BIFs.
They are very rarely used.

Maybe we should take them out and replace them with Erlang
functions in the erlang module.

> 
> Yep.
> 	
>     tuple_stuff(Tuple, Amount, Fill, Rotation, Drop, Take)
> 	Tuple		a tuple
> 	Amount		a non-negative integer
> 	Fill		any term	
> 	Rotation	an integer in the range -N..+N,
> 			where N = size(Tuple) + Amount	
> 	Drop		an integer in the range -N..+N
> 	Take		an integer in the range -M..+M
> 			where M = N - |Drop|
> 
>     Step 1:  extend Tuple on the right with Amount copies of Fill
> 	e.g. tuple_stuff({a,b,c}, 2, d, ...) starts with {a,b,c,d,d}.
> 
>     Step 2:  rotation Tuple by Rotation.  If Rotation >= 0, take the
> 	last Rotation elements and move them to the front; if Rotation =< 0,
> 	take the first |Rotation| elements and move them to the back.
> 
>     Step 3:  if Drop >= 0, discard the first |Drop| elements;
> 	if Drop =< 0, discard the last |Drop| elements.
> 
>     Step 4:  if Take >= 0, take the first |Take| elements;
> 	if Take =< 0, take the last |Take| elements.
> 

Given a better name, we could consider adding this BIF.

/Bjorn
-- 
Björn Gustavsson, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list