当前位置:网站首页>Go程序员进化史
Go程序员进化史
2022-08-10 20:28:00 【用户9959267】
“写 n! 的若干种进化(退化)史
初级程序员
package fac
func Factorial(n int) int {
res := 1
for i := 1; i <= n; i++ {
res *= i
}
return res
}
学了递归的程序员
package fac
func Factorial(n int) int {
if n == 0 {
return 1
} else {
return Factorial(n - 1) * n
}
}
学了泛型的程序员
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
}
刚转Go的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
}
边栏推荐
- 铁蛋白颗粒Tf包载多肽/凝集素/细胞色素C/超氧化物歧化酶/多柔比星(定制服务)
- idea汉化教程[通俗易懂]
- Rider调试ASP.NET Core时报thread not gc-safe的解决方法
- 电信保温杯笔记——《统计学习方法(第二版)——李航》第17章 潜在语义分析
- svg+元素js实现在图片上描点成框,并获取相对图片的坐标位置
- 测试开发【Mock 平台】08 开发:项目管理(四)编辑功能和Component抽离
- 【一致性hash】负载均衡器分发请求
- 爱丁堡大学最新《因果机器学习: 医疗健康与精准医疗应用》2022综述
- QSslSocket has not been declared
- 《分布式微服务电商》专题(一)-项目简介
猜你喜欢

【语义分割】2015-UNet MICCAI
壁仞推出全球最大算力芯片,号称以7nm超越英伟达4nm最新GPU
[email protected])纳米酶"/>血红素-金纳米颗粒(Heme-AuNP)复合纳米酶|金纳米颗粒核多孔空心碳纳米球壳([email protected])纳米酶

The most complete GIS related software in history (CAD, FME, ArcGIS, ArcGISPro)

Multifunctional Nanozyme Ag/PANI | Flexible Substrate Nano ZnO Enzyme | Rhodium Sheet Nanozyme | Ag-Rh Alloy Nanoparticle Nanozyme | Iridium Ruthenium Alloy/Iridium Oxide Biomimetic Nanozyme

leetcode 547.省份数量 并查集

铁蛋白颗粒负载雷替曲塞/培美曲塞/磺胺地索辛/金刚烷(科研试剂)

XML小讲

.NET现代应用的产品设计 - DDD实践

WCF and TCP message communication practice, c # 】 【 realize group chat function
随机推荐
怎么完全卸载赛门铁克_Symantec卸载方法,赛门铁克卸载「建议收藏」
转铁蛋白(Tf)修饰去氢骆驼蓬碱磁纳米脂质体/香豆素-6脂质体/多柔比星脂质体
转铁蛋白Tf功能化β-榄香烯-雷公藤红素/紫杉醇PLGA纳米粒/雷公藤甲素脂质体(化学试剂)
深度学习实战教程(一):感知器
zip文件协议解析
【图像分类】2018-MobileNetV2
cordova 安装错误 Command failed: powershell 解决方法
壁仞推出全球最大算力芯片,号称以7nm超越英伟达4nm最新GPU
@Autowired annotation --required a single bean, but 2 were found causes and solutions
mysql性能监控与执行计划
Metasploit——渗透攻击模块(Exploit)
[SWPUCTF 2021 新生赛] web
leetcode 84.柱状图中最大的矩形 单调栈应用
验证码倒计时自定义hooks
Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户
sklearn 笔记 TSNE
MySQL数据库的主从复制部署详解
Transferrin-modified osthole long-circulating liposomes/PEG-PLGA nanoparticles loaded with notoginsenoside R1 ([email prot
LeetCode 1-10题
C语言系列——猜名次、猜凶手、打印杨辉三角