/
hello_world.coffee
144 lines (122 loc) · 3.34 KB
/
hello_world.coffee
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
_ = require('underscore')
HelloWorld =
print: (str) ->
console.log str
product: () ->
_.reduce(arguments, ((x, y) -> x*y))
main: () ->
# stdout
this.print "Hello World"
# basic math
this.print "// basic math"
_sum = 0
_sum = 2.0 + 2.0
this.print _sum
sqrt = Math.sqrt(_sum)
this.print sqrt
# for and while loops; nested arrays; functions
this.print "// loops; nested arrays; functions"
n = 10
nsums = []
totalsum = 0
i = 0
while (i<n)
nsums.push Array(n)
for j in (num for num in [n-1..0])
nsums[i][j] = this.product(i,j)
totalsum += nsums[i][j]
i+=1
# if-conditionals
this.print "// if-conditionals"
if totalsum==2025
this.print "totalsum*5: " + totalsum*5
else if totalsum==0
this.print "totalsum is zero"
else
this.print "totalsum is off"
# function overload (what is this called again?)
this.print "// function overload"
this.print this.product(2,3,4)
# input coercion
ten = parseInt("10")
tenpointoh = parseFloat("10.0")
# try/catch blocks
this.print "// try/catch blocks"
doublestr = "x20.2"
try
twentypointtwo = parseFloat(doublestr)
if twentypointtwo == NaN
throw "'" + twentypointtwo + "' is not a float"
catch error
this.print "err: '" + doublestr + "' is not a double"
finally
doublestr = doublestr[1..]
twentypointtwo = parseFloat(doublestr)
##
# ArrayLists
this.print "// ArrayLists"
intlist = [0..4]
this.print intlist.length
intlist.pop(0)
this.print intlist.length
# traditional iteration # TODO
for nextint in intlist
this.print nextint
# iteration with downcasting
for nextint in intlist
this.print nextint
# javascript string join
this.print intlist.join("\n")
##
# HashMaps
this.print "// Maps"
map = {}
map['one'] = 1
map['two'] = 2
map['three'] = 3
map['four'] = 4
this.print _.keys(map).length
key = "one"
if key in map
this.print key + ": " + map[key]
searchvalue = "four"
if _.indexOf(_.values(map), searchvalue) != -1
this.print "contains the value '" + searchvalue + "'"
else
this.print "does not contain the value '" + searchvalue + "'"
for key in _.keys(map)
this.print key + "=" + map[key]
## Collections, sorting
this.print "// Collections.sort"
for k in _.keys(map).sort()
this.print k + ": " + map[k]
##
# OrderedDict
this.print "OrderedDict"
keyorder = []
pushKey = (obj,k,v) ->
keyorder.push k
obj[k] = v
sortedmap = {}
pushKey(sortedmap, "one", 1)
pushKey(sortedmap, "two", 2)
pushKey(sortedmap, "three", 3)
pushKey(sortedmap, "four", 4)
for key in keyorder
this.print key + "=" + sortedmap[key]
##
# invert dict
pushKey(sortedmap, "uno", 1)
invertedmap = {}
for k in keyorder
v = sortedmap[k]
_currentvalue = invertedmap[v]
if _currentvalue != undefined
if _currentvalue != k
this.print "duplicate key: '" + v + "' [" + k + "] {{" +
_currentvalue + "}}"
else
invertedmap[v] = k
for key in _.keys(invertedmap)
this.print key + "=" + invertedmap[key]
HelloWorld.main()