Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String replace on async lambda #319

Closed
3 tasks done
jcubic opened this issue Feb 25, 2024 · 0 comments
Closed
3 tasks done

String replace on async lambda #319

jcubic opened this issue Feb 25, 2024 · 0 comments
Milestone

Comments

@jcubic
Copy link
Collaborator

jcubic commented Feb 25, 2024

I was working on the $ interpolation syntax extension (right now it needs to be #$), the code needs to look like this:

(define-macro (interpolate str)
  (typecheck "interpolate" str "string")
  (let ((values (gensym "values"))
        (_str (gensym "str"))
        (promises (gensym "promises"))
        (re (gensym "re")))
    `(let ((,promises (vector))
           (,re #/\$\{([^\}]+)\}/)
           (,_str ,str))
       (--> ,_str (replace ,re (lambda (_ expr)
                                 (let ((value '>(eval (read expr))))
                                   (--> ,promises (push value))))))
       (let ((,values (await (Promise.all ,promises))))
         (--> ,_str (replace ,re (lambda (_ expr)
                                   (--> ,values (shift)))))))))

The problem is that the eval code is async and you need to do gimmicks like quoting promises and using await with Promise.all to get the values.

The code is based on an idea from this StackOverflow JavaScript Q&A: javascript : Async/await in .replace.

What needs to be done:

  • Document as one of the problems with async code (together with Array::forEach)
  • Update replace function
  • Document to use replace instead of (--> str (replace (lambda or (str.replace (lambda
@jcubic jcubic added the bug Something isn't working label Feb 25, 2024
@jcubic jcubic added this to the 1.0 milestone Feb 25, 2024
@jcubic jcubic removed the bug Something isn't working label Mar 3, 2024
jcubic added a commit that referenced this issue Mar 5, 2024
@jcubic jcubic closed this as completed Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant