Skip to content

Instantly share code, notes, and snippets.

@trigeorgis
Created September 12, 2011 12:33
Show Gist options
  • Save trigeorgis/1211149 to your computer and use it in GitHub Desktop.
Save trigeorgis/1211149 to your computer and use it in GitHub Desktop.
Stream.js addition
drop: function(howmany){
if ( howmany <= 0 ) {
if ( this.empty() ) {
return new Stream();
}
return new Stream( this.head(), this.tail() );
}
// Otherwise we could check only once if it's empty and omit the exception.
if ( this.empty() ) {
throw 'You want to drop too many items.';
}
return this.tail().drop( howmany - 1 );
}
@dionyziz
Copy link

That's a very nice idea — thank you. I have two suggestions before this can be merged:

  1. s.drop( n ) where n > s.length() should just drop s.length() without an error (and return the empty stream). I believe that would be a more convenient API.
  2. The drop operation should be implemented iteratively, not recursively. This is somewhat important given that Javascript has tight stack memory limits. Please see my recent changes in the implementation of s.length() and s.item() that reflect this requirement.

Otherwise great job! Thanks :)

@trigeorgis
Copy link
Author

  1. I am not sure how would be better but now that I am thinking bout it Haskell does exactly the same.
  2. Yeap you are right, did not notice that you have the js on github till later on.

I will fork it and make the changes ;) Whats next, maybe http://haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html ? :p

Great idea and job as well! ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment