当前位置:网站首页>The evolution history of Go programmers
The evolution history of Go programmers
2022-08-10 20:56:00 【User 9959267】
“写 n! several evolutions(退化)史
初级程序员
package fac
func Factorial(n int) int {
res := 1
for i := 1; i <= n; i++ {
res *= i
}
return res
}
Programmers who learned recursion
package fac
func Factorial(n int) int {
if n == 0 {
return 1
} else {
return Factorial(n - 1) * n
}
}
Programmers who learned generics
package fac
func Factorial(n interface{}) interface{} {
v, valid := n.(int)
if !valid {
return 0
}
res := 1
for i := 1; i <= v; i++ {
res *= i
}
return res
}
学了Goroutine的程序员
package fac
import "sync"
func Factorial(n int) int {
var (
left, right = 1, 1
wg sync.WaitGroup
)
wg.Add(2)
pivot := n / 2
go func() {
for i := 1; i < pivot; i++ {
left *= i
}
wg.Done()
}()
go func() {
for i := pivot; i <= n; i++ {
right *= i
}
wg.Done()
}()
wg.Wait()
return left * right
}
学了Channel的程序员
package fac
func Factorial(n int) <-chan int {
ch := make(chan int)
go func() {
prev := 1
for i := 1; i <= n; i++ {
v := prev * i
ch <- v
prev = v
}
close(ch)
}()
return ch
}
just turnGo的Java程序员
package fac
/**
* @see https://en.wikipedia.org/wiki/Factorial
*/
type IFactorial interface {
CalculateFactorial() int
}
// FactorialImpl implements IFactorial.
var _ IFactorial = (*FactorialImpl)(nil)
/**
* Used to find factorial of the n.
*/
type FactorialImpl struct {
/**
* The n.
*/
n int
}
/**
* Constructor of the FactorialImpl.
*
* @param n the n.
*/
func NewFactorial(n int) *FactorialImpl {
return &FactorialImpl{
n: n,
}
}
/**
* Gets the n to use in factorial function.
*
* @return int.
*/
func (this *FactorialImpl) GetN() int {
return this.n
}
/**
* Sets the n to use in factorial function.
*
* @param n the n.
* @return void.
*/
func (this *FactorialImpl) SetN(n int) {
this.n = n
}
/**
* Returns factorial of the n.
*
* @todo remove "if" statement. Maybe we should use a factory or somthing?
*
* @return int.
*/
func (this *FactorialImpl) CalculateFactorial() int {
if this.n == 0 {
return 1
}
n := this.n
this.n = this.n - 1
return this.CalculateFactorial() * n
}
高级程序员
package fac
// Factorial returns n!.
func Factorial(n int) int {
res := 1
for i := 1; i <= n; i++ {
res *= i
}
return res
}
Rob Pike
package fac
// Factorial returns n!.
func Factorial(n int) int {
res := 1
for i := 1; i <= n; i++ {
res *= i
}
return res
}
边栏推荐
猜你喜欢
单选点击可取消功能
机器学习模型验证:被低估的重要一环
@Autowired annotation --required a single bean, but 2 were found causes and solutions
npm‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件
面向未来的 IT 基础设施管理架构——融合云(Unified IaaS)
Demis Hassabis:AI 的强大,超乎我们的想象
铱钌合金/氧化铱仿生纳米酶|钯纳米酶|GMP-Pd纳米酶|金钯复合纳米酶|三元金属Pd-M-Ir纳米酶|中空金铂合金纳米笼核-多空二氧化硅壳纳米酶
多功能纳米酶Ag/PANI|柔性衬底纳米ZnO酶|铑片纳米酶|Ag-Rh合金纳米颗粒纳米酶|铱钌合金/氧化铱仿生纳米酶
Transferrin-modified vincristine-tetrandrine liposomes | transferrin-modified co-loaded paclitaxel and genistein liposomes (reagents)
C语言系列——猜名次、猜凶手、打印杨辉三角
随机推荐
【SemiDrive源码分析】【MailBox核间通信】52 - DCF Notify 实现原理分析 及 代码实战
Auto.js中APP应用相关指令
Colocate Join :ClickHouse的一种高性能分布式join查询模型
报错:runtime error: reference binding to null pointer of type ‘std::vector<int, std::allocator<int>>‘
【CMU博士论文】视频多模态学习:探索模型和任务复杂性,152页pdf
面向未来的 IT 基础设施管理架构——融合云(Unified IaaS)
C 语言 时间函数使用技巧(汇总)
设备管理中数据聚类处理
idea插件 协议 。。 公司申请软件用
Implementation of graceful exit in Golang
机器学习笔记:t-SNE
水溶性合金量子点纳米酶|CuMoS纳米酶|多孔硅基Pt(Au)纳米酶|[email protected]纳米模拟酶|PtCo合金纳米粒子
实施MES管理系统前,这三个问题要考虑好
多线程与高并发(五)—— 源码解析 ReentrantLock
「POJ 3666」Making the Grade 题解(两种做法)
苹果字体查找
[SWPUCTF 2021 新生赛] web
kuberentes Auditing 入门
【语义分割】2017-PSPNet CVPR
【图像分类】2018-MobileNetV2