{"id":24987434,"url":"https://github.com/sergi/enumjs","last_synced_at":"2025-03-29T11:12:33.885Z","repository":{"id":5641245,"uuid":"6849760","full_name":"sergi/enumjs","owner":"sergi","description":"Enumeration over abstract collection of elements, in a functional way.","archived":false,"fork":false,"pushed_at":"2012-12-03T08:35:47.000Z","size":103,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-26T08:13:55.465Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sergi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-11-25T10:26:42.000Z","updated_at":"2019-07-11T15:52:15.000Z","dependencies_parsed_at":"2022-08-24T20:52:07.843Z","dependency_job_id":null,"html_url":"https://github.com/sergi/enumjs","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sergi%2Fenumjs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sergi%2Fenumjs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sergi%2Fenumjs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sergi%2Fenumjs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sergi","download_url":"https://codeload.github.com/sergi/enumjs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246174604,"owners_count":20735417,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-04T11:42:59.627Z","updated_at":"2025-03-29T11:12:33.870Z","avatar_url":"https://github.com/sergi.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"enum.js\n=======\n\nInstall\n-------\n\n    npm install enumjs\n\nOr from source:\n\n    git clone git://github.com/sergi/enumjs.git\n    cd enumjs\n    npm link\n\nWhat is it\n----------\n\nEnum.js provides easy representation of finite or infinite sequences of elements.\n\nEnumerations are a uniform manner of reading and manipulating the contents\nof a data structure, or as a simple manner of reading or writing sequences\nof characters, numbers, strings, etc. from/to files, network connections or\nother inputs/outputs.\n\nEnumerations are typically computed as needed, which allows the definition\nand manipulation of huge (possibly infinite) sequences. Manipulating an\nenumeration is a uniform and often comfortable way of extracting subsequences\n(For example, using `Enumjs.filter`), converting sequences into other\nsequences (Using `Enumjs.map`), gathering information (function `Enumjs.scanl` et al)\nor performing loops (functions Enumjs.iter and Enum.map).\n\nThe library comes with support with common \"sequenceable\" items such as arrays or\nstrings, but it shines when creating custom enumerations.\n\nSimple examples\n---------------\n\n```javascript\n    var seq = Enum.fromArray([1, 2, 3, 4, 5, 6]);\n    // [1, 2, 3, 4, 5, 6]\n    //  ^\n    //  Cursor is at position 0\n\n    seq.next(); // returns 1\n    // [1, 2, 3, 4, 5, 6]\n    //     ^\n    //     Cursor is at position 1\n\n    // Iterate over the values and print them. This will consume the whole\n    // enumeration\n    seq.iter(console.log);\n    // [1, 2, 3, 4, 5, 6]\n    //                 ^\n    //                 Cursor is at position 5. The enumeration is now depleted.\n\n    var seq2 = Enum.fromArray([1, 2, 3, 4, 5, 6]);\n    var even = function(e) {\n        return e % 2 === 0;\n    };\n\n    var el = seq2.find(even); // Returns 2\n    // [1, 2, 3, 4, 5, 6]\n    //     ^\n    //     Cursor is at position 1\n```\n\nIn the example below, Enumjs.randInt() creates an infinite enumeration of\nrandom numbers. Combined with `Enum.map`, we may turn this into an\ninfinite enumeration of squares of random even numbers:\n\n```javascript\n\nEnum.randInt()\n    .filter(function(e) { return e % 2 == 0 }\n    .map(function(e) { return e * e })\n\n```\n\nSimilarly, to get an enumeration of 50 random integers, we may use\nEnum.take:\n\n```javascript\nEnum.take(50, Enum.randInt())\n```\n\nDefining custom enumerations\n----------------------------\n\nIn order to define\n\nOne of the advantages of Enum.js is that it can operate over infinite lists of items,\nas long as the `next()` operation is properly defined. Obviously, the Fibonacci\nenumeration above is an example of this, and that's why the `count` method returns\ninfinity.\n\nFor example, an enumeration for the Fibonacci sequence would look a bit like this:\n\n```javascript\nvar fibo = new Enum(\n    function next() {\n        var n;\n        var data = this.data;\n\n        if (data.count \u003c 2)\n            n = data.count;\n        else\n            n = data.cache[0] + data.cache[1];\n\n        data.cache.push(n);\n        data.cache = data.cache.slice(-2); // We only cache last 2 values\n        data.count++;\n\n        return n;\n    },\n\n    function count() { return Infinity; },\n\n    function clone() {\n        var _clone = fibo();\n        _clone.data.count = this.data.count;\n        _clone.data.cache = [this.cache[0], this.cache[1]];\n        return clone;\n    },\n\n    // Initial extra properties for this enumeration\n    { count: 0, cache: [] }\n);\n```\n\nTo list the first 300 Fibonacci numbers we can do:\n\n```javascript\nfor (var i = 0; i \u003c 300; i++)\n    console.log(fibo.get());\n```\n\nEnumerations can only go forward. That means that most operations in the enumeration\nconsume the current item (that is, advance the cursor). Enumerations are entirely\nfunctional and in case to be based on a particular object like an array, they do not\nmodify the original object, while trying to be as efficient as possible.\n\nEnum.js is inspired by OCaml's `Enum` module.\n\n\nCopyright 2012 Sergi Mansilla.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsergi%2Fenumjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsergi%2Fenumjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsergi%2Fenumjs/lists"}