Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eslisp-chain

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslisp-chain

eslisp macro for chaining method calls on a single object

  • 0.0.1
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

eslisp-chain

An 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 where 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);

FAQs

Package last updated on 10 Oct 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc