简体   繁体   中英

How to insert array subscript in a parenscript expression?

I have this code working in javascript:

(document.querySelectorAll('[rel="next"]'))[0].click()

I am trying to write the same thing in parenscript (a library from the Common Lisp ecossystem). The expressions in my current sketches are being evalued by the REPL under a package called Nyxt availabe here .

CL-USER> (in-package :nyxt)

Nyxt is designed to be an infinitely extensible browser. Thus, the user can change the code and/or create extensions while the program is running. This is live hackability by design.

Here is the code:

(ps:chain document (query-selector-all "[rel=next]")) (click))

Using ps:ps I can "see" what parenscript is building:

NYXT> (ps:ps (ps:chain document (query-selector-all "[rel=next]") (click)))
"document.querySelectorAll('[rel=next]').click();"

The above result is close to what I need. However, there is still one thing missing: the subscription of the parenscript array (id. est., "[0]" in Javascript), before the .click() .

I have some failed attempts .

  1. If I try:
NYXT> (ps:ps (ps:chain document (query-selector-all "[rel=next]") [0] (click)))

I get an extra ".":

"document.querySelectorAll('[rel=next]').[0].click();"
  1. If I try:
NYXT> (ps:ps (ps:chain document (query-selector-all "[rel=next]") ([0]) (click)))

I get an extra "." and unnecessary "()":

"document.querySelectorAll('[rel=next]').[0]().click();"
  1. If I try:
NYXT> (ps:ps (ps:chain document (query-selector-all "[rel=next]")[0] (click)))

I get an extra ".":

"document.querySelectorAll('[rel=next]').[0].click();"
  1. If I try:
NYXT> (ps:ps (ps:chain document (query-selector-all "[rel=next]" [0]) (click)))

I get an extra [0] as another argument:

"document.querySelectorAll('[rel=next]', [0]).click();"

How do I insert it?

Accidentaly, I discovered the answer, the trick was just to pass just 0 without [] :


NYXT> (ps:ps (ps:chain document  (query-selector-all "[rel=next]") 0 (click)))

"document.querySelectorAll('[rel=next]')[0].click();"

I think parenscript could have more tutorials and documentation. I have struggled with it. Thus, I am leaving this here to help other Common Lisp newbies like me :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM