Expand

wrap

save a function call for later

<wrap> = wrap <command>

wrap's arguments are evaluated immediately, but function itself is not called. Instead function along with its arguments is boxed (or wrapped) together in a function-like object. In functional programming this is known as partial application or currying.

$cmd := wrap print "Crash!"
$cmd  = wrap $cmd  "Boom!"

# same as `print "Crash!" "Boom!" "Bang!"`
run $cmd "Bang!"

wrap is also useful to create callbacks of function in a local scope and pass it as an argument to an imported function.

a possible dry-run switch implementation
$kubectl := wrap kubectl
if $dryRun {
    $kubectl = wrap kubectl --dry-run=true
}
run $kubectl apply -f deploy.yaml
run $kubectl rollback deploy webserver
turn a local function into a callback
# This is a syntax example,
# http is not implemented yet.
import http

fn handler $req {
    print "<p>Hello, {$req.ip}!</p>"    
}

fn main {
    http serve :8080 (wrap handler)
}
create a closure
fn create-box {
    $state := null

    fn inner get    { put $state  }
    fn inner set $v { $state = $v }

    # return
    wrap inner
}

$box1 := create-box
$box2 := create-box

run $box1 set "foo"
run $box2 set "bar"
run $box2 set "baz"

print box1=(run $box1 get) # foo
print box2=(run $box2 get) # baz

See Also