• Download Gist
Evolution of a Groovy programmer.groovy
Groovy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
// 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
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
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)

I thought expert Groovy programmers do pay attention and follow Groovy/Java coding style guidelines. In other words, an expert Groovy programmer would name the function factExpert rather than fact_expret, ey?

That's a good point. :-) I had thought about removing all of those _* names but that would unfortunately cause it to of course error. I'll make that change.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.