-
Notifications
You must be signed in to change notification settings - Fork 0
/
presentation.js
806 lines (578 loc) · 20.1 KB
/
presentation.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
// Function Declaration
function add (x, y) {
return x + y;
}
// Function Expression
var multiply = function (x, y) {
return x * y
};
// Function Constructor - Avoid this like the plague
var increment = new Function('n', 'return n + 1');
function sayMyName(name) {
return 'My name is ' + name;
}
sayMyName("Heisenberg");
var sayMyName = function _sayer(name) {
return 'My name is ' + name;
};
sayMyName('Heisenberg');
var sayMyName = new Function('name', "return 'Say my name ' + name");
sayMyName('Heisenberg');
var favoriteCharacter = { // A random object
name: 'Linus Van Pelt',
gender: 'Male',
inventory: ['Security Blanket']
};
// Getter
favoriteCharacter.name; // Using the dot notation
favoriteCharacter['gender']; // Using the brackent notation
// Setter
favoriteCharacter.sister = 'Lucy Van Pelt'; // Ditto here
favoriteCharacter['friend'] = 'Charlie Brown';
// Mention for =delete= who might be forgotten
delete favoriteCharacter.inventory;
var getter = function _doGet(emptyValue, key, object) {
if (object && object.hasOwnProperty(key)) {
return object[key];
} else {
return emptyValue;
}
};
var setter = function (key, value, object) {
if (object) {
object[key] = value;
}
return value;
};
var hero = {
name: 'Ogre Magi',
type: 'Intelligence'
};
getter('Anonymous', 'name', hero); // ::'Ogre Magi'
getter(0, 'baseHp', hero); // :: 0
setter('baseHp', 550, hero); // :: hero
1 + 2; // :: 3
2 * 3; // :: 6
var add = function _addition(x, y) {
return x + y;
};
var multiply = function _multiplication(x, y) {
return x * y;
};
add(1, 2); // :: 3
multiply(2, 3); // :: 6
// NOTE: JavaScript types really suck, so just roll with this
1 === 0; // :: false
'True' !== 'False'; // :: true
1 <= 2; // :: true
1 >= 2; // :: false
!'Nullify'; // :: false
!!''; // :: false
var equals = function _equals(x, y) { return x === y; },
notEquals = function _notEquals(x, y) { return x !== y;};
var not = function _not(x) { return !x; };
var lessThan = function _lessThan(x, y) { return x < y; },
moreThan = function _moreThan(x, y) { return x > y;};
var lessThanOrEquals = function _lessThan(x, y) { return x <= y; },
moreThanOrEquals = function _moreThan(x, y) { return x >= y; };
equals(true, false);
not(1);
not(not(1));
lessThanOrEquals(1, 2);
var getFullName = function _getFullName(firstName, lastName) {
return lastName + ', ' + firstName;
};
var firstName = 'Bruce',
lastName = 'Wayne';
// The default invokation, no frills here
getFullName(firstName, lastName);
// Invoking a function using the =call= method
getFullName.call(null, firstName, lastName);
// Invoking a function with array of arguments
getFullName.apply(null, [firstName, lastName]);
var sum = function _addTriple(x, y, z) { return x + y + z; };
// NOTE: Do notice the first parameter is null and the rest are the real arguments
sum.call(null, 1, 2, 3); // :: 6
sum.call(null, 4, 3, 0); // :: 7
var double = function _double(n) { return n * 2; };
double.call(null, 1); // :: 2
double.call(null, 2); // :: 4
var sum = function _addTriple(x, y, z) { return x + y + z; };
// NOTE: Again the first parameter is null, we'll get to that
sum.apply(null, [1, 2, 3]); // :: 6
sum.apply(null, [4, 3, 0]); // :: 7
var double = function _double(n) { return n * 2; };
double.apply(null, [1]); // :: 2
double.apply(null, [2]); // :: 4
var Animal = {
makeSound: function _makeSound() {
var self = this; // Notice is *this* is not defined anywhere
if (!this.type) { // If the animal has no type, throw an error
throw "Animal must have a type";
} else if (this.type === 'Cat') {
return 'Meow';
} else if (this.type === 'Duck') {
return 'Quack';
}
}
};
// Animal.makeSound(); // :: error "Animal must have a type";
Animal.type = 'Duck';
Animal.makeSound(); // :: 'Quack'
Animal.makeSound.call({ type: 'Cat'}); // :: Meow
var myFunction = function (/* args */) { // Nice convention when using arguments variable
var args = arguments; // Make it explicit rather than implicit
return args; // Just return what was passed
};
myFunction(0, false, 'Derp'); // :: [0, false, 'Derp']
myFunction(true); // :: [true]
myFunction() // :: []
var myOtherFunction = function (/* args */) { // I like this convention
var argObject = arguments;
// Now we have to do this when we want it as a list
var argList = [].slice.call(arguments);
return argList;
};
// Compare with the output of myFunction
myFunction(0, false, 'Derp');
myOtherFunction(0, false, 'Derp');
// Put a comment in the parameter section to indicate it's usage
var theArgumentFunction = function (/* argumentsName */) {
// First line is the variable name of =arguments=, as named above in the parameters
var argumentsName = arguments;
// var argumentsName = [].slice.call(arguments); // If you are going to do something with it.
// Code goes here
};
var isNumber = function (rawValue) {
if (typeof rawValue !== 'number') { // Thorw a type error like in Java
throw (typeof rawValue) + "<" + rawValue + ">" + " is not a number"
}
};
var typeCheckAllArgs = function (typeChecker, f) {
return function _argParser(/* args */) {
var args = [].slice.call(arguments);
args.forEach(typeChecker); // For loop through each argument and check the type
return f.apply(this, args);
};
};
var add = function (x, y) { return x + y; },
typeCheckedAdd = typeCheckAllArgs(isNumber, add);
add("5", "2"); // :: '52'
// typeCheckedAdd("5", "2"); // :: error "5 is not a integer"
typeCheckedAdd(5, 2); // :: 7
var sum = function _varArgSum(/* numbers */) {
var numbers = [].slice.call(arguments);
// I chose this implementation for it's brevity, for looping also works
var add = function (x, y) { return x + y; };
return numbers.reduce(add, 0);
};
sum(1, 2, 3); // :: 6
sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // :: 55
var firstClassFunction = function _() { return 'Something'; };
var functionVariable = firstClassFunction; // Variable functions
// A function that returns a function
var incrementer = function _startAt(start) {
var counter = start || 0;
return function _increment() { // Returns a function
counter = counter + 1;
return counter;
};
};
var countFromThree = incrementer(3);
// Goes up for each invokation, you can make a infinite sequence with this
countFromThree(); // :: 4
countFromThree(); // :: 5
countFromThree(); // :: 6
var save = function (entity) {
// Do some ajax call, local storate or what have you
/* Some hypothetical newly saved object */
return {
id: 42,
/* ... */
};
};
var save = function (entity) {
console.log('Save was called');
// Do some ajax call, local storate or what have you
/* Some hypothetical newly saved object */
return {
id: 42,
/* ... */
};
};
var logIt = function _decorator(message, loggedFunction) {
// Here we return a wrapped function
return function _decoratedFunction(/* args */) {
var args = arguments;
console.log(message || 'Function was called');
return loggedFunction.apply(this, args);
};
};
var save = function (entity) { // Function from above
return {
};
};
var loggedSave = logIt('Save was called', save);
loggedSave({
name: 'Snoopy'
});
var onClick = function _buttonHandler(event) {
// Do something with the event, use your imagination
return 'Button was clicked';
};
var isThrottled = false,
throttlePeriod = 500;
var onClick = function _buttonHandler(event) {
if (!isThrottled) {
isThrottled = true;
setTimeout(function _releaseThrottling() {
isThrottled = false;
}, throttlePeriod);
// Do something with the event, use your imagination
return 'Button was clicked';
} else {
return; // Do nothing when it is throttled
}
};
var throttle = function _throttler (delay, throttled) {
var isThrottled = false,
throttlePeriod = delay;
return function _throttledFunction(/* args */) {
var args = arguments;
if (!isThrottled) {
isThrottled = true;
setTimeout(function _releaseThrottling() {
isThrottled = false;
}, throttlePeriod);
return throttled.apply(this, args);
} else {
return; // Do nothing when it is throttled
}
};
};
// Original
onClick(); // :: 'Button was clicked'
onClick(); // :: 'Button was clicked'
// Let's increase the throttling period for effect
var throttledOnClick = throttle(2500, onClick);
// Throttled - Cue demo
throttledOnClick(); // :: 'Button was clicked'
throttledOnClick(); // :: undefined
var gotoHomePage = function (/* args */) {
return 'Went to the home page'; // Just imagine something here
};
var isAuthenticated = function (/* args */) {
return true; // Well
};
var gotoHomePage = function (/* args */) {
var args = arguments;
var allowExecution = isAuthenticated.apply(this, args);
if (allowExecution === true) {
return 'Went to the home page';
} else {
return; // Again do nothing
}
};
// Again, simple refactoring
var doIf = function _callIf(condition, executor) {
return function _conditionalCall(/* args */) {
var args = arguments;
var allowExecution = condition.apply(this, args);
if (allowExecution === true) {
return executor.apply(this, args);
} else {
return;
}
};
};
var authenticatedGotoPage = doIf(isAuthenticated, gotoHomePage);
// Previous declaration
var authenticatedGotoPage = doIf(isAuthenticated, gotoHomePage);
// Might be asynchronous but bear with me
var cachedStatus = false; // Say there is a ping every second and saved in a variable
var isServerUp = function () {
return cachedStatus === true;
};
// Awesome reuse
var authenticatedAndActiveGotoPage = doIf(isServerUp, authenticatedGotoPage);
// Let's see if we can copy this
var operation = function (n) {
return 2 * n + 1;
};
operation(4); // :: 9
var increment = function (n) { return n + 1; },
multiplyByTwo = function (n) { return n * 2; };
// Chaining the functions together
increment(multiplyByTwo(5)); // :: 9
// Just wrapping methods into functions
var toText = function (value) { return value.toString(); },
toUpperCase = function (text) { return text.toUpperCase() },
length = function (text) { return text.length; };
var theNumber = 42;
// Let's chain them
// toText -> toUpperCase -> length
// Paren over paren over paren, yuck
length(toUpperCase(toText(theNumber))); // :: 2
// Why not just?
theNumber
.toString()
.toUpperCase()
.length;
// Pay attention to the name
var removeEmptyFields = function (entity) { return entity; },
upperCaseTextFields = function (entity) { return entity; },
serializeToJson = function (entity) { return JSON.stringify(entity); };
// Pretend form value
var formValues = {
name: 'SUSHI',
gender: 'MALE',
cuteness: 0.80
};
// Chaining order
// removeEmptyFields -> upperCaseTextFields -> serializeToJson
serializeToJson(upperCaseTextFields(removeEmptyFields(formValues)));
// Rather what we need is not a chain but a behavior
var processFormValues = function (formEntity) {
return serializeToJson(upperCaseTextFields(removeEmptyFields(formEntity)));
};
// This is what we are actually looking for
processFormValues(formValues);
// Again, imagine it is some big function or whatnot
var f = function (/* args */) { return "inner"; },
g = function (/* args */) { return "outer"; };
// How do we clean this up?
g(f(1, 2, 3)); // Some value
// We can try this
var fAndg = function (/* args */) {
var args = [].slice.call(arguments);
var fValue = f.apply(this, args);
var gValue = g.call(this, fValue);
return gValue;
};
// Looks cleaner
fAndg(1, 2, 3); // Same value
var compose = function _composer(outer, inner) {
return function _composedFunction (/* args */) {
var args = [].slice.call(arguments);
var innerValue = inner.apply(this, args);
var outerValue = outer.call(this, innerValue);
return outerValue;
};
};
// Start with two
var increment = function (n) { return n + 1; },
triple = function (n) { return n * 3; };
// Remember read from the end to the beginning
var incrementThenTriple = compose(triple, increment),
incrementTwice = compose(increment, increment),
multipleByNine = compose(triple, triple);
// Too easy? let's use lists
var first = function (xs) { return xs[0]; },
rest = function (xs) { return xs.slice(1); },
reverse = function (xs) { return xs.reverse(); };
var second = compose(first, rest),
last = compose(first, reverse),
dropTwo = compose(rest, rest),
same = compose(reverse, reverse);
var pipe = function (inner, outer) { // Notice the reversed naming
return compose(outer, inner);
};
var prependNamespace = function (symbolName) { return 'fn/' + symbolName; },
appendParens = function (symbolName) { return prependNamespace + '()'; };
var mySymbol = 'mySymbol';
appendParens(prependNamespace(mySymbol)); // :: fn/mySymbol()
var processSymbolWithPipe = pipe(prependNamespace, appendParens), // Now left to right
processSymbolWithCompose = compose(appendParens, prependNamespace); // Default right to left
// Same answers as above
processSymbolWithPipe(mySymbol);
processSymbolWithCompose(mySymbol);
var composes = function _multiComposer(/* fns */) {
var fns = [].slice.call(arguments);
if (fns.length === 0) {
throw "No function to compose"
} else if (fns.length === 1) {
return fns[0]; // Same function
} else {
var reversedFns = fns.reverse(),
otherFns = reversedFns.slice(1),
firstFn = reversedFns[0];
return otherFns.reduce(pipe, firstFn);
}
};
var pipes = function _reversedCompose(/* fns */) {
var fns = [].slice.call(arguments);
var reversedFns = fns.reverse();
return composes.apply(this, reversedFns);
};
var myWords = "The quick brown fox jumps over the head of the lazy dog.";
// Let's count the number of words
myWords
.split(' ')
.length;
// Then let's compare this with the functional style
var splitByWord = function (text) { return text.split(' '); },
length = function (xs) { return xs.length; };
var countWords = composes(length, splitByWord);
// So how does this compare?
countWords(myWords);
var mapIf = function (predicate, mapper) {
return function (value) { // This time it's is a single argument
var ifValue = predicate.call(this, value);
return ifValue === true ? mapper.call(this, value) : value;
};
};
var tooLongName = function (username) { return username.length > 10; },
toLowerCase = function (text) { return text.toLowerCase(); },
escapeHtml = function (text) { return text; }, // The regexp might be too long, imagine
truncateName = function (username) { return username.slice(0, 10); };
var myUsername = "CharlieBrowniest";
var displayName = composes( // A nice convention when composing functions
escapeHtml,
mapIf(tooLongName, truncateName),
toLowerCase
);
displayName(myUsername); // :: charliebro
// Redefined for text
var concat = function (x, y) {return x + y; };
// Notice the redundancy here
var prefixMr = function (name) { return concat('Mr.', name); },
prefixMrs = function (name) { return concat('Mrs.', name); };
prefixMr('Apevosfe');
prefixMrs('Senapvf')
var presetFirstArg = function _presetFirstArgs(f, firstArg) {
return function _waitingForSecondArg(secondArg) {
return f.call(this, firstArg, secondArg);
};
};
var concat = function (x, y) { return x + y; };
var prefixMr = presetFirstArg(concat, 'Mr'),
prefixMrs = presetFirstArg(concat, 'Mrs.');
// Looks like a pyramid
var presetTwoArgs = function _presetFirstArg(f, firstArg) {
return function _waitingForSecondArg(secondArg) {
return function _waitingForThirdArg(thirdArg) {
return f.call(this, firstArg, secondArg, thirdArg);
};
};
};
var fullName = function (firstName, middleName, lastName) {
return lastName + ', ' + firstName + ' ' + middleName;
};
// Preset the arguments
var johnWho = presetTwoArgs(fullName, 'John');
var johnMichaelWho = johnWho('Michael');
johnMichaelWho('Vincent'); // :: "John Michael Vincent"
johnMichaelWho('Allan'); // :: "John Michael Allan"
var curry = function (f) {
var self = this,
argumentLength = f.length;
// Using a helper function
var currier = function _recursiveCurrier(oldArgs) {
return function _recursiveCurry(/* newArgs */) {
var newArgs = [].slice.call(arguments),
curArgs = oldArgs.concat(newArgs);
if (curArgs.length < argumentLength) { // Check if there is enough parameters
return currier(curArgs); // Keep returning a function until then
} else {
return f.apply(self, curArgs);; // Execute when there is enough
}
};
};
return currier([]);
};
var getId = curry(getter)(0, 'id'),
getDescription = curry(getter)('', 'description'),
setEmptyDescription = curry(setter)('description', 'N/A');
var ps4 = { id: 100, name: 'PlayStation Portable'},
ds = {name: 'Nintendo DS', description: 'Infidel Handheld'};
getId(ps4); // :: 100
getId(ds); // :: 0
getDescription(ps4); // :: ''
getDescription(ds); // :: 'Infidel Handheld'
setEmptyDescription(ps4);
getDescription(ps4); // :: 'N/A'
var isZero = curry(equals)(0),
isNull = curry(equals)(null);
isZero(10); // :: false
isZero(0); // :: true
isNull(10); // :: false
var isNotZero = composes(not, isZero);
isNotZero(10); // :: true
isNotZero(0); // :: false
var people = [
{name: 'Alice', gender: 'Female', source: 'Dilbert', salary: 100, starred: true},
{name: 'Dilbert', gender: 'Male', source: 'Dilbert', salary: 120 },
{name: 'Bruce Wayne', gender: 'Male', source: 'DC', starred: true},
{name: 'Selina Kyle', gender: 'Female', source: 'DC'},
{name: 'Master Chief', gender: 'Male', source:'Video Game'},
{name: 'Doom Guy', gender: 'Male', source: 'Video Game', starred: true},
{name: 'Samus', gender: 'Female', source: 'Video Game'}
];
var getName = curry(getter)('', 'name'),
getSalary =curry(getter)(0, 'salary');
people.map(getName); // :: ["Alice", "Dilbert", "Bruce Wayne", ...
people.map(getSalary) // :: [100, 120, 0, ... ]
// What if we want an total, no need to understand
people
.map(getSalary)
.reduce(add, 0); // 240
var isFemale = curry(equals)('Female'),
isMale = curry(equals)('Male'),
getGender = curry(getter)('', 'gender');
// Notice we composed
var isFemalePerson = compose(isFemale, getGender),
isMalePerson = compose(isMale, getGender);
var males = people.filter(isMale),
females = people.filter(isFemale);
// Other convention
var isStarred = compose(
curry(equals)(true),
curry(getter)(false, 'starred')
);
var favoritePeople = people.filter(isStarred);
var deepObject = {
a: {
b: {
c: 1
},
d: 2
},
e: 3
};
var safeGet = curry(getter)(null);
var getC = composes(
safeGet('c'),
safeGet('b'),
safeGet('a')
);
getC(deepObject); // :: 1
deepObject.a.b.c;
var getIncorrectC = composes(
safeGet('c'),
safeGet('bx'),
safeGet('b'),
safeGet('a')
);
getIncorrectC(deepObject); // :: null
// deepObject.a.b.bx.c // :: undefined type error
var currentDate = new Date(), // Obtained somewhere
addLastModifiedDate = setter('lastModifiedDate', currentDate);
var csrfToken = '1234abcd', // Somewhere in the DOM maybe?
addCsrfToken = setter('csrfToken', currentDate);
var removeEmptyFields = function (entity) {
for (var key in entity) {
if (entity.hasOwnProperty(key)) {
if (!entity[key]) {
delete entity[key];
}
}
}
return entity;
};
var serializeToJson = function (entity) { return JSON.stringify(entity); };
var processEntity = composes( // Now turn this into a method chain
serializeToJson,
addCsrfToken,
removeEmptyFields,
addLastModifiedDate
);