Functions and Methods 函数和方法 agentzh ( 章亦春 )

Similar documents
Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

JavaIO.PDF

Lorem ipsum dolor sit amet, consectetuer adipiscing elit

6-1 Table Column Data Type Row Record 1. DBMS 2. DBMS MySQL Microsoft Access SQL Server Oracle 3. ODBC SQL 1. Structured Query Language 2. IBM

CC213

chp6.ppt

软件测试(TA07)第一学期考试

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

Microsoft Word - 第四組心得.doc

1505.indd

Strings

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO

Microsoft Word doc

Preface This guide is intended to standardize the use of the WeChat brand and ensure the brand's integrity and consistency. The guide applies to all d

第3章.doc

EJB-Programming-3.PDF

untitled

4. 每 组 学 生 将 写 有 习 语 和 含 义 的 两 组 卡 片 分 别 洗 牌, 将 顺 序 打 乱, 然 后 将 两 组 卡 片 反 面 朝 上 置 于 课 桌 上 5. 学 生 依 次 从 两 组 卡 片 中 各 抽 取 一 张, 展 示 给 小 组 成 员, 并 大 声 朗 读 卡

Chapter 9: Objects and Classes

<4D F736F F D205F FB942A5CEA668B443C5E9BB73A740B5D8A4E5B8C9A552B1D0A7F75FA6BFB1A4ACFC2E646F63>

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf ("%d", & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9

SDK 概要 使用 Maven 的用户可以从 Maven 库中搜索 "odps-sdk" 获取不同版本的 Java SDK: 包名 odps-sdk-core odps-sdk-commons odps-sdk-udf odps-sdk-mapred odps-sdk-graph 描述 ODPS 基


Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.07.doc

C++ 程式設計

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

全国计算机技术与软件专业技术资格(水平)考试

[ 13 年 12 月 06 日, 下 午 6 点 24 分 ] Intel Hosts 新 加 入 的 同 学 们, 快 去 听 听 在 线 宣 讲 会 哦, 同 时 完 成 页 面 下 方 有 奖 调 查, 就 有 资 格 参 与 大 奖 抽 取 啦! [ 13 年 12 月 06 日, 下 午

untitled

Microsoft Word - 11月電子報1130.doc

EJB-Programming-4-cn.doc

untitled

2015年4月11日雅思阅读预测机经(新东方版)

國立中山大學學位論文典藏

Chn 116 Neh.d.01.nis

硕 士 学 位 论 文 论 文 题 目 : 北 岛 诗 歌 创 作 的 双 重 困 境 专 业 名 称 : 中 国 现 当 代 文 学 研 究 方 向 : 中 国 新 诗 研 究 论 文 作 者 : 奚 荣 荣 指 导 老 师 : 姜 玉 琴 2014 年 12 月

Microsoft Word - 24.doc

參 加 第 二 次 pesta 的 我, 在 是 次 交 流 營 上 除 了, 與 兩 年 沒 有 見 面 的 朋 友 再 次 相 聚, 加 深 友 誼 外, 更 獲 得 與 上 屆 不 同 的 體 驗 和 經 歴 比 較 起 香 港 和 馬 來 西 亞 的 活 動 模 式, 確 是 有 不 同 特

内 容 简 介 本 书 是 一 本 关 于 语 言 程 序 设 计 的 教 材, 涵 盖 了 语 言 的 基 本 语 法 和 编 程 技 术, 其 中 包 含 了 作 者 对 语 言 多 年 开 发 经 验 的 总 结, 目 的 是 让 初 学 的 读 者 感 受 到 语 言 的 魅 力, 并 掌

致 谢 本 人 自 2008 年 6 月 从 上 海 外 国 语 大 学 毕 业 之 后, 于 2010 年 3 月 再 次 进 入 上 外, 非 常 有 幸 成 为 汉 语 国 际 教 育 专 业 的 研 究 生 回 顾 三 年 以 来 的 学 习 和 生 活, 顿 时 感 觉 这 段 时 间 也

余德浩诗词


Logitech Wireless Combo MK45 English

mvc

数据分析技术介绍

一 課 後 社 團 名 稱 :B02. 直 排 輪 校 隊 C 班 ( 校 隊 班 ) 二 授 課 教 師 : 劉 輔 人 助 教 : 杜 翊 嘉 世 界 盃 滑 輪 溜 冰 錦 標 賽 世 界 冠 軍 榮 獲 VOUGE 時 尚 雜 誌 專 訪 同 週 一 校 隊 班 介 紹

(baking powder) 1 ( ) ( ) 1 10g g (two level design, D-optimal) 32 1/2 fraction Two Level Fractional Factorial Design D-Optimal D

The Development of Color Constancy and Calibration System

Microsoft Word - 口試本封面.doc

Microsoft Word 軟體設計第二部份範例試題_C++_ _1_.doc

基于ECO的UML模型驱动的数据库应用开发1.doc

Fuzzy Highlight.ppt

目 錄 實 施 計 畫 1 專 題 演 講 因 應 十 二 年 國 民 基 本 教 育 課 程 綱 要 學 校 本 位 課 程 的 整 體 布 局 A-1 推 動 十 二 年 國 民 基 本 教 育 課 程 綱 要 相 關 配 套 措 施 A-10 分 組 研 討 法 規 研 修 B-1 課 程 教

Windows XP

2-7.FIT)

國立桃園高中96學年度新生始業輔導新生手冊目錄

1.JasperReport ireport JasperReport ireport JDK JDK JDK JDK ant ant...6

翻 那 本 日 记, 好 像 和 自 己 赌 气, 好 像 那 个 人 还 在 乎 一 样, 里 面 的 内 容, 因 为 在 较 着 劲, 就 是 不 愿 意 去 看 那 些 甜 蜜 的 过 往 小 小 的 日 记 本 塞 在 行 李 箱 的 底 部, 安 静 地 呆 在 那 儿, 只 是, 不

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

untitled

Microsoft Word - Final Exam Review Packet.docx

ENGG1410-F Tutorial 6

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F

1.第二卷第二期p1

準 備 第 1~2 週 服 務 第 3~15 週 反 省 第 16~17 週 慶 賀 第 18 週 六 合 作 機 構 協 力 單 位 合 作 協 議 書 : 已 簽 及 附 影 本 乙 份 未 簽, 核 准 之 後 補 送 機 構 名 稱 / 聯 絡 人 台 中 市 青 年 志 工 中 心 財 團

BC04 Module_antenna__ doc

使 用 Java 语 言 模 拟 保 险 箱 容 量 门 板 厚 度 箱 体 厚 度 属 性 锁 具 类 型 开 保 险 箱 关 保 险 箱 动 作 存 取 款

诚 实 守 信 公 平 交 易 好 的 伦 理 为 经 营 之 道 我 们 的 价 值 观 我 们 的 日 常 工 作 让 客 户 和 消 费 者 展 露 微 笑 我 们 关 注 员 工 产 品 和 业 务 的 不 断 改 善 和 进 步 我 们 珍 视 我 能 做 到 的 态 度 和 精 神, 尝

Microsoft PowerPoint - L17_Inheritance_v4.pptx

前 言 香 港 中 文 大 學 優 質 學 校 改 進 計 劃 ( 下 稱 計 劃 ) 團 隊 自 1998 年 起 積 極 於 本 地 推 動 理 論 及 實 踐 並 重 的 學 校 改 進 工 作, 並 逐 步 發 展 成 為 本 地 最 具 規 模 的 校 本 支 援 服 務 品 牌, 曾 支

untitled

Java

新竹市建華國民中學九十四學年度課程計畫

提纲 1 2 OS Examples for 3

A. 蛋 餅 + 生 菜 = 營 養 更 均 衡 : 蛋 餅 是 油 餅 皮 加 上 一 顆 蛋, 含 有 人 體 所 需 的 碳 水 化 合 物 與 蛋 白 質, 但 少 了 膳 食 纖 維, 建 議 點 蛋 餅 時, 請 店 家 在 蛋 餅 中 加 點 生 菜, 營 養 會 更 均 衡 B. 鐵

untitled

untitled

中國的科學與中國的公民:大陸研究在台灣的困境\\

44 生 活 與 生 涯 規 劃 : 彩 繪 生 命 的 藍 圖 : 生 涯 規 劃 :something you should know 學 習 規 劃 與 生 涯 發 展 192.1/1133/ 給 青 年

(Microsoft Word - 10\246~\253\327\262\304\244@\264\301\256\325\260T_Version4)

59 1 CSpace 2 CSpace CSpace URL CSpace 1 CSpace URL 2 Lucene 3 ID 4 ID Web 1. 2 CSpace LireSolr 3 LireSolr 3 Web LireSolr ID

240 生 异 性 相 吸 的 异 性 效 应 [6] 虽 然, 心 理 学 基 础 研 [7-8] 究 已 经 证 实 存 在 异 性 相 吸 异 性 相 吸 是 否 存 在 于 名 字 认 知 识 别 尚 无 报 道 本 实 验 选 取 不 同 性 别 的 名 字 作 为 刺 激 材 料, 通


提问袁小兵:

士 学 位 资 格 进 行 了 全 面 审 查 各 二 级 学 院 于 6 月 6 日 至 12 日 对 审 查 结 果 进 行 了 公 示 毕 业 生 毕 业 资 格 审 查 包 括 学 籍 审 查 德 育 审 查 和 学 业 审 查 ; 毕 业 生 学 士 学 位 资 格 审 核 严 格 按 照

2015年下半年全国教师资格笔试《地理学科知识与教学能力》备考指导

small fire indd

< F5FB77CB6BCBD672028B0B6A46AABE4B751A874A643295F5FB8D5C5AA28A668ADB6292E706466>

國 史 館 館 刊 第 23 期 Chiang Ching-kuo s Educational Innovation in Southern Jiangxi and Its Effects ( ) Abstract Wen-yuan Chu * Chiang Ching-kuo wa

Microsoft Word - SupplyIT manual 3_cn_david.doc

课题调查对象:

Theory of Groups is another course for undergraduates; and Module Theory will be a basic course of graduates). The main contents include the basic str

