Pull to refresh

Comments 2

Тема си Так и не понял, что такое алгебраические эффекты.
Думаю, простой пример кода мог бы помочь (лучше с пояснениями)
немного погуглил раз (пример на пальцах и js) и два (более абстрактно, но после первого — понятно о чем там речь) и

три
Отрывок
A simple example of a handler is:
handler {read(; k) 7→ k “Bob”}
which provides a constant input string “Bob” each time read is called. We can, of
course, generalise it to a function that takes a string s and returns a handler that
feeds it to read:
alwaysRead def = fun s 7→ handler {read(; k) 7→ k s}
This handler works as follows: whenever read is called, we ignore its unit parameter
and capture its continuation in a function k that expects the resulting string and
resumes the evaluation when applied. Next, instead of calling read, we evaluate
the computation in the handling clause: we resume the continuation k, but instead
of reading the string from interactive input, we yield the constant string s. The
handler implicitly continues to handle the continuation, so any read in the handled
computation again yields s. If the handled computation calls any operation other
than read, the call escapes the handler, but the handler again wraps itself around the
continuation so that it may handle any further read calls. For example, evaluating
with (alwaysRead “Bob”) handle printFullName
first prints out “What is your name?” as print is unhandled. Then, read is handled
so “Bob” gets bound to forename. Similarly, the second print is unhandled, and
in the second read, “Bob” gets bound to surname as well and finally “Bob Bob” is
printed out.

— т.е. если кто знает что такое Dependency Injection и как работает async / await / yield в джаваскрипте (или других языках) — то несложно понять аналогию работы: в контексте вызовов у вас целевая функция может быть подменена, а при выходе за контекст вызова — ее поведение вернется обратно, вот и все.
Sign up to leave a comment.