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. oracle中用while循环查询1到100的质数(素数)

    declare i number:=1;  --表示当前数字 j number:=0;  --从2开始,存储判断的数字 sum1 number:=0;--总数begin while(i<100) ...

  2. table-layout:fixed; 表格比例固定

    固定表格布局: 固定表格布局与自动表格布局相比,允许浏览器更快地对表格进行布局. 在固定表格布局中,水平布局仅取决于表格宽度.列宽度.表格边框宽度.单元格间距,而与单元格的内容无关. 通过使用固定表格 ...

  3. 【转】QT 添加外部库文件

    转自:Qt 添加外部库文件 LIBS += D:\Code\Opengltest\OpenGL32.Lib D:\Code\Opengltest\GlU32.Lib # 直接加绝对路径 LIBS += ...

  4. Problem 4: Largest palindrome product

    A palindromic number reads the same both ways. The largest palindrome made from the product of two 2 ...

  5. Python PIL

    Python PIL PIL (Python Image Library) 库是Python 语言的一个第三方库,PIL库支持图像存储.显示和处理,能够处理几乎所有格式的图片. 一.PIL库简介 1. ...

  6. 移动端设备中1px适配

    方式1:伪类+transform实现,主要用transform中的scale缩放,缩放默认中心点是以x,y轴的50%处,因此需要用transform-origin调整中心点 html代码: <d ...

  7. vue2.0 添加监听滚动事件

    export default { data () { return { isFixed: true } }, mounted () { window.addEventListener('scroll' ...

  8. java面向对象编程(九)--final

    1.final概念 final可以修饰变量或者方法.在某些情况下,程序员可能有以下需求: a.当不希望父类的某个方法被子类覆盖(override)时,可以用final关键字修饰. b.当不希望类的某个 ...

  9. mysql 存储 2

    mysql> create database db1; mysql> use db1; mysql> create table PLAYERS as select * from TE ...

  10. tuxedo 提供buildserver命令编译服务器进程

    转自:http://blog.sina.com.cn/s/blog_5413cc0f0100nbgc.html 事实上buildserver只完成预编译,它会调用当前操作系统中已经安装的默认C编译器来 ...