forked from parmanoir/jscocoa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path45 ƒ function shortcut.js
75 lines (58 loc) · 1.36 KB
/
45 ƒ function shortcut.js
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
//
// Use ƒ (Option-f) as a shortcurt for function. Can omit name and arguments
//
// name + parameters
// function name(a, b) { ... }
ƒ nameAndParams(a, b)
{
return a+b
}
// name
// function name() { ... }
ƒ name
{
return 'hello'
}
// params
// function (a, b) { ... }
var f2 = ƒ (a, b)
{
return a*b
}
// no name, no params
var f3 = ƒ
{
return 'world'
}
// function () { ... }
var f1 = ƒ{ return 'hello' }
function DualNumber(a, b)
{
this.a = a
this.b = b
}
DualNumber.prototype.toString = function ()
{
return '(' + this.a + ':' + this.b + ')'
}
var a = [new DualNumber(5, 2), new DualNumber(1, 3), new DualNumber(8, 1), new DualNumber(1, 1)]
// log('raw=' + a)
a.sort(ƒ{
var a = arguments[0]
var b = arguments[1]
if (a.a < b.a) return -1
if (a.a > b.a) return 1
if (a.b < b.b) return -1
if (a.b > b.b) return 1
return 0
})
// log('sorted=' + a)
if (nameAndParams(8, 5) != 13) throw 'ƒ shortcut failed (1)'
if (name() != 'hello') throw 'ƒ shortcut failed (2)'
if (f2(9, 6) != 54) throw 'ƒ shortcut failed (3)'
if (f3() != 'world') throw 'ƒ shortcut failed (4)'
var sorted = a[0].a == 1 && a[0].b == 1
&& a[1].a == 1 && a[1].b == 3
&& a[2].a == 5 && a[2].b == 2
&& a[3].a == 8 && a[3].b == 1
if (!sorted) throw 'ƒ shortcut failed (5)'