Skip to content

Query Functions

Naoya Komatsu edited this page May 20, 2016 · 13 revisions

Filtering Queries

where

Same as Where in LINQ to Objects.

  • where(bool predicate(T))
    Filter elements based on the predicate.
    predicate is called with each element. The elements is filtered if it returns false.

    Enumerable.from([10,20,30]).where("_ <= 20"); // => 10, 20
  • where(bool predicate(T, int))
    predicate can take index as 2nd argument.

  • where(bool predicate(T, int, args*), args*)
    All arguments are passed to predicate.

distinct

Same as Distinct in LINQ to Objects.

  • distinct()
    Remove all duplicated elements.

    Enumerable.from([1,1,2.0,2,3]).distinct(); // => 1,2.0,3
  • distinct(String keySelector(T))
    keySelector is called with each element and returns string which is used to compare elements.

    Enumerable.from([0,1,2,3,4,5]).distinct("_ % 3"); // => 0,1,2
  • distinct(String keySelector(T, args*), args*)
    All arguments are passed to keySelector.

skip

Same as Skip in LINQ to Objects.

  • skip(int count)
    Skip the specified number of elements.

    Enumerable.from([1,2,3,4]).skip(2); // => 3,4

skipWhile

Same as SkipWhile in LINQ to Objects.

  • skipWhile(bool predicate(T))
    Skip elements while predicate returns false.
    predicate is called with each element.

    Enumerable.from([1,2,3,4]).skipWhile("_ != 3"); // => 3,4
  • skipWhile(bool predicate(T, int))
    predicate can take index as 2nd argument.

  • skipWhile(bool predicate(T, int, args*), args*)
    All arguments are passed to predicate.

take

Same as Take in LINQ to Objects.

  • take(int count)
    Take only the specified number of elements. The left elements are ignored.

    Enumerable.from([1,2,3,4]).take(2); // => 1,2

takeWhile

Same as TakeWhile in LINQ to Objects.

  • takeWhile(bool predicate(T))
    Take elements while predicate returns true.
    predicate is called with each element.

    Enumerable.from([1,2,3,4]).takeWhile("_ != 3"); // => 1, 2
  • takeWhile(bool predicate(T, int))
    predicate can take index as 2nd argument.

  • takeWhile(bool predicate(T, int, args*), args*)
    All arguments are passed to predicate.

Projection Queries

select

Same as Select in LINQ to Objects.

  • select(T2 selector(T))
    Transform each element.
    selector is called with each element and returns a transformed element.

    Enumerable.from([10,20,30]).select("_ * 2"); // => 20, 40, 60
  • select(T2 selector(T, int))
    selector can take index as 2nd argument.

  • select(T2 selector(T, int, args*), args*)
    All arguments are passed to selector.

selectMany

Same as SelectMany in LINQ to Objects.

  • selectMany(TCollection[] collectionSelector(T))
    Flatten each sequences which collectionSelector returns.
    collectionSelector is called with each element and returns sequence which will be flattened.

    Enumerable.from([[10,20,30], [1,2]]).selectMany("_"); // => 10, 20, 30, 1, 2
  • selectMany(TCollection[] collectionSelector(T, int))
    collectionSelector can take index as 2nd argument.

  • selectMany(TCollection[] collectionSelector(T), TResult resultSelector(T, TCollection))
    resultSelector is called with each element of the sequence which collectionSelector returns.
    Its 1st argument is the same as collectionSelector's argument and 2nd argument is each element.

    Enumerable.from([
            %[ name:"even", data:[2,4] ],
            %[ name:"odd", data:[3,5,7] ]
        ]).selectMany("_.data", "_.name + ':' + _2");
    // => "even:2", "even:4", "odd:3", "odd:5", "odd:7"
  • selectMany(TCollection[] collectionSelector(T, int), TResult resultSelector(T, TCollection))
    collectionSelector can take index as 2nd argument.

  • selectMany(TCollection[] collectionSelector(T, int, args*), TResult resultSelector(T, TCollection, args*), args*)
    All arguments are passed to collectionSelector and resultSelector.