untitled

Gerotor Motors Series Dimensions A,B C T L L G1/2 M G1/ A 4 C H4 E

FileMaker 16 ODBC 和 JDBC 指南

穨control.PDF

168 健 等 木醋对几种小浆果扦插繁殖的影响 第1期 the view of the comprehensive rooting quality, spraying wood vinegar can change rooting situation, and the optimal concent

Microsoft Word - TIP006SCH Uni-edit Writing Tip - Presentperfecttenseandpasttenseinyourintroduction readytopublish

致 谢 本 论 文 能 得 以 完 成, 首 先 要 感 谢 我 的 导 师 胡 曙 中 教 授 正 是 他 的 悉 心 指 导 和 关 怀 下, 我 才 能 够 最 终 选 定 了 研 究 方 向, 确 定 了 论 文 题 目, 并 逐 步 深 化 了 对 研 究 课 题 的 认 识, 从 而 一

第 15 章 程 式 編 写 語 言 15.1 程 式 編 写 語 言 的 角 色 程 式 編 寫 語 言 是 程 式 編 寫 員 與 電 腦 溝 通 的 界 面 語 法 是 一 組 規 則 讓 程 式 編 寫 員 將 字 詞 集 合 起 來 電 腦 是 處 理 位 元 和 字 節 的 機 器, 與

Transcription:

Functions and Methods

Functions and Methods 函数和方法 agentzh ( 章亦春 ) 2006.10

Consistent interface, Simple interface, rich interface 一致的接口, 简单的接口, 丰富的接口

C# seems to have hundreds of different collection classes, used inconsistently in the.net libraries. -- Ned Batchelder C# 似乎拥有成百上千个不同的集合类, 它 们在.NET 类库中的使用是如此的不一致

Perl 6's array Simple interface with rich functionalities Perl 6 数组 : 简单的接口, 丰富的功能

# use it as an ordinary array: @item = 'dog', 'cat', 'mouse', 'tiger'; @item[0].say; # prints 'dog' say ~@item[1..-1]; # prints 'cat mouse tiger' @item[2] = 'human'; @item.push('camel', 'moose'); # append elements @item = (@item, 'camel', 'moose'); # just the same

# use it as a stack (FILO): @item.push('moose'); push @item, 'elk'; # just another way $top = @item[-1]; $top = @item.pop(); $top = @item.pop; # ditto

# use it as a queue (FIFO): @item.unshift(256); unshift @item, 128, 'hello'; $elem = @item.shift(); $elem = @item.shift; $elem = shift @item; say "length of the queue: ", @elem.elems;

Make simple things easy and hard things possible. -- Larry Wall 让简单的事情很容易办到, 让困难的事情有可能办到

Writing a string to a file should be a simple task, however... 向一个文件写入一个字符串本该是 一件很简单的任务, 可是

# The Java way: import java.io.*; class WriteFile { public static void main(string args[]) { FileOutputStream fostream; PrintStream pstream; try { fostream = new FileOutputStream("somefile.txt"); pstream = new PrintStream( fostream ); pstream.println ("This is written to a file"); pstream.close(); } catch (Exception e) { System.err.println ("Error writing to file " + e); } } }

/* The C way: */ #include <stdio.h> #include <stdlib.h> int main() { FILE* fh; if ((fh = fopen("somefile.txt", "w") == NULL) { fprintf(stderr, "Can't open file: %s", strerror(errno)); return 1; } fprintf(fh, "This is written to a file\n"); fclose(fh); return 0; }

# The Perl 5 way: open my $fh, "> somefile.txt" or die "Can't open file: $!"; print $fh "This is written to a file\n" or die "Can't print to file: $!"; close $fh;

output arguments <=> return values 输出参数 <=> 返回值

/* pass results via return values */ int add(int a, int b) { return a + b; }... if (add(1, 2) == 3) { printf("ok"); } printf("%d", add(5, 6));

/* pass results via output arguments */ void add(int a, int b, int& c) { c = a + b; }... int temp; add(1, 2, temp); if (temp == 3) { printf("ok"); } add(5, 6, temp); printf("%d", temp);

Multiple return values 多重返回值

