Skip to content

Static Functions

Naoya Komatsu edited this page May 21, 2016 · 2 revisions

Generation

There are some static functions to create enumerable object.
Enumerable object means the object which has "getEnumerator" and other query methods like "select" as method.

Enumerable.from

Same as "Enumerable.From" in linq.js.

  • Enumerable.from(Array target)
    Converts the specified array to an enumerable object.

    var enumerableArray = Enumerable.from([1,2,3]);
    enumerableArray.select("_*2").forEach(function(x) {
        System.inform(x); // <= 2,4,6
    });
  • Enumerable.from(Dictionary target)
    Converts the specified dictionary to an enumerable object.
    Dictionary objects are enumerated as dictionary which contains "key" and "value".

    var enumerableDictionary = Enumerable.from(%[
        name : "Alice",
        age : 306
    ]);
    enumerableDictionary.forEach(function(pair) {
        System.inform(pair.value, pair.key);
            // <= %[key:"name", value:"Alice"], %[key:"age", value:30]
    });
  • Enumerable.from(String target)
    Converts the specified string to an enumerable object.
    String is enumerated for each character.

    var enumerableString = Enumerable.from("abcd");
    enumerableString.forEach(function(x) {
        System.inform(x); // <= a,b,c,d
    });
  • Enumerable.from(T target)
    If target has "getEnumerator" method, returns the target itself.
    If not, returns null.

Enumerable.empty

Same as "Enumerable.Empty" in LINQ to Objects.

  • Enumerable.empty()
    Creates an enumerable object which has no element.

Enumerable.range

Same as "Enumerable.Range" in LINQ to Objects.

  • Enumerable.range(int start)
    Creates an enumerable object counting number.
    start is the first element and following elements are recursively incremented numbers.

    enumerableString.range(0); // => 0,1,2,3,...
    enumerableString.range(10); // => 10,11,12,13,...
  • Enumerable.range(int start, int count)
    count is the number of elements.

    Enumerable.range(0,5); // => 0,1,2,3,4
    Enumerable.range(10,3); // => 10,11,12
  • Enumerable.range(int start, int count, int step)
    step is the difference of the adjacent elements.

    Enumerable.range(0,5,-1); // => 0,-1,-2,-3,-4
    Enumerable.range(10,3,3); // => 10,13,16

Enumerable.repeat

Same as "Enumerable.Repeat" in LINQ to Objects.

  • Enumerable.repeat(T value)
    Creates an enumerable object which enumerates the specified value infinitely.

    Enumerable.repeat(0); // => 0,0,0,0,...
  • Enumerable.repeat(T value, int count)
    count is the number of elements.

    Enumerable.repeat(0, 3); // => 0,0,0

Enumerable.return

Same as "Enumerable.Return" in linq.js.

  • Enumerable.return(T value)
    Creates an enumerable object whose only element is the specified value.

    Enumerable.return(0); // => 0

Enumerable.matches

  • Enumerable.matches(RegExp regex, String target)
    Applies the specified regex to the target string and returns the enumerable objects which contains the result as elements.
    Each element is a return value of RegExp.matches.
    regex must include "g" in its flag and its start is reset to 0 automatically.

    Enumerable.matches(/([a-z])([A-Z])/g, "aAbBcc");
        // => [ "aA", "a", "A"], ["bB", "b", "B"]
  • Enumerable.matches(RegExp regex, String target, bool reset)
    If reset is false, regex's start will not be reset.

Enumerable.random

  • Enumerable.random()
    Creates an infinite enumerable object whose element is a random number in the range of [0,1).

    Enumerable.random(); // => 0.6198627001926930, 29104060701630, 95182625324763, ...
  • Enumerable.random(int seed)
    seed is the random seed.

  • Enumerable.random(int seed, int count)
    count is the number of elements.

Enumerable.randomInt

  • Enumerable.randomInt(int min, int max)
    Creates an infinite enumerable object whose elment is a random number in the range of [min, max].

    Enumerable.randomInt(0,3); // => 2,1,3,3,2,3,0,...
  • Enumerable.randomInt(int min, int max, int seed)
    seed is the random seed.

  • Enumerable.randomInt(int min, int max, int seed, int count)
    count is the number of elements.

Enumerable.generate

Same as "Enumerable.Generate" in linq.js.

  • Enumerable.generate(T generator(int), int count)
    Creates an enumerable object.
    While enumerating, generator is called to get next element with an index.
    count is the number of elements.
    If count is void, its enumerable object has infinite elements.

    var enumerableObject = Enumerable.generate(function(index) { return index;}, 5);
    enumerableObject.forEach(function(x) {
        System.inform(x); // <= 0,1,2,3,4
    });
  • Enumerable.generate(T generator(int, args*), int count, args*)
    All arguments are passed to generator.

Utility

Enumerable.extendTo

Same as "Enumerable.extendTo" in linq.js.

  • Enumerable.extendTo(Class target)
    Adds query methods like "select" into target class object.
    If target is Array, "getEnumerator" is added too.
    In the other words, all array objects can be used as enumerable object once you execute "Enumerable.extendTo(Array);"

    Enumerable.extendTo(Array);
    var array = [-2,-1,0,1,2,3];
    array.where("_ >= 0").select("_ * 2").forEach(function(x) {
        System.inform(x); // <= 0,2,4,6
    });

Enumerable.isEnumerable

  • Enumerable.isEnumerable(T object)
    Returns true if the specified object is a sort of Object and having getEnumerator method.
Clone this wiki locally