Extra functions on Arrays, Maps, etc..

Adam Shannon adam at ashannon.us
Thu Apr 5 08:26:45 PDT 2012

Hey all,

Scala has a lot of really handy functions that are on arrays, maps,
and a whole range of collections. (It's collection support is a lot
bigger than ES's, anyway..) It would be nice to have these included
(since we're already including .map, .foreach, etc..).

drop(n) // Drops the first n elements
dropRight(n) // Drops the last n elements
filter(fn) // Returns a sub-collection containing the elements that
fn(elm) returned true
fold(newColl, fn) // Folds the elements of the original collection
into newColl given the results of fn
getOrElese // Sugar for (.get === undefined) ? fillerValue : .get
partition(fn) // Splits the collection into two sub-collections given
the result of fn(elm) being true or false.
take(n) // Take only the first n elements
takeRight(n) // Take only the last n elements
A.zip(B) // Creates a map from the two collections, treating A as the
keys and B as the values

Of course, there are more than just these that I've listed.


A simple use case for these are all over in ES6.

var arr = document.getElementsByTagName("a");
var externalElms = arr.filter(function(acc, item) {
(/^https?:\/\//).test(item.href) });


var arr = document.querySelector("div")
var divMap = arr.fold(new Map, function(acc, item) { acc.add(item.id, item); });

Adam Shannon
University of Northern Iowa
Sophomore -- Computer Science B.S. & Mathematics

More information about the es-discuss mailing list