# The Perl 6 way: sub div ($a, $b) { return ($a/$b, $a%$b); }... my ($quo, $rem) = div(5, 2);

/* The C way: */ void div (int a, int b, int* ptr_quo, int* ptr_rem) { *ptr_quo = a / b, *ptr_rem = a % b; }... int quo, rem; div(5, 2, &quo, &rem);

// The C++ way: void div (int a, int b, int& quo, int& rem) { quo = a / b; rem = a % b; }... int quo, rem; div(5, 2, quo, rem);

The power of notation 记法的威力

Inventing a language doesn't necessarily mean building the successor to Java; often a thorny problem can be cleared up by a change of notation... -- "The Practice of Programming" 发明一种语言并不一定意味着创建 Java 的继承者 ; 通 常一个很棘手的问题可以通过记法上的改变而获得澄清 -- 程序设计实践

Case #1: Formated output 案例 #1: 格式化输出

// The C++ style: cout << "{ list[" << i << "][" << j << "] = " << elems[i][j] << " }\n";

// The C++ style: cout << "{ list[" << i << "][" << j << "] = " << elems[i][j] << " }\n"; This is ugly.

// The C style: printf("{ list[%d][%d] = %f }\n", i, j, elems[i][j]);

// The C style: printf("{ list[%d][%d] = %f }\n", i, j, elems[i][j]); This is much better.

The problem is that many developers choose the solution before defining the problem. It's not the case that any programming language is "one size fits all". -- Ovid 问题就在于许多开发人员在定义问题之前就选定了解 决方案 并不存在一种编程语言能做到 一劳永逸

Case #2: A simple-minded CSV file parser in Perl 6 案例 #2: 一个简单的逗号分隔 (CSV) 文件的解析器 ( 使用 Perl 6)

my $csv_src = slurp $csv_file; my @lines = split /[\n\s]+/, $csv_src; for @lines -> $line { my @fields = split /\s*,\s*/, $line; # process the fields here... }

Job done!

In large applications covering problem domains suitable for both Perl and Java, the Java programmer can't hold a candle to me. -- Ovid 对于 Perl 和 Java 都适合的大型应用, Java 程序员根本无法赶上我的编程效率

Higher order functions (λ calculus) 高阶函数 (λ 演算 )

C# 3.0 ( C# Orcas ) introduces several language extensions that build on C# 2.0 to support the creation and use of higher order, functional style class libraries. The extensions enable construction of compositional APIs that have equal expressive power of query languages in domains such as relational databases and XML. -- C# Version 3.0 Specification

Closures A piece of code manipulable in arbitrary contexts

my $foo = { say 1 + 2 }; my $bar = $foo; $bar.(); # prints 3 $foo.(); # ditto

Closures can remember the context in which it was created.

sub factory($seed) { return { say $seed }; } my $foo = factory(7); $foo.(); # prints 7 my $bar = factory(100); $bar.(); # prints 100 $foo.(); # still prints 7

Higher order functions 0 loops 高阶函数 0 循环

Case #1: We want to print out all the elements contained in an array of arrays. 案例 #1: 我们想打印出一个数组的数组里的所有元素

// The C# way (using loops): int[][] elems = new int[][] { new int[] {1,2}, new int[] {3,4,5} }; for (int i = 0; i < elems.length; i++) for (int j = 0; j < elems[i].length; j++) Console.WriteLine(elems[i][j]);

# The Perl 6 way: my @elems = [1,2], [3,4,5]; map { map {.say }, $\_ }, @elems;

# Anyway, we can still use loops in Perl 6: my @elems = [1,2], [3,4,5]; for @elems { for \@\$\_ {.say } }

Case #2: We want to filter out customers with income higher than $ 5000. 案例 #2: 我们想到筛选出所有收入在 5000 美元以上的客户

# Traditional way in Perl 6 (using loops): my @res; for @customers -> $customer { if $customer.income() > 5000 { push @res, $customer; } }

# As before, but more concise: my @res; for @customers { if.income > 5000 { push @res, $\_; } }

# A functional-style solution: my @res = grep {.income > 5000 }, @customers;

Higher order functions As expressive as SQL 高阶函数 拥有和 SQL 一样的表达力

Case #1: Customer filtering and sorting 案例 #1: 客户筛选与排序

select * from customers where income > 5000 and gender = 'female' order by name

my @res = sort { $^a.name cmp $^b.name }, grep {.income > 5000 and.gender eq 'female' }, @customers;

Case #2: Boy student statistics 案例 #2: 男生统计信息

select class_id, count(*) as count from students where gender = 'male' group by class_id order by count desc

my %class; map { %class{.class_id}++ }, grep {.gender eq 'male' }, @student; my @res = reverse sort { $^a.value <=> $^b.value }, %class.pairs;

JIT slide making... 即时幻灯片制做

Thank you!