Skip to content

Commit 93cc447

Browse files
author
mamouel
committed
ex src HOF, exceptions, __proto__, return, bind, this and anonymous function
1 parent 5ca423f commit 93cc447

File tree

16 files changed

+664
-104
lines changed

16 files changed

+664
-104
lines changed

HOF avec commentaires/problem1.js

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
function callNoException(f, arg) {
2+
try {
3+
f(arg);
4+
return f(arg);
5+
}
6+
catch(err) {
7+
return null;
8+
}
9+
}
10+
11+
// if f(arg) throws an exception, return null
12+
// otherwise return what f(arg) returned
13+
// Example:
14+
// function throwsZero(x){
15+
// if(x==0) throw new Error("woops");
16+
// return x;
17+
// }
18+
// callNoException(throwsZero, 0) returns null
19+
// callNoException(throwsZero, 12) returns 12
20+
21+
//
22+
// if f(arg) returns null, throw an exception
23+
// otherwise return what f(arg) returned
24+
// Example:
25+
// function nullZero(x) {
26+
// if(x==0) return null;
27+
// return x;
28+
// }
29+
// callNoNull(nullZero, 0) throws an exception
30+
// callNoNull(nullZero, 12) returns 12
31+
function callNoNull(f, arg) {
32+
var ret = f(arg);
33+
if(ret === null) throw new Error("oh no!");
34+
return ret
35+
}
36+
37+
38+
39+
/* si callNoNull retourne une fonction :
40+
function callNoNull(f, arg) {
41+
function g() {
42+
var ret = f(arg);
43+
if(ret === null) throw new Error("oh no!");
44+
return ret
45+
}
46+
return g;
47+
}
48+
*/
49+
50+
/*
51+
function callNoNull(f, arg) {
52+
if(f(arg) === null) throw "arg is null";
53+
return f(arg);
54+
}
55+
56+
function nullZero(x) {
57+
if(x==0) return null;
58+
return x;
59+
}
60+
61+
try {
62+
callNoNull(nullZero, 12)
63+
} catch(err) {
64+
console.log(err)
65+
}
66+
*/
67+
68+
//
69+
function exceptionalize(f) {
70+
return function(arg) {
71+
if (f(arg) == null) {throw 'arg is null';}
72+
else return f(arg);
73+
}
74+
}
75+
76+
/*function exceptionalize(f) {
77+
return f();
78+
}
79+
80+
function nullZero(x) {
81+
if(x==0) return null;
82+
return x;
83+
}
84+
85+
function g(arg) {
86+
if(nullZero(arg) === "null") throw "arg is null";
87+
return g(arg)
88+
}
89+
*/
90+
91+
// returns a new function
92+
// this function takes 1 input, called arg
93+
// if f(arg) is null, this new function throws an exception
94+
// otherwise it returns what f(arg) returned
95+
// difference with callNoNull: callNoNull doesn't return a function
96+
// Example:
97+
// function nullZero(x) {
98+
// if(x==0) return null;
99+
// return x;
100+
// }
101+
// exceptionalize(nullZero) returns a function g such that
102+
// g(0) throws an exception
103+
// g(12) returns 12
104+
105+
106+
//
107+
function nullify(f) {
108+
// returns a new function
109+
// this function takes 1 input, called arg
110+
// if f(arg) throws an exception, this new function returns null
111+
// otherwise it returns what f(arg) returned
112+
// Example
113+
// function throwsZero(x){
114+
// if(x==0) throw new Error("woops");
115+
// return x;
116+
// }
117+
// nullify(throwsZero) returns a function g such that
118+
// g(0) returns null
119+
// g(12) throws an exception
120+
function g(arg) {
121+
try {
122+
return f(arg);
123+
} catch (err) {
124+
return null;
125+
}
126+
}
127+
return g;
128+
}
129+
130+
131+
//
132+
function map(lst, f) {
133+
var ret = [];
134+
for (index in lst) {
135+
ret.push(f(lst[index]));
136+
}
137+
return ret;
138+
}
139+
140+
function toUpperCase(str) { return str.toUpperCase(); }
141+
map(["bob", "susie"], toUpperCase)
142+
143+
144+
//
145+
146+
function filter(lst, f) {
147+
var arr = [];
148+
for(var i = 0; i < lst.length; i++) {
149+
if(f(lst[i]) == true) {
150+
arr.push(lst[i]);
151+
}
152+
}
153+
return arr;
154+
}
155+
156+
// lst is an array and f is a function
157+
// f takes one argument and returns a boolean (true or false)
158+
// filter(lst, f) returns a list with all the elements of lst that does not satisfy f removed
159+
// filter(lst, f) has fewer elements than lst
160+
// if lst_ = filter(lst, f) and x is an element of lst_ it means that:
161+
// x is an element of lst
162+
// f(x) is true
163+
164+
// Example:
165+
// function isEven(x) {return x % 2 == 0;}
166+
// filter([1, 2, 3, 4, 5], isEven) returns [2,4];
167+
168+
function every(lst, f) {
169+
// lst is an array and f is a function
170+
// f takes 1 arguments and returns a boolean
171+
// filter(lst, f) returns a true if f returns true for every element of lst
172+
173+
// Example
174+
// every([2,4,12], x => x % 2 == 0) returns true
175+
// every([2,3,12], x => x % 2 == 0) returns false
176+
for(var i = 0; i < lst.length; i++) {
177+
if(f(lst[i]) === false) {
178+
return false;
179+
}
180+
181+
}
182+
return true;
183+
}
184+
185+
186+
module.exports = {
187+
callNoException,
188+
callNoNull,
189+
exceptionalize,
190+
nullify,
191+
map,
192+
filter,
193+
every
194+
};

0 commit comments

Comments
 (0)