-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathex08.js
83 lines (45 loc) · 2.67 KB
/
ex08.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
/*
Partial application allows you to create new functions from existing functions, while fixing some number of arguments. After setting the arguments to be partially applied, you get a new function ready to take the rest of the arguments and perhaps execute the original function.
More formally: Partial application refers to the process of fixing a number of arguments to a function, producing another function of smaller arity.
As an example, say we have a function add, that takes 2 arguments and adds them together:
function add(x, y) {
return x + y
}
add(10, 20) // => 30
Now, pretend we have a function partiallyApply. partiallyApply takes a function, and some arguments to 'partially apply'.
Here we 'partially apply' the first parameter, x, of our add function:
var addTen = partiallyApply(add, 10) // fix `x` to 10
addTen is a new function that takes the y parameter of add. add has not yet been called!
Once we pass the argument for y, we can execute the original add function:
addTen(20) // => 30
addTen(100) // => 110
addTen(0) // => 10
// etc
All of the above examples are same as calling add(10, y), where y was supplied in the call to the appropriately named addTen.
Task
Use partial application to create a function that fixes the first argument to console.log. i.e. Implement a logging function that prepends a namespace string to its output.
Your implementation should take a namespace String and return a function that prints messages to the console with the namespace prepended.
You should use Function#apply to implement the partial application.
Make sure all arguments passed to the returned logging function are printed.
Print the output to the console directly
## Example
var info = logger('INFO:')
info('this is an info message')
// INFO: this is an info message
var warn = logger('WARN:')
warn('this is a warning message', 'with more info')
// WARN: this is a warning message with more info
* Do not use Function#bind
* Use Function#apply
## Hints
Remember console.log takes any number of arguments and prints them, separated by spaces:
console.log('hello', 'world') // => 'hello world'
console.log(1,2,3) // => 1 2 3
We simply want to 'partially apply' the first argument to console.log.
Function.prototype.apply allows us to execute a function, supply a new 'value for this' (we can ignore in this case), and then an array of arguments to apply to the function:
add(10, 20) // => 30
add.apply(null, [10, 20]) // => 30
Also contrast apply with Function.prototype.call:
add.apply(null, [10, 20]) // => 30
add.call(null, 10, 20) // => 30
*/