scan

Same as Scan in Interactive Extensions.

  • scan(T accumulator(T, T))
    Accumulate values in sequence while returning processing values. accumulator is called with accumulated value and each element and returns next accumulated value.
    The first element of sequence is used as initial accumulated value.

    Enumerable.from("abcd").scan("_ + _2"); // => "ab", "abc", "abcd"
  • scan(T accumulator(T, T, args*), args*)
    All arguments are passed to accumulator.

scanWithSeed

Same as Scan in Interactive Extensions.

  • scanWithSeed(T seed, T accumulator(T, T))
    Almost the same as scan but seed can be specified.
    seed is used as initial accumulated value.

    Enumerable.from("abcd").scanWithSeed("", "_ + _2"); // => "a", "ab", "abc", "abcd"
  • scanWithSeed(T seed, T accumulator(T, T, args*), args*)
    All arguments are passed to accumulator.

Concatenating Queries

startsWith

Same as StartWith in Interactive Extensions.

  • startWith(values*)
    Insert specified values at the head of sequence

    Enumerable.from([1,2,3]).startWith(-1,0); // => -1,0,1,2,3

concat

Same as Concat in LINQ to Objects.

  • concat(T[] second)
    Concatenate specified sequence.

    Enumerable.from([1,2,3]).concat([4,5]); // => 1,2,3,4,5

zip

Same as Zip in LINQ to Objects.

  • zip(TSecond[] second, TResult resultSelector(T, TSecond))
    Merge specified sequence.
    resultSelector is called with two elements - former is from the source and latter is from the specified sequence as second - and returns merged new element.
    If the length of two elements differ, the extra elements in longer sequence are ignored.

    Enumerable.from([1,2,3]).zip([4,5,6], "[_, _2]"); // => [1,4], [2,5], [3,6]
    Enumerable.from([1,2,3]).zip([4,5], "[_, _2]"); // => [1,4], [2,5]
  • zip(TSecond[] second, TResult resultSelector(T, TSecond, args*), args*)
    All arguments are passed to resultSelector.

defaultIfEmpty

Same as DefaultIfEmpty in LINQ to Objects.

  • defaultIfEmpty(T defualtValue)
    Only when the sequence is empty, insert defaultValue.

    Enumerable.from([1,2,3]).defaultIfEmpty("def"); // => 1,2,3
    Enumerable.empty().defaultIfEmpty("def"); // => "def"

repeat

Same as "Repeat" in Interactive Extensions.

  • repeat()
    Repeat elements infinitly.

    Enumerable.from([1,2,3]).repeat(); // => 1,2,3,1,2,3,1,2,3,...
  • repeat(count)
    Repeat elements specified times.

    Enumerable.from([1,2,3]).repeat(2); // => 1,2,3,1,2,3

Join Queries

innerJoin

Same as Join in LINQ to Objects.

  • innerJoin(T2[] inner, String outerKeySelector(T), String innerKeySelector(T2), TResult resultSelector(T, T2))
    Correlate two elements from the source and the specified sequence as inner based on their keys.
    inner is sequence whose elements will be correlated.
    outerKeySelector is called with each element from the source and returns corresponding key.
    innerKeySelector is called with each element from inner sequence and returns corresponding key.
    resultSelector is called with two elements whose keys match and returns a result element.

    Enumerable.from([0,1,2]).
        innerJoin(
            Enumerable.from(["a", "b", "cd", "efg"]),
            "_", "_.length", "%[ length:_, str:_2 ]");
    // => %[ length:1, str:"a"], %[ length:1, str:"b"], %[ length:2, str:"cd"]
  • innerJoin(T2[] inner, String outerKeySelector(T, args*), String innerKeySelector(T2, args*), TResult resultSelector(T, T2, args*), args*)
    All arguments are passed to outerKeySelector, innerKeySelector, and resultSelector.

outerJoin

