Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

96
Views
Traducir call/comp al estilo CPS equivalente en JS

Ya leí muchos artículos sobre cómo traducir call/cc al estilo CPS equivalente y ya sé lo básico, pero actualmente no entiendo cómo la raqueta realiza la transformación con call/comp (también conocido como call-with-composable-continuation) y me quedé atascado. aquí.

Lo que hice primero fue intentar traducir este fragmento de código de raqueta:

 #lang racket/base

(let ([tag (make-continuation-prompt-tag)])
 (println "111")
 (println (string-append "222"
 (call-with-continuation-prompt
 (lambda ()
 (println (string-append "333"
 (call-with-composable-continuation
 (lambda (k)
 (println (string-append "444" (k "555")))
 ;; (abort-current-continuation tag "bbb")
 "666")
 tag)))
 "777")
 tag
 (lambda (k)
 (println (string-append "888" k))
 "aaa"
 )))))

En código JS equivalente. Aquí mi intento actual hasta ahora:

 var prompt = (fn, tag, r0) => { // call-with-continuation-prompt
 // Incorrect
 // r0(fn((p0) => {
 // r0(p0);
 // }));
};

var comp = (fn, tag, r0) => { // call-with-composable-continuation
 // Incorrect
 // fn((pass, r1) => {
 // r0(r1(pass));
 // });
};

var abort = () => { // abort-current-continuation

};

((r0) => {
 console.log("111");
 ((r1) => {
 prompt((r2) => {
 ((r3)=>{
 comp((k, r4) => {
 k("555", (r5) => {
 console.log("444" + r5);
 r4("666");
 });
 }, "tag", r3);
 })
 ((p3) => {
 console.log("333" + p3);
 r2("777");
 });
 }, "tag", r1);
 })
 ((p1) => {
 console.log("222" + p1);
 r0();
 });
})
((p0) => {
 throw 0; // throw 0 to exit early
});

¿Cómo debo llenar prompt y comp aquí? ¿Me perdí un paso de CPS? ¿Es posible la transformación?

almost 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Después de un tiempo con muchas pruebas y errores, se me ocurrió esta solución:

 var give = (p, rs, rt) => { // return value from function
 rs(p, rt);
};

var exit = (p, rr) => { // exit program?
 give(p, rr, rr);
};

var prompt = (fn, r_ab, r_cx) => {
 fn(
 (p_fn, r_fn) => {
 if (r_fn == null) // Check if we are aborting
 r_ab(p_fn, (p_ab, r_ab_) => {
 give(p_ab, r_ab_, r_cx);
 });
 else
 give(p_fn, r_fn, r_cx);
 }
 );
};

var comp = (fn, r_cx) => {
 fn(
 (p_fn, r_fn) => { // Function composition
 r_cx(p_fn, (p_cc) => {
 give(p_cc, r_fn, (p_cx) => {
 r_cx(p_cx, (p_out, r_out) => {
 exit(p_out, r_out);
 });
 });
 });
 },
 (p_fn, r_fn) => {
 give(p_fn, r_fn, r_cx);
 },
 );
};

var abort = (fn, rr) => {
 fn((p_fn) => {
 rr(p_fn, null);
 });
};

((r0) => {
 console.log("111");
 prompt(
 (r1) => {
 comp(
 (k, r2) => {
 // return give("666", r2, r1); // Or comment k(...) and uncomment this
 k(
 "555",
 (pk, rk_) => {
 console.log("444" + pk);
 /* <-- Flip between these two
 abort(
 (ra) => {
 ra("bbb");
 },
 r1
 );
 /*/
 give("666", rk_, r2);
 //*/
 },
 );
 },
 (p2, r2_) => {
 console.log("333" + p2);
 give("777", r2_, r1);
 },
 );
 },
 (pa, ra_) => {
 console.log("888" + pa);
 exit("aaa", ra_);
 },
 (p1, r1_) => {
 console.log("222" + p1);
 exit(0, r0); // Should end the program
 },
 );
})
((p0, r0_) => {
 give(p0, r0_, (p0_) => {
 console.log("out: ", p0_);
 });
});

Honestamente, todavía no estoy seguro de si mi implementación es correcta para todos los casos posibles con CPS válido. ¿Cualquier pensamiento?

almost 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!