{"id":19238287,"url":"https://github.com/olical/forc","last_synced_at":"2026-05-18T15:39:34.393Z","repository":{"id":57240018,"uuid":"51036958","full_name":"Olical/forc","owner":"Olical","description":"Clone of Clojure list comprehension in JavaScript","archived":false,"fork":false,"pushed_at":"2016-02-08T23:35:57.000Z","size":44,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-28T08:55:09.573Z","etag":null,"topics":["clojure","lisp"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Olical.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":"2016-02-03T22:58:42.000Z","updated_at":"2024-08-08T14:42:16.000Z","dependencies_parsed_at":"2022-08-30T00:11:33.231Z","dependency_job_id":null,"html_url":"https://github.com/Olical/forc","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/Olical/forc","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Olical%2Fforc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Olical%2Fforc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Olical%2Fforc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Olical%2Fforc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Olical","download_url":"https://codeload.github.com/Olical/forc/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Olical%2Fforc/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264502110,"owners_count":23618552,"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":["clojure","lisp"],"created_at":"2024-11-09T16:30:31.670Z","updated_at":"2026-05-18T15:39:34.350Z","avatar_url":"https://github.com/Olical.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# forc [![npm version](https://badge.fury.io/js/forc.svg)](http://badge.fury.io/js/forc) [![Build Status](https://travis-ci.org/Olical/forc.svg?branch=master)](https://travis-ci.org/Olical/forc) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard) [![Gitter](https://badges.gitter.im/Olical/forc.svg)](https://gitter.im/Olical/forc?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge)\n\nClone of [Clojure's for][clj-for] in JavaScript, this gives you powerful list comprehension without a terse API. It's a declarative code-as-data approach which may seem odd to those that haven't written any kind of lisp before.\n\n## Syntax\n\nIt pretty much translates to the Clojure API 1-1, including the laziness, you just have to use a function wherever you'd use a binding. Clojure allows you to use [lazy sequences][lazyseq], so `forc` lets you use *any* [iterable][], including generators.\n\nThis applies to every single thing you pass or receive from `forc`, including the main expression list. Every \"array\" you see is actually treated as an iterable so you can generate the entire thing on the fly if you so wish.\n\nThis lazy approach should yield huge memory and performance improvements in some situations. It's like everything's a stream internally.\n\n\u003e **WARNING:** `forc` is a generator, it does not return an array. This means you can iterate the results lazily. If you wish to pull all of the values through in one go, you can use `[...result]` on the iterable `forc` returns to resolve everything. I'd recommend using the [iterable][] API though.\n\n### Clojure\n\n```clojure\n(for [x (range 1 6)\n      :let [y (* x x)\n            z (* x x x)]]\n  [x y z])\n\n;; ([1 1 1] [2 4 8] [3 9 27] [4 16 64] [5 25 125])\n```\n\n### JavaScript\n\n```javascript\nforc(['x', [1, 2, 3, 4, 5],\n      ':let', ['y', ({x}) =\u003e x * x,\n               'z', ({x}) =\u003e x * x * x]],\n  ({x, y, z}) =\u003e [x, y, z])\n\n// It returns a generator, but you can resolve it all at once with [...result]\n// I'd recommend working with it as an iterable though, keeping things lazy is a good idea.\n// [[1, 1, 1], [2, 4, 8], [3, 9, 27], [4, 16, 64], [5, 25, 125]]\n```\n\nUsing ES6 destructuring and arrow functions obviously makes this bearable, without that it'd be fairly messy to write. The Clojure syntax is still better, but this could help a lot if you're dealing with a lot of nested mapping or filters.\n\n## Usage\n\nThis is an ES6 project compiled with [Babel][] but it's compiled to ES5 upon publishing of the package. This means you can use the project with or without Babel, it is not required to use `forc`.\n\n * Install the package with `npm install --save forc`\n * Import the function...\n  * ES6: `import forc from 'forc'`\n  * ES5: `var forc = require('forc').default` (note the use of `.default`)\n  * Browsers: Use [webpack][] or [browserify][] with your chosen import method\n * Now read the examples found in here, the tests or the [Clojure for][clj-for] documentation to learn how to use everything\n\nIf you're planning on using this with ES5 you may want to reconsider. I'm not sure how easy it is to use generators and iterables pre-ES6, but I'd imagine it's not great.\n\n## Examples\n\nThese examples can also be found in `./test/examples.js`, you can find a lot in the test directory, actually. I'd recommend having a little read.\n\nThey show how `forc` can be a lazy `map` or `filter` for example. When used correctly, for comprehensions can help you flatten nested `map` or `filter` calls over multiple lists.\n\nThey can replace the need for nested loops of any kind. Instead of iterating over your `x` and `y` coordinates in a nested way, iterate other both at once.\n\n### Map\n\n```javascript\nforc([\n  'x', [60, 61, 62, 63, 64]\n], ({x}) =\u003e String.fromCharCode(x))\n\n// ['\u003c', '=', '\u003e', '?', '@']\n```\n\n### Filter\n\n```javascript\nfunction isEven (n) {\n  return n % 2 === 0\n}\n\nforc([\n  'x', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],\n  ':when', ({x}) =\u003e isEven(x)\n], ({x}) =\u003e x)\n\n// [2, 4, 6, 8, 10]\n```\n\n### Replacing nested loops\n\n```javascript\nforc([\n  'x', [1, 2, 3],\n  'y', [1, 2, 3],\n  ':when', ({x, y}) =\u003e x !== y\n], ({x, y}) =\u003e [x, y])\n\n// All grid coordinates that don't share the same x and y\n// [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]]\n```\n\n## Infinite sequences\n\n```javascript\n// An infinite generator of all natural numbers\nfunction * numbers () {\n  let n = 0\n\n  while (true) {\n    yield n++\n  }\n}\n\nforc([\n  'n', numbers(),\n  ':let', ['square', ({n}) =\u003e n * n],\n  ':while', ({square}) =\u003e square \u003c 100\n], ({square}) =\u003e square)\n\n// Results in only those whos square is \u003c 100\n// [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n```\n\n## Author\n\n[Oliver Caldwell][author-site] ([@OliverCaldwell][author-twitter])\n\n## Unlicenced\n\nFind the full [unlicense][] in the `UNLICENSE` file, but here's a snippet.\n\n\u003eThis is free and unencumbered software released into the public domain.\n\u003e\n\u003eAnyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.\n\nDo what you want. Learn as much as you can. Unlicense more software.\n\n[unlicense]: http://unlicense.org/\n[author-site]: http://oli.me.uk/\n[author-twitter]: https://twitter.com/OliverCaldwell\n[clj-for]: https://clojuredocs.org/clojure.core/for\n[iterable]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Iteration_protocols\n[lazyseq]: http://clojure.org/reference/sequences\n[babel]: https://babeljs.io/\n[webpack]: https://webpack.github.io/\n[browserify]: http://browserify.org/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Folical%2Fforc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Folical%2Fforc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Folical%2Fforc/lists"}