注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

星期五

明天不上班

 
 
 

日志

 
 
关于我

一个特立独行的Java程序员,比较宅,上上网,写博客,听音乐,看电影。

网易考拉推荐

Evolution of a Groovy programmer.groovy(转载)  

2010-08-15 15:09:47|  分类: Groovy |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
今天在twitter上新认识一位好友 所以打算转载他的文章

一个Groovy程序员的进化
 
// Newbie programmer 新手程序员
int factorial_newbie(int n) {
    if (n == 0) {
        return 1
    } else {
        return n * factorial_newbie(n - 1)
    }
}
println factorial_newbie(6)
// First year programmer, studied Pascal 有Pascal背景的程序员
int factorial_pascal(int n) {
    int result = 1;
    for (int i = 1; i <= n; i++) {
        result = result * i;
    }
    return result;
}
println factorial_pascal(6)
// First year programmer, studied C 有C背景的程序员
int fact_c(int n) {
    int result = 1;
    for (int i = 1; i <= n; i++) {
        result *= i;
    }
    return result;
}
println fact_c(6)
// First year programmer, SICP
int fact_sicp(int n, int acc=1) {
    if (n > 1) {
        return (fact_sicp((n - 1), (acc * n)))
    } else {
        return (acc)
    }
}
println fact_sicp(6)
    
// First year programmer, Python
int Factorial_python(int n) {
    int res = 1
    (2..n).each { i ->
        res *= i
    }
    return res
}
println Factorial_python(6)
// Lazy Groovy programmer
int fact_lazy(int n) {
    return (n <= 1) ? 1 : n * fact_lazy(n - 1)
}
println fact_lazy(6)
// Lazier Groovy programmer
def factLazier = { (it <= 1) ? 1 : it * call(it - 1) }
println factLazier(6)
// Groovy expert programmer
def factExpert = { n -> n ? (1..n).inject(1, { a, b -> a * b }): 1 }
println factExpert(6)
// Unix programmer
def factorialUnix(int n) {
    def command = "factorial " + n
    def proc = command.execute()
    proc.waitFor()
    print proc.in.text
}
factorialUnix(6)
// Web designer
String factorialWeb(int n) {
    /*-------------------------------------------------
      --- Code snippet from The Math Vault ---
      --- Calculate factorial (C) Arthur Smith 1999 ---
      -------------------------------------------------*/
    String result = Integer.toString(1);
    int i = 1 //Thanks Adam
    while (i <= n) {
        //result = result * i; //It's faster to use *=
        //result = Integer.toString(result * result + i)
           //result = Integer.parseInt(result *= i) #??????
        result = Integer.toString(Integer.parseInt(result) * i)
        //result = new String(Integer.parseInt(result) * i)
        i = i + 1;
    }
    return result
}
println factorialWeb(6)
// Windows programmer
def CalculateAndPrintFactorialEx(dwNumber,
                                 hOutputDevice,
                                 lpLparam,
                                 lpWparam,
                                 lpsscSecurity,
                                 dwReserved) {
    if (lpsscSecurity != null) {
        return null; // Not implemented
    }
    int dwResult = 1;
    for (int dwCounter = 1; dwCounter <= dwNumber; dwCounter++) {
        dwResult *= dwCounter;
    }
    hOutputDevice.print(dwResult);
    hOutputDevice.print('\n');
    return 1;
}
CalculateAndPrintFactorialEx(6, System.out, null, null, null, null)
// Enterprise programmer
class InternalBase {
    private int base
    public InternalBase(Integer base) {
        this.base = base.intValue()
    }
    int getBase() {
        return new Integer(base)
    }
}
class StandardMathematicsSystem {
    private static StandardMathematicsSystem INSTANCE = null
    private Integer base
    private StandardMathematicsSystem(InternalBase base) throws RuntimeException {
        if (base.getBase().compareTo(new Integer(2)) != 0) {
            throw RuntimeException("Non base 2 bases are not supported.")
        }
        this.base = base.getBase()
    }
    int calculateFactorial(Integer target) {
        Integer result = new Integer(1)
        for (Integer i = new Integer(2); i.compareTo(target) <= 0; i = new Integer(i.intValue() + 1)) {
            result = result * i
        }
        return result
    }
    static private StandardMathematicsSystem createInstance(InternalBase base) {
        return new StandardMathematicsSystem(base)
    }
    static StandardMathematicsSystem getInstance(InternalBase base) {
        if (INSTANCE == null) {
            INSTANCE = createInstance(base)
        }
        return INSTANCE
    }
}
println StandardMathematicsSystem.getInstance(new InternalBase(new Integer(2))).calculateFactorial(new Integer(6))
        
// CPU parsimonious programmer
def factorial_cpu(int n) {
    def fact = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600, 6227020800, 87178291200, 1307674368000, 20922789888000, 355687428096000, 6402373705728000, 121645100408832000, 2432902008176640000]
    return fact[n]
}
println factorial_cpu(6)

看完后感觉真的很有意思 (看来每种程序员的code style都是不是一样的)
喜欢的话多多转载 大家一起努力推广Groovy Grails 将ruby python的特性一起用在Java上
  评论这张
 
阅读(165)| 评论(0)
推荐

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017