In package pryr, there is a function called parent_promise.
I know what a promise is, but I'm not familiar with the term parent promise. Furthermore, I don't really understand the example in the documentation, perhaps because I don't know what I'm looking for.
library(pryr)
example(parent_promise)
# prnt_p> f <- function(x) g(x)
# prnt_p> g <- function(y) h(y)
# prnt_p> h <- function(z) parent_promise(z)
# prnt_p> h(x + 1)
# x + 1
# prnt_p> g(x + 1)
# x + 1
# prnt_p> f(x + 1)
# x + 1
To help me get a better understanding of the above example, can someone explain what a parent promise is, and if/how it differs from a regular promise?
There's no special thing called a "parent promise." There are just promises. But a promise might point to another promise. The parent_promise function basically walks up the chain of promises to find the first non-promise.
So when you call f(x), that in turn calls g(y) with y (promise)-> x. Since you never evaluate y, that parameter is passed along as a promise to h(z) with z (promise)-> y. So
z (promise)-> y (promise)-> x (promise)-> x+1
So calling parent_promise(z) goes up the chain to find the first non-promise object which in each of these cases is the expression x+1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With