-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
603 lines (461 loc) · 13.7 KB
/
index.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
// Core set of functions that are available. If it's not here, it
// is not supported.
// NOTE: these functions are mostly reduction functions over a list
// which I feel is absolutely amusing. One of the many things
// that are amazing about a list.
// NOTE:
// could've used variadic functions too. But yeah, I just wanted
// to use the arguments object for fun and also that lisp-y wants
// run in a lot more environments...like one's before the ES6 and
// so yeah I thought this would help
// NOTE: These functions are modeled after the clojure functions
// NOTE: What I also realized recently was that, the list functions
// almost always return a JS Array, which sits perfectly well into
// our AST, because our AST is also one. I'm not sure if this is the
// right way to do things. Help me out here?
const utils = require("./utils");
const getArgs = utils.getArgs;
const throwError = utils.throwError;
const isZero = utils.isZero;
const sequenceIterator = utils.sequenceIterator;
// Utitlity for adding n numbers
function add() {
const [args, argsCount] = getArgs(arguments);
if (argsCount === 0) {
return 0;
}
return args.reduce((total, number) => total + number, 0);
}
// Utility for subtracting n numbers
// NOTE: The implementation is similar to the one in the clojure docs
function subtract() {
const [args, argsCount] = getArgs(arguments);
if (argsCount === 0) {
return 0;
}
if (args.length === 1) {
return -1 * args[0];
}
if (args.length === 2) {
const [x, y] = args;
return x - y;
}
const [startValue, ...toSubtractList] = args;
return toSubtractList.reduce((total, number) => subtract(total, number), startValue);
}
// utility for multiplication
function multiply() {
const [args, argsCount] = getArgs(arguments);
if (argsCount === 0) {
return 1;
}
return args.reduce((total, number) => total * number, 1);
}
// Utility for division
function divide() {
const [args, argsCount] = getArgs(arguments);
if (argsCount === 0) {
throwError({
func: "divide",
message: "Too few arguments"
});
}
if (args.length === 1) {
isZero(args[0]);
return 1/args[0];
}
if (args.length === 2) {
const [x, y] = args;
isZero(y);
return x / y;
}
// NOTE: divide(/) in clojure gives the fraction if the
// numbers are not perfectly divisible.
// TODO: Try to emulate clojure divide function, in the sense that
// I must display the fraction rep. by default
const [startNumber, ...restArgs] = args;
return restArgs.reduce((total, number) => {
isZero(number);
return divide(total, number);
}, startNumber);
}
// Increment a value
function inc() {
const [args, argsCount] = getArgs(arguments);
if (argsCount !== 1) {
throwError({
func: "inc",
message: "Too many arguments"
});
}
const num = args[0];
// TODO: Check for the type of the arg, using the data within
// the AST, do not use isNaN or any other method.
// Or could have used the add function, like so: return add(num, 1)
return num + 1;
}
// Decrement a value
function dec() {
const [args, argsCount] = getArgs(arguments);
if (argsCount !== 1) {
throwError({
func: "dec",
message: "Too many arguments"
});
}
const num = args[0];
return num - 1;
}
// NOTE: we might have to add support for the types but yes,
// this is a lisp within javascript-land. So yeah, let it be.
function list() {
const [args] = getArgs(arguments);
return args;
}
// get the first element from a collection
function first() {
const [args, argsCount] = getArgs(arguments);
// NOTE: It'll be good to check if it is an array/list as well
if (argsCount !== 1) {
// NOTE: I'm doing this because I think it would help
// for something like prop-types to exist for node
// projects and this object representation atleast
// would help in the documentation process. And this
// applies to all the functions that are applied to collections.
throwError({
func: "first",
message: "Too many arguments"
});
}
const coll = args[0];
const [first, ...rest] = coll;
// return the first element of the collection
return first;
}
// get the last element from a collection
function last() {
const [args, argsCount] = getArgs(arguments);
if (argsCount !== 1) {
throwError({
func: "last",
message: "Too many arguments"
});
}
const coll = args[0];
const collLength = args.length;
// return the first element of the collection
return coll[collLength - 1];
}
// sort the collection
function sort() {
const [args, argsCount] = getArgs(arguments);
if (argsCount !== 1) {
throwError({
func: "sort",
message: "Too many arguments"
});
}
const coll = args[0];
// return the first element of the collection
// TODO: try and implement the function my self, to
// help add performance gains.
// GOTCHA: sort function sorts the list lexicographically.
coll.sort((a, b) => a - b);
return coll;
}
// get the rest of the elements from the first
function rest() {
const [args, argsCount] = getArgs(arguments);
if (argsCount !== 1) {
throwError({
func: "first",
message: "Too many arguments"
});
}
const coll = args[0];
const [first, ...rest] = coll;
// return the first element of the collection
return rest;
}
// Add String functions here
// Concats all argument strings together
function str() {
const [args, argsCount] = getArgs(arguments);
if (!argsCount) {
return "";
}
return args.reduce((accString, currentString) => accString.concat(currentString), "");
}
// TODO: This function should work for all types
// Inserts a character in between the different words that are passed
// while concat-ing them in the process
function interpose() {
const [args, argsCount] = getArgs(arguments);
if (argsCount !== 2) {
throwError({
func: "interpose",
message: "Incorrect arguments passed"
});
}
// FIXME: "\"" The double quote can't be passed, unless it is escaped,
// or else it goes into an infinite loop
const [char, wordList] = args;
const [startWord, ...restWordList] = wordList;
const wordListLen = wordList.length;
return restWordList.reduce((accStr, currentWord, index) => {
if (index === (wordListLen - 1)) {
return accStr;
}
return accStr.concat(char, currentWord);
}, startWord);
}
// this is the list reverse & not the string reverse
function reverse() {
const [args, argsCount] = getArgs(arguments);
if (argsCount !== 1) {
throwError({
func: "reverse",
message: "Incorrect arguments passed"
});
}
const list = args[0];
return list.reverse();
}
// generates a list with elements within a range
function range() {
const [args, argsCount] = getArgs(arguments);
let start = 0, end, step = 1;
if (!argsCount) {
throwError({
func: "range",
message: "Too few Arguments"
});
}
if (argsCount === 1) {
end = args[0];
}
if (argsCount === 2) {
start = args[0];
end = args[1];
}
if (argsCount === 3) {
start = args[0];
end = args[1];
step = args[2];
}
if (argsCount > 3) {
throwError({
func: "range",
message: "Too many Arguments"
});
}
// get result from the iterator
let result = [];
let numberSeq = sequenceIterator(start, end, step);
for (let num of numberSeq) {
result.push(num);
}
return result;
}
// shuffles the elements of a list
// uses the Fisher-Yates Shuffle Algorithm to perform the shuffle
// This was taken right off of Mike Bostock's post : https://bost.ocks.org/mike/shuffle/
function shuffle() {
const [args, argsCount] = getArgs(arguments);
if (argsCount !== 1) {
throwError({
func: "shuffle",
message: "Incorrect number of arguments passed"
});
}
let listCopy = args[0];
// NOTE: The algorithm is an in-place shuffling algorithm
// I'm really sorry about the variable names if you don't use
// autocomplete. This sentence was composed mainly by autocomplete.
let marker = listCopy.length, temp, intermediate;
while(marker) {
intermediate = Math.floor(Math.random() * marker--);
temp = listCopy[marker];
listCopy[marker] = listCopy[intermediate];
listCopy[intermediate] = temp;
}
return listCopy;
}
// Calculate the sum of all elements in a given list
function sum() {
const[args, argsCount] = getArgs(arguments);
if (argsCount !== 1) {
throwError({
func: "sum",
message: "Incorrect arguments passed"
});
}
const list = args[0];
return add(...list);
}
// Calculate the product of all elements of the given list
function product() {
const [args, argsCount] = getArgs(arguments);
if (argsCount !== 1) {
throwError({
func: "product",
message: "Incorrect arguments passed"
});
}
const list = args[0];
return multiply(...list);
}
// The infamous map function, supply a predicate and list.
function map() {
const [args, argsCount] = getArgs(arguments);
if (argsCount !== 2) {
throwError({
func: "map",
message: "Incorrect arguments passed"
});
}
const [funcToApply, list] = args;
return list.map(elem => {
// might have to do some checks perhaps? this will
// come after additional information is supplied
// by the AST
return coreLibFunctions[funcToApply](elem);
});
}
// interleave does what zip does in Haskell
// `interleaves` multiple lists until one of them is exhausted
// NOTE: Pass only lists as inputs
function interleave() {
const [args, argsCount] = getArgs(arguments);
if(argsCount === 1) {
const list = args[0];
return list;
}
if(argsCount === 0) {
return [];
}
const lists = args;
const listsLengths = lists.map(xs => xs.length);
let numOfIterations = coreLibFunctions["sort"](listsLengths)[0];
let finalResult = [];
while(numOfIterations) {
lists.forEach(list => {
const currentElement = list.shift();
finalResult.push(currentElement);
});
numOfIterations--;
}
return finalResult;
}
// Returns the length of a list
function count() {
const [args, argsCount] = getArgs(arguments);
if (argsCount !== 1) {
throwError({
func: "count",
message: "Incorrect arguments passed"
});
}
const list = args[0];
return list.length;
}
// Checks whether a number is even
function isEven() {
const [args, argsCount] = getArgs(arguments);
if (argsCount !== 1) {
throwError({
func: "isEven",
message: "Incorrect arguments passed"
});
}
const number = args[0];
return number % 2 === 0;
}
// Checks if the number is odd
function isOdd() {
const [args, argsCount] = getArgs(arguments);
if (argsCount !== 1) {
throwError({
func: "isOdd",
message: "Incorrect arguments passed"
});
}
return !isEven(args);
}
// Checks if substring is present in the string
function includes() {
const [args, argsCount] = getArgs(arguments);
if (argsCount !== 2) {
throwError({
func: "includes",
message: "Incorrect arguments passed"
});
}
const [str, substr] = args;
// NOTE: Using indexOf over includes
return str.indexOf(substr) !== -1;
}
// Converts the first letter of the string to upper-case
// and the rest of the characters to lower-case
function capitalize() {
const[args, argsCount] = getArgs(arguments);
if (argsCount !== 1) {
throwError({
func: "capitalize",
message: "Incorrect arguments passed"
});
}
const str = args[0];
const strSplit = str.split("");
// NOTE: This process can be performed with simple
// joins and then applying toUpperCase and toLowerCase
// on those parts separately.
return strSplit.reduce((capWord, letter, index) => {
if (index === 0) {
return capWord.concat(letter.toUpperCase());
}
return capWord.concat(letter.toLowerCase());
}, "");
}
// the infamous `filter` reduction function
function filter() {
const [args, argsCount] = getArgs(arguments);
if(argsCount !== 2) {
throwError({
func: "filter",
message: "Incorrect arguments applied"
});
}
const [funcToApply, list] = args;
return list.filter(elem => coreLibFunctions[funcToApply](elem));
}
// The total list of functions supported in lisp-y
const coreLibFunctions = {
"list": list,
"add" : add,
"first": first,
"subtract": subtract,
"multiply": multiply,
"divide": divide,
"sort": sort,
"last": last,
"rest": rest,
"inc": inc,
"dec": dec,
"str": str,
"interpose": interpose,
"reverse": reverse,
"range": range,
"shuffle": shuffle,
"sum": sum,
"product": product,
"map": map,
"interleave": interleave,
"count": count,
"isEven": isEven,
"isOdd": isOdd,
"includes": includes,
"capitalize": capitalize,
"filter": filter
};
module.exports = coreLibFunctions;