A Tour of Go Exercise: Fibonacci closure
Let's have some fun with functions.
Implement a fibonacci
function that returns a function (a closure) that returns successive fibonacci numbers.
package main import "fmt" // fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
before :=
now :=
return func() int {
if before == {
before++
return before
}
if now == {
now++
return now
}
temp := now
now = before + now
before = temp
return now
}
} func main() {
f := fibonacci()
for i := ; i < ; i++ {
fmt.Println(f())
}
}
A Tour of Go Exercise: Fibonacci closure的更多相关文章
- A Tour of Go Exercise: Images
Remember the picture generator you wrote earlier? Let's write another one, but this time it will ret ...
- A Tour of Go Exercise: HTTP Handlers
Implement the following types and define ServeHTTP methods on them. Register them to handle specific ...
- A Tour of Go Exercise: Errors
Copy your Sqrt function from the earlier exercises and modify it to return an error value. Sqrt shou ...
- A Tour of Go Exercise: Maps
Implement WordCount. It should return a map of the counts of each “word” in the string s. The wc.Tes ...
- A Tour of Go Exercise: Slices
Implement Pic. It should return a slice of length dy, each element of which is a slice of dx 8-bit u ...
- A Tour of Go Exercise: Loops and Functions
As a simple way to play with functions and loops, implement the square root function using Newton's ...
- exercise.tour.go google的go官方教程答案
/* Exercise: Loops and Functions #43 */ package main import ( "fmt" "math" ) fun ...
- go 准备
坚持每天抽点时间 学习联系 go 语法 主要参考 https://tour.golang.org 官方导向,英语不好的可以切换到中文版本.这个之前都是墙外面的,只能访问国内映像地址 吐槽一下就是 里面 ...
- Go for Pythonistas Go and the Zen of Python 禅
Go for Pythonistas https://talks.golang.org/2013/go4python.slide#1 Things I don't like about Python ...
随机推荐
- <context:component-scan>配置解析(转)
在xml配置了这个标签后,spring可以自动去扫描base-pack下和其子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注 ...
- 用java运行Hadoop程序报错:org.apache.hadoop.fs.LocalFileSystem cannot be cast to org.apache.
用java运行Hadoop例程报错:org.apache.hadoop.fs.LocalFileSystem cannot be cast to org.apache.所写代码如下: package ...
- EXPORT_SYMBOL的作用是什么
http://www.cnblogs.com/riskyer/p/3221805.html EXPORT_SYMBOL只出现在2.6内核中,在2.4内核默认的非static 函数和变量都会自动 导入到 ...
- SGU 168
SGU 168,寻找矩阵中右上方,右方,下方最小的元素,采用动态规划解答. #include <iostream> #include <vector> #include < ...
- P73、面试题9:斐波那契数列
题目一:写一个函数,输入n,求斐波那契数列(Fibonacci)数列的第n项,斐波那契数列的定义如下: f(n) = {0 n = 0; 1 n = 1; f(n-1)+f(n-2) n& ...
- nginx负载均衡 - session失效
最近迷上了Nginx,真实麻雀虽小,五脏俱全..功能实在强大.. nginx不单可以作为强大的web服务器,也可以作为一个反向代理服务器,而且nginx还可以按照调度规则实现动态.静态页面的分离,可以 ...
- HTML5学习(十)---Web Workers
参考教程:http://www.w3school.com.cn/html5/html_5_webworkers.asp web worker 是运行在后台的 JavaScript,不会影响页面的性能. ...
- poj 2506 Tiling(递推 大数)
题目:http://poj.org/problem?id=2506 题解:f[n]=f[n-2]*2+f[n-1],主要是大数的相加; 以前做过了的 #include<stdio.h> # ...
- struts2的action中获得request response session 对象
在struts2中有两种方式可以得到这些对象 1.非IoC方式 要获得上述对象,关键Struts 2中com.opensymphony.xwork2.ActionContext类.我们可以通过它的静态 ...
- poj1989
一道非常神奇的题目 var v:array[0..10010] of boolean; n,k,i,x,ans,s:longint; begin readln(n,k); fillchar(v,siz ...