Lexical Scoping :有Java繼承中呼叫子類時先生成父類的概念,呼叫函數後,系統會轉至其定義處,將其 environment 中所具有的東西(有些可能定義在外層)形成 Closure [閉包]

Dynamic Scoping :呼叫處起算,逐漸往上層找

有閉包的lexical scoping是依據定義處逐層向外檢查Closure中變量是否存在,而dynamic scoping則是根據函數調用鏈逐層向外檢查變量是否存在

Execution environments : 每當function被呼叫,遵守 fresh start principle,會新生成封閉式環境,用以host execution,當function執行完成後,此環境將被清除。

Calling environments :    呼叫function時當下的封閉式環境

f()時的calling 環境中,所具有的參數為 (global)  x =0,  y = 10

g()時的calling 環境中,所具有的參數為 (f)  x =1 ,  (global)  y = 10

h()時的calling 環境中,所具有的參數為 (g)  x =2 ,  (global)  y = 10

將x設為3後,計算x+y=13,執行完成時f()、g()、h()各自所產生的封閉環境逐一瓦解,global 中的參樹依然沒有改變  x =0,  y = 10

parent.frame:The parent.frame refers to the environment where the function was called from, not where it was defined.

Closures  enclose the environment of the parent function and can access all its variables.

此時功能性(多層次)function設計可將參數劃分為兩種層級

  • parent level : 調控運作
  • child level: 實際運作

範例中以一個 parent function (power()) 生成兩個  child functions (square() 和 cube())

power <- function(exponent) {
function(x) {
x ^ exponent
}
} square <- power(2)
cube <- power(3)
square(2)
## [1] 4
cube(2)
## [1] 8

square被設定為次方,cube則被設定為立方,但檢視兩者時會發現並無明顯不同

square
## function(x) {
## x ^ exponent
## }
## <environment: 0x3719630> cube
## function(x) {
## x ^ exponent
## }
## <environment: 0x3870b58>

其實兩者的差異,是在於所擁有的environment包並不一致

square  :  <environment: 0x3719630>   
cube : <environment: 0x3870b58>

可用as.list(environment( ))  或是  pryr::unenclose() 查看environment包中的設定數值

as.list(environment(square))
## $exponent
## [1] 2
as.list(environment(cube))
## $exponent
## [1] 3
library(pryr)
unenclose(square)
## function (x)
## {
## x^2
## }
unenclose(cube)
## function (x)
## {
## x^3
## }

R中幾乎所有function都是Closure,Closure可獨立存取呼叫時所設立的資訊,而使其不會隨呼叫完成時一起消失 [有Java生成實體的感覺]

更多可參考   Advanced R by Hadley Wickham

[R] Lexical & Dynamic Scoping / Execution & Calling environments / Closures的更多相关文章

  1. R2—《R in Nutshell》 读书笔记(连载)

    R in Nutshell 前言 例子(nutshell包) 本书中的例子包括在nutshell的R包中,使用数据,需加载nutshell包 install.packages("nutshe ...

  2. R Programming week2 Functions and Scoping Rules

    A Diversion on Binding Values to Symbol When R tries to bind a value to a symbol,it searches through ...

  3. Coursera系列-R Programming第三周-词法作用域

    完成R Programming第三周 这周作业有点绕,更多地是通过一个缓存逆矩阵的案例,向我们示范[词法作用域 Lexical Scopping]的功效.但是作业里给出的函数有点绕口,花费了我们蛮多心 ...

  4. Static vs Dynamic Scope

    转自:http://hoolihan.net/blog-tim/2009/02/17/static-vs-dynamic-scope/ // start pseudo-code var y = &qu ...

  5. [转]Dynamic SQL & Stored Procedure Usage in T-SQL

    转自:http://www.sqlusa.com/bestpractices/training/scripts/dynamicsql/ Dynamic SQL & Stored Procedu ...

  6. 《R语言入门与实践》第一章:R基础

    前言 本章介绍了 R 语言的基础知识 界面: 使用命令 “ R “进行命令行的实时编译 对象 定义: 用于储存数据的,设定一个名称 格式: a <- 1:6 命名规则: 规则1:不能以数字开头规 ...

  7. 泡泡一分钟:Efficient Trajectory Planning for High Speed Flight in Unknown Environments

    张宁  Efficient Trajectory Planning for High Speed Flight in Unknown Environments 高效飞行在未知环境中的有效轨迹规划链接: ...

  8. bash5.0参考手册

    Bash Reference Manual a.summary-letter { text-decoration: none } blockquote.indentedblock { margin-r ...

  9. malware analysis、Sandbox Principles、Design && Implementation

    catalog . 引言 . sandbox introduction . Sandboxie . seccomp(short for secure computing mode): API级沙箱 . ...

随机推荐

  1. windows环境下,spring boot服务使用docker打包成镜像并推送到云服务器私有仓库

    最近在淘宝上学习springcloud教程,其中有几节课是讲解讲本地springboot服务打包成镜像并推送到云服务器私有仓库,但是教程里面用的事Mac环境,我的是Windows环境,而且课程里面没有 ...

  2. 视觉显著性简介 Saliency Detection

    内容转移到博客文章系列:显著性检测 1.简介 视觉显著性包括从下而上和从上往下两种机制.从下而上也可以认为是数据驱动,即图像本身对人的吸引,从上而下则是在人意识控制下对图像进行注意.科研主要做的是从下 ...

  3. Delphi10.2 关于Http 下载

    演示如图: 代码如下: unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Vari ...

  4. 目标检测(一) R-CNN

    R-CNN全称为 Region-CNN,它是第一个成功地将深度学习应用到目标检测的算法,后续的改进算法 Fast R-CNN.Faster R-CNN都是基于该算法. 传统方法 VS R-CNN 传统 ...

  5. 利用Excel-Vba进行多表汇总和数据透视表

    汇总表格式 详情表格式 要求根据汇总表中的信息,到详情表中查找详细物料的具体个数 最终,对物料的个数进行汇总,结果如下图: ExcelVba代码如下(有一些注释代码供参考) Sub Start() S ...

  6. uploadfy 图片/视频上传

    JS引入 <link href="../../Scripts/uploadify/uploadify.css" rel="stylesheet" /> ...

  7. matlab绘图与可视化

    1.设置图形对象属性值 set(h,'属性名称','属性值') >> subplot(,,); h1=line([ ],[ ]); text(,0.5,'unchange'); subpl ...

  8. 大数据处理N!(21<N<2000)

    输入: 每行输入1个正整数n,(0<n<1000 000) 输出: 对于每个n,输出n!的(十进制)位数 digit, 和最高位数firstNum.(n!约等于 firstNum * 10 ...

  9. c# 连接数据库SqlHelper

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  10. Autolayout Breakpoints

    articles archives team Autolayout Breakpoints Auto layout has become a crucial tool for iOS and OS X ...