<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>Snipplr</title>
    <description>Recent snippets posted on Snipplr.com</description>
    <link>https://snipplr.com/</link>
    <lastBuildDate>Mon, 15 Jun 2026 12:12:11 +0000</lastBuildDate>
    <item>
      <title>(JavaScript) ObjectBoilerPlateModule - some methods to assist creating objects - codernator</title>
      <link>https://snipplr.com/view/69305/objectboilerplatemodule--some-methods-to-assist-creating-objects</link>
      <description>&lt;p&gt;# addMethod - By John Resig (MIT Licensed)&#13;
Add a method to an object. Creates function overloading scenario based entirely on length of argument list. Won't work to overload functions according to argument "type".&#13;
&#13;
# inherit&#13;
Create parameter-less inheritance chain for situations in which the parent object takes arguments in its "constructor".&#13;
&#13;
Usage:&#13;
&#13;
    function A(a, b) { }&#13;
    function B() { A.call(this, 1, 2); }&#13;
    B.prototype = inherit(A.prototype);&lt;/p&gt;</description>
      <pubDate>Sat, 29 Dec 2012 08:52:22 UTC</pubDate>
      <guid>https://snipplr.com/view/69305/objectboilerplatemodule--some-methods-to-assist-creating-objects</guid>
    </item>
    <item>
      <title>(JavaScript) GeneratorModule: Array Creation Inspired by Python\'s List Comprehension - codernator</title>
      <link>https://snipplr.com/view/69270/generatormodule-array-creation-inspired-by-pythons-list-comprehension</link>
      <description>&lt;p&gt;The inspiration for this module comes from Python's list comprehension: [x for x in range(10)]. The idea is to replace a for-loop with something more condensed. However, since such syntax is completely foreign to Javascript, that operation looks more like: app.genList(app.rangeGenerator(0, 10)).&#13;
&#13;
Requires ObjectBoilerPlateModule.&#13;
&#13;
GeneratorModule comes with four "public" methods:&#13;
&#13;
* getGeneratorModuleVersion - useful to test for existence of module.&#13;
* arrayGenerator - iterate over an array, optionally filtering items from the array with a predicate argument.&#13;
* rangeGenerator - iterate over a number sequence from min to max.&#13;
* genList - create an array using either an arrayGenerator or a rangeGenerator, optionally morphing elements from the generator into a new data type.&#13;
&#13;
This is implemented using module pattern. To inject the functions into the global namespace, simply invoke the GeneratorModule method. Otherwise, the functions can be added to an existing object using GeneratorModule.call([object]).&#13;
&#13;
Examples:&#13;
&#13;
    function test() {&#13;
        "use strict";&#13;
&#13;
        function reportNumber(number) {&#13;
            return number + " is " + (number % 2 === 0 ? "even" : "odd") + ".";&#13;
        }&#13;
&#13;
        function reportLetter(letter) {&#13;
            return "The letter is " + letter + ".";&#13;
        }&#13;
&#13;
        function isMultipleOf3(number) {&#13;
            return number % 3 === 0;&#13;
        }&#13;
&#13;
        var app = {};&#13;
        ObjectBoilerPlateModule.call(app);&#13;
        GeneratorModule.call(app, app);&#13;
        console.log(app.toList(app.range(0, 10)));&#13;
        // [0,1,2,3,4,5,6,7,8,9]&#13;
&#13;
        console.log(app.toList(reportNumber, app.range(0, 15), isMultipleOf3));&#13;
        // ["0 is even.", "3 is odd.", "6 is even.", "9 is odd.", "12 is even."]&#13;
&#13;
        console.log(app.toList(app.inArray(["a", "d", 'q'])));&#13;
        // ["a", "d", "q"]&#13;
&#13;
        console.log(app.toList(reportLetter, app.inArray(["a", "d", 'q']), function (x) { return x === "a"; }));&#13;
        // ["The letter is a."]&#13;
    }&#13;
&#13;
Limitations/Considerations:&#13;
&#13;
* Like Python generators, only 1 iteration will function - the generators do not come with a "reset" method.&#13;
* The ArrayGenerator objects hold reference to "list" passed in the constructor. If there is code between creating the generator and invoking it (say with genList), it is possible the contents of "list" will change. (This could be mitigated by having the ArrayGenerator constructor perform a deep copy "list"...)&lt;/p&gt;</description>
      <pubDate>Thu, 27 Dec 2012 06:11:03 UTC</pubDate>
      <guid>https://snipplr.com/view/69270/generatormodule-array-creation-inspired-by-pythons-list-comprehension</guid>
    </item>
  </channel>
</rss>