Alias of groupJoin.

groupJoin

Same as GroupJoin in LINQ to Objects.

  • groupJoin(T2[] inner, String outerKeySelector(T), String innerKeySelector(T2), TResult resultSelector(T, T2[]))
    Correlate the element from the source and corresponding elements from the specified sequence as inner based on their keys.
    inner is sequence whose elements will be correlated.
    outerKeySelector is called with each element from the source and returns corresponding key.
    innerKeySelector is called with each element from inner sequence and returns corresponding key.
    resultSelector is called with a element from the source and a sequence of all matching elements from inner.
    resultSelector's 2nd argument may be empty sequence. resultSelector returns a result element.

    Enumerable.from([0,1,2]).
        groupJoin(
            Enumerable.from(["a", "b", "cd", "efg"]),
            "_", "_.length", "%[ length:_, str:_2.toArray() ]");
    // => %[ length:0, str:[] ], %[ length:1, str:["a","b"] ], %[ length:2, str:["cd"] ]
  • groupJoin(T2[] inner, String outerKeySelector(T, args*), String innerKeySelector(T2, args*), TResult resultSelector(T, T2[], args*), args*)
    All arguments are passed to outerKeySelector, innerKeySelector, and resultSelector.

Grouping Queries

groupBy

Same as GroupBy in LINQ to Objects.

  • groupBy(String keySelector(T));
    Grouping elements based on their keys.
    keySelector is called with each element and returns corresponding keys.

    Enumerable.from([1,2,3,4,5])
        .groupBy("_ % 2 == 0 ? 'odd:' : 'even:'")
        .select("_.key + _.toString(',')");
    // => "odd:2,4", "even:1,3,5
  • groupBy(String keySelector(T), TElement elementSelector(T))
    elementSelector is called with each element and returns a transformed element.

    Enumerable.from([1,2,3,4,5])
        .groupBy("_ % 2 == 0 ? 'odd:' : 'even:'", "_ + ','")
        .select("_.key + _.toString()");
    // => "odd:2,4", "even:1,3,5
  • groupBy(String keySelector(T), TElement elementSelector(T), TResult resultSelector(String, TElement[]))
    resultSelector is called with each key returned by keySelector and sequence of matching elements returned by elementSelector.
    resultSelector returns the result value.

      ```javascript
      Enumerable.from([1,2,3,4,5])
          .groupBy("_ % 2 == 0 ? 'odd:' : 'even:'", "_ + ','", "_ + _2.toString()")
      // => "odd:2,4", "even:1,3,5
      ```
    
  • groupBy(String keySelector(T, args*), TElement elementSelector(T, args*), TResult resultSelector(String, TElement[], args*), args*)
    All arguments are passed to keySelector, elementSelector, and resultSelector.

buffer

Same as Buffer in Interactive Extensions.

  • buffer(int count)
    Segment sequence every specified number.

    Enumerable.from([1,2,3,4,5]).buffer(2); // => [1,2], [3,4], [5]
  • buffer(int count, int skip)
    Each segmented sequence starts after an interval of number specified as skip.
    These sequences can overlap.

    Enumerable.from([1,2,3,4,5]).buffer(2,1); // => [1,2], [2,3], [3,4], [4,5], [5]

Set Operational Queries

union

Same as Union in LINQ to Objects.

  • union(T[] second)
    Concatenate specified sequence and remove duplicated elements.

    Enumerable.from([1,2,3]).union([4,5,2,1]); // => 1,2,3,4,5
  • union(T[] second, String keySelector(T))
    keySelector is called with each element and returns string which is used to compare elements.

    Enumerable.from([1,2,3]).union([0,4,5,6], "_%5"); // => 1,2,3,0,4
  • union(T[] second, String keySelector(T, args*), args*)
    All arguments are passed to keySelector.

intersect

Same as Intersect in LINQ to Objects.

  • intersect(T[] second)
    Create new sequence whose elements are all included in the source and the specified sequence.

    Enumerable.from([1,2,3]).intersect([4,5,2,3], "_%5"); // => 2,3
  • intersect(T[] second, String keySelector(T))
    keySelector is called with each element and returns string which is used to compare elements.

    Enumerable.from([1,2,3]).intersect([0,4,5,6], "_%5"); // => 1
  • intersect(T[] second, String keySelector(T, args*), args*)
    All arguments are passed to keySelector.

except

Same as Except in LINQ to Objects.

  • except(T[] second)
    Remove elements which are included in second and duplicated elements.

    Enumerable.from([1,2,3,4]).except([3,4,5,6]); // => 1,2
  • except(T[] second, String keySelector(T))
    keySelector is called with each element and returns string which is used to compare elements.

    Enumerable.from([1,2,3]).except([0,4,5,6], "_%5"); // => 2,3

Sorting Queries

orderBy

Same as OrderBy in LINQ to Objects.

  • orderBy(TKey keySelector(T))
    Sort elements in ascending order according to the selected keys.
    keySelector is called with each element and returns corresponding key.

    Enumerable.from([4,2,5,3,1]).orderBy("_"); // => 1,2,3,4,5
  • orderBy(TKey keySelector(T), int comparer(T, T))
    comparer is called with two keys and returns -1, 0, or 1.
    -1 means the 1st argument is smaller than 2nd one.
    0 means the 1st and 2nd arguments are equal.
    1 means the 1st argument is larger than 2nd one.

orderByDescending

Same as OrderByDescending in LINQ to Objects.

  • orderByDescending(TKey keySelector(T))
    Sort elements in ascending order according to the selected keys.
    keySelector is called with each element and returns corresponding key.

    Enumerable.from([4,2,5,3,1]).orderByDescending("_"); // => 5,4,3,2,1
  • orderByDescending(TKey keySelector(T), int comparer(T, T))
    comparer is called with two keys and returns -1, 0, or 1.
    -1 means the 1st argument is smaller than 2nd one.
    0 means the 1st and 2nd arguments are equal.
    1 means the 1st argument is larger than 2nd one.

thenBy

Same as ThenBy in LINQ to Objects.

  • thenBy(TKey keySelector(T))
    Sort elements in ascending order according to the selected keys subsequently to orderBy.
    keySelector is called with each element and returns corresponding key.

    Enumerable.from(["dd","g","","aa","f","b"])
        .orderBy("_.length")
        .thenBy("_");
         // => "","b","f","g","aa","dd"
  • thenBy(TKey keySelector(T), int comparer(T, T))
    comparer is called with two keys and returns -1, 0, or 1.
    -1 means the 1st argument is smaller than 2nd one.
    0 means the 1st and 2nd arguments are equal.
    1 means the 1st argument is larger than 2nd one.

thenByDescending

Same as ThenByDescending in LINQ to Objects.

  • thenByDescending(TKey keySelector(T))
    Sort elements in ascending order according to the selected keys subsequently to orderBy.
    keySelector is called with each element and returns corresponding key.

    Enumerable.from(["dd","g","","aa","f","b"])
        .orderBy("_.length")
        .thenByDescending("_");
         // => "","g","f","b","dd","aa"
  • thenByDescending(TKey keySelector(T), int comparer(T, T))
    comparer is called with two keys and returns -1, 0, or 1.
    -1 means the 1st argument is smaller than 2nd one.
    0 means the 1st and 2nd arguments are equal.
    1 means the 1st argument is larger than 2nd one.

reverse

Same as Reverse in LINQ to Objects.

  • reverse()
    Reverse the order of the elements.

    Enumerable.from("abcde").reverse(); // => "e","d","c","b","a"

Element acquisition Queries

elementAt

Same as ElementAt in LINQ to Objects.

  • elementAt(int index)
    Return element at the specified index.
    If the index is out of range, an exception is thrown.

    Enumerable.from([1,2,3,4]).elementAt(2); // => 3

elementAtOrDefault

Same as ElementAtOrDefault in LINQ to Objects.

  • elementAtOrDefault(int index, T defaultValue)
    Return element at the specified index.
    If the index is out of range, defaultValue is returned.

    Enumerable.from([1,2,3,4]).elementAtOrDefault(10, 0); // => 0

first

Same as First in LINQ to Objects.

  • first()
    Return the first element.
    If there is no element, an exception is throwns.

    Enumerable.from([-2,-1,0,1,2]).first(); // => -2
  • first(bool predicate(T))
    Return the first element which the predicate returns true with.
    predicate is called with each element.
    If no element meets the condition, an exception is thrown.

    Enumerable.from([-2,-1,0,1,2]).first("_ > 0"); // => 1
  • first(bool predicate(T, args*), args*)
    All arguments are passed to predicate.

firstOrDefault

Same as FirstOrDefault in LINQ to Objects.

  • firstOrDefault(T defaultValue)
    Return the first element.
    If there is no element, defaultValue is returned.

    Enumerable.from([]).firstOrDefault(1); // => 1
  • firstOrDefault(T defaultValue, bool predicate(T))
    Return the first element which the predicate returns true with.
    predicate is called with each element.
    If no element meets the condition, defaultValue is returned.

    Enumerable.from([-2,-1,0]).firstOrDefault(10, "_ > 0"); // => 10
  • firstOrDefault(T defaultValue, bool predicate(T, args*), args*)
    All arguments are passed to predicate.

last

Same as Last in LINQ to Objects.

  • last()
    Return the last element.
    If there is no element, an exception is throwns.

    Enumerable.from([0,1,2]).last(); // => 2
  • last(bool predicate(T))
    Return the last element which the predicate returns true with.
    predicate is called with each element.
    If no element meets the condition, an exception is thrown.

    Enumerable.from([2,1,0,-1,-2]).last("_ > 0"); // => 1
  • last(bool predicate(T, args*), args*)
    All arguments are passed to predicate.

lastOrDefault

Same as LastOrDefault in LINQ to Objects.

  • lastOrDefault(T defaultValue)
    Return the last element.
    If there is no element, defaultValue is returned.

    Enumerable.from([]).lastOrDefault(1); // => 1
  • lastOrDefault(T defaultValue, bool predicate(T))
    Return the last element which the predicate returns true with.
    predicate is called with each element.
    If no element meets the condition, defaultValue is returned.

    Enumerable.from([-2,-1,0]).lastOrDefault(10, "_ > 0"); // => 10
  • lastOrDefault(T defaultValue, bool predicate(T, args*), args*)
    All arguments are passed to predicate.

single

Same as Single in LINQ to Objects.

  • single()
    Return the only element.
    If the number of element is not one, an exception is thrown.

    Enumerable.from([0]).single(); // => 0
  • single(bool predicate(T))
    Return the only element which the predicate returns true with.
    predicate is called with each element.
    If the number of element is not one, an exception is thrown.

    Enumerable.from([1,0,-1]).single("_ > 0"); // => 1
  • single(bool predicate(T, args*), args*)
    All arguments are passed to predicate.

singleOrDefault

Same as SingleOrDefault in LINQ to Objects.

  • singleOrDefault(T defaultValue)
    Return the only element.
    If there is no element, defaultValue is returned.

    Enumerable.from([]).singleOrDefault(1); // => 1
  • singleOrDefault(T defaultValue, bool predicate(T))
    Return the only element which the predicate returns true with.
    predicate is called with each element.
    If no element meets the condition, defaultValue is returned.

    Enumerable.from([-2,-1,0]).singleOrDefault(10, "_ > 0"); // => 10
  • singleOrDefault(T defaultValue, bool predicate(T, args*), args*)
    All arguments are passed to predicate.

Aggregation Queries

count

Same as Count in LINQ to Objects.

  • count()
    Return the number of elements.

    Enumerable.from([1,2,3]).count(); // => 3
  • count(bool predicate(T))
    Return the number of elements which the *predicate returns true with.
    predicate is called with each element.

    Enumerable.from([-1,0,1,2]).count("_ > 0"); // => 2
  • count(bool predicate(T, args*), args*)
    All arguments are passed to predicate.

aggregate

Same as Aggregate in LINQ to Objects.

  • aggregate(T2 accumulator(T2, T))
    Accumulate elements by the specified accumulator.
    The 1st argument of accumulator is the accumulated value returned by itself.
    The 2nd argument is each element to accumulate.
    For the first call, the first element is passed as 1st argument.

    Enumerable.from([1,2,3,4]).aggregate("_ * _2"); // => 24
  • aggregate(T2 accumulator(T2, T), TResult resultSelector(T2))
    resultSelector is called with the final accumulated value and returns a result.

    Enumerable.from([1,2,3,4]).aggregate("_ * _2", "_ * -1"); // => -24
  • aggregate(T2 accumulator(T2, T, args*), TResult resultSelector(T2, args*), args*)
    All arguments are passed to accumulator and resultSelector.

aggregateWithSeed

Same as AggregateWithSeed in LINQ to Objects.

  • aggregateWithSeed(T seed, T2 accumulator(T2, T))
    Accumulate elements by the specified accumulator.
    The 1st argument of accumulator is the accumulated value returned by itself.
    The 2nd argument is each element to accumulate.
    For the first call, seed is passed as 1st argument.

    Enumerable.from([1,2,3,4]).aggregateWithSeed(5, "_ * _2"); // => 120
  • aggregateWithSeed(T seed, T2 accumulator(T2, T), TResult resultSelector(T2))
    resultSelector is called with the final accumulated value and returns a result.

    Enumerable.from([1,2,3,4]).aggregateWithSeed(5, "_ * _2", "_ * -1"); // => -120
  • aggregateWithSeed(T seed, T2 accumulator(T2, T, args*), TResult resultSelector(T2, args*), args*)
    All arguments are passed to accumulator and resultSelector.

average

Same as Average in LINQ to Objects.

  • average() Returns the average of all elements.

    Enumerable.from([1,2,3,4]).average(); // => 2.5
  • average(T2 selector(T)) selector is called with each element and transforms the element.

    Enumerable.from(["a", "bc", "def"]).average("_.length"); // => 2
  • average(T2 selector(T, args*), args*)
    All arguments are passed to selector.

max

Same as Max in LINQ to Objects.

  • max() Returns the max value of all elements.

    Enumerable.from([1,2,3,4]).max(); // => 4
  • max(T2 selector(T)) selector is called with each element and transforms the element.

    Enumerable.from(["a", "bc", "def"]).max("_.length"); // => 3
  • max(T2 selector(T, args*), args*)
    All arguments are passed to selector.

min

Same as Min in LINQ to Objects.

  • min() Returns the max value of all elements.

    Enumerable.from([1,2,3,4]).min(); // => 1
  • min(T2 selector(T)) selector is called with each element and transforms the element.

    Enumerable.from(["a", "bc", "def"]).min("_.length"); // => 1
  • min(T2 selector(T, args*), args*)
    All arguments are passed to selector.

sum

Same as Sum in LINQ to Objects.

  • sum() Returns the sum of all elements.

    Enumerable.from([1,2,3,4]).sum(); // => 10
  • sum(T2 selector(T)) selector is called with each element and transforms the element.

    Enumerable.from(["a", "bc", "def"]).sum("_.length"); // => 6
  • sum(T2 selector(T, args*), args*)
    All arguments are passed to selector.

Judgement Queries

isEmpty

Same as IsEmpty in LINQ to Objects.

any

Same as Any in LINQ to Objects.

all

Same as All in LINQ to Objects.

contains

Same as Contains in LINQ to Objects.

indexOf

Same as IndexOf in linq.js.

sequenceEqual

Same as SequenceEqual in LINQ to Objects.

Conversion Queries

toArray

toString

toDictionary

toLookup

Action Queries

forEach

trace

force

Clone this wiki locally