Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stasm/eslisp-chain
An eslisp macro for chaining method calls on a single object.
https://github.com/stasm/eslisp-chain
Last synced: about 1 month ago
JSON representation
An eslisp macro for chaining method calls on a single object.
- Host: GitHub
- URL: https://github.com/stasm/eslisp-chain
- Owner: stasm
- Created: 2015-10-10T14:31:32.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-10-10T16:45:32.000Z (over 9 years ago)
- Last Synced: 2024-10-16T18:18:54.480Z (3 months ago)
- Language: Makefile
- Homepage: https://www.npmjs.com/package/eslisp-chain
- Size: 121 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# eslisp-chain [![Build status][travisimage]][travislink]
[travisimage]: https://travis-ci.org/stasm/eslisp-chain.png?branch=master
[travislink]: https://travis-ci.org/stasm/eslisp-chainAn [eslisp][] macro for chaining method calls on a single object.
## Examples
The macro is best suited for APIs whose methods return the same
object to make it possible to chain sebsequent method calls. _eslisp_'s
functional syntax makes such API rather tedious to use.Consider the following example in JavaScript:
service.method('greet', function (name) {
return 'Hello, ' + (name + '!');
}).listen();This can be expressed in _eslisp_ with the following code:
((.
((. service method) "greet" (lambda (name)
(return (+ "Hello, " name "!"))))
listen))As the number of method calls increases, this inside-out functional syntax can
get unwieldy, with multiple nested `((.` invocations.The _eslisp-chain_ macro makes it possible to write the same code in
a sequential manner:(macro -> (require "eslisp-chain"))
(-> service
(method "greet" (lambda (name)
(return (+ "Hello, " name "!"))))
(listen))This syntax is convenient for chaining promises:
(macro -> (require "eslisp-chain"))
(-> (read "data.json")
(then (. JSON parse))
(then (lambda (obj)
(return ((. Object keys) obj))))
(catch (. console error)))↓
read('data.json').then(JSON.parse).then(function (obj) {
return Object.keys(obj);
}).catch(console.error);It can be used to compose array methods:
(macro -> (require "eslisp-chain"))
(-> Object
(keys data)
(filter (lambda (key)
(return (!== (get key 0) "_"))))
(forEach processPublicMembers))↓
Object.keys(data).filter(function (key) {
return key[0] !== '_';
}).forEach(processPublicMembers);The macro offers an alternative even for single method calls when there is no
chaining:(macro -> (require "eslisp-chain"))
(-> app
(get "/" (lambda (req, res)
((. res send) "Hello, world!"))))(-> app
(listen 3000))↓
app.get('/', function (req, unquote(res)) {
res.send('Hello, world!');
});
app.listen(3000);[eslisp]: https://www.npmjs.com/package/eslisp