Last active
October 8, 2020 10:45
-
-
Save parulagg27/e58b8a81a3e0205844d98c642388623d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//returns length of the given array | |
function length(arr){ | |
if (arr[0]==undefined) | |
return 0; | |
return lengthFinder(arr,0); | |
} | |
function lengthFinder(arr, counter){ | |
if (arr[counter+1]) | |
return lengthFinder(arr, counter+1); | |
return counter+1; | |
} | |
//========================================================== | |
//========================================================== | |
//returns first element of given array | |
function head(arr){ | |
if (arr.length==0) | |
return null; | |
return arr[0]; | |
} | |
//========================================================== | |
//========================================================== | |
// all the elements of given array except for first element | |
function tail(arr){ | |
if (arr.length==0 || arr.length==1) | |
return []; | |
return tailFinder(arr, 0, []); | |
} | |
function tailFinder(arr, start, retArr=[]){ | |
if (start==0) | |
return tailFinder(arr, start+1, retArr); | |
if (start == arr.length) | |
return retArr; | |
retArr[start-1] = arr[start]; | |
return tailFinder(arr, start+1, retArr); | |
} | |
//========================================================== | |
//========================================================== | |
//returns only the last element of given array | |
function last(arr){ | |
if (arr[0]==undefined) | |
return null; | |
return arr[arr.length-1]; | |
} | |
//=========================================================== | |
//=========================================================== | |
//returns reverse of the given array | |
function reverse(arr){ | |
if (arr[0]==undefined) return null; | |
return reverseFinder(arr, 0, []); | |
} | |
function reverseFinder(arr, index, revArr){ | |
if (arr.length==1) return arr; | |
if (index==arr.length) | |
return revArr; | |
revArr = reverseFinder(arr, index+1, revArr); | |
revArr[revArr.length] = arr[index]; | |
return revArr; | |
} | |
//============================================================ | |
//============================================================ | |
//returns all elements of given array except for first element | |
function init(arr){ | |
if (arr[0]==undefined) | |
return null; | |
return reverse(tail(reverse(arr))); | |
} | |
//============================================================ | |
//============================================================ | |
//applies the given function to each element of given array | |
function map(arr, func){ | |
return mapFinder(arr, func, start=0, []); | |
} | |
function mapFinder(arr, func, start=0, retArr){ | |
if (arr[start]==undefined) | |
return retArr; | |
retArr[start] = func(arr[start]); | |
return mapFinder(arr, func, start+1, retArr); | |
} | |
identity = n=>n; | |
square = n => n*n; | |
cube = n => n*n*n; | |
//================================================================== | |
//================================================================== | |
// filters only those elements of an array that satisfies the given condition | |
function filter(arr, condition){ | |
return filterFinder(arr, condition, 0, [], 0); | |
} | |
function filterFinder(arr, condition, start=0, retArr, retIndex){ | |
if (arr[start] ==undefined) | |
return retArr; | |
if (condition(arr[start])){ | |
retArr[retIndex] = arr[start]; | |
return filterFinder(arr, condition, start+1, retArr, retIndex+1); | |
} | |
return filterFinder(arr, condition, start+1, retArr, retIndex) | |
} | |
filterUpperCase = str => (/^A-Z/).test(str); | |
//================================================================== | |
//================================================================== | |
// reduces the given array to a single value as per given function | |
function reduce(arr, func, initialVal=0, index=0, retVal){ | |
if (arr[index]==undefined) | |
return retVal; | |
if (index==0 && initialVal!=0) | |
retVal = initialVal; | |
retVal = func(retVal, arr[index]); | |
return reduce(arr, func, initialVal, index+1, retVal); | |
} | |
//================================================================= | |
//================================================================= | |
//eturns max. value in the given array | |
function max(arr){ | |
if (arr.length==0) | |
return null; | |
return maxFinder(arr, 0, arr[0]); | |
} | |
function maxFinder(arr, index, maxVal){ | |
if (arr[index] > maxVal) | |
maxVal = arr[index]; | |
if (index == (arr.length-1)) | |
return maxVal; | |
return maxFinder(arr, index+1, maxVal); | |
} | |
//================================================================= | |
//================================================================= | |
function min(arr){ | |
if (arr.length==0) | |
return null; | |
return minFinder(arr, 0, arr[0]); | |
} | |
function minFinder(arr, index, minVal){ | |
if (arr[index] < minVal) | |
minVal = arr[index]; | |
if (index == arr.length-1) | |
return minVal; | |
return minFinder(arr, index+1, minVal); | |
} | |
//================================================================= | |
//================================================================= | |
//returns first 'n' elements of given list | |
function take(arr, n){ | |
if (arr[0]==undefined) | |
return []; | |
if (arr.length < n) | |
return "invalid length given"; | |
return takeFinder(arr, n, 0, []); | |
} | |
function takeFinder(arr, n, index, takeArr){ | |
takeArr[index] = arr[index]; | |
if (index == n-1) | |
return takeArr; | |
return takeFinder(arr, n, index+1, takeArr); | |
} | |
//================================================================ | |
//================================================================ | |
// deletes first 'n' elements of given list | |
function drop(arr, n){ | |
if (arr.length==0) return null; | |
if (arr.length <= number) return []; | |
return reverse(take(reverse(arr), (arr.length-n))); | |
} | |
//=============================================================== | |
//================================================================ | |
//checks if given element is present in array | |
function contains(arr, element){ | |
if (arr.length==0) return false; | |
return containValidator(arr, element, 0); | |
} | |
function containValidator(arr, element, index){ | |
if (index==arr.length && arr[index]!=element) | |
return false; | |
if (arr[index]==element) | |
return true; | |
return containValidator(arr, element, index+1); | |
} | |
//================================================================ | |
//================================================================ | |
function cycle(arr){ | |
return { | |
index:0, | |
next: function(){ | |
return arr[(this.index++)%arr.length]; | |
} | |
} | |
} | |
//================================================================= | |
//================================================================= | |
function range(start, end, jump=1){ | |
return rangeFinder(start, end, jump, 0, []); | |
} | |
function rangeFinder(startVal, endVal, jump, index, retArr){ | |
if (startVal>=endVal && jump>0) | |
return retArr; | |
if (startVal<=endVal && jump<0) | |
return retArr; | |
retArr[index] = startVal; | |
return rangeFinder(startVal+jump, endVal, jump, index+1, retArr); | |
} | |
//=================================================================== | |
//=================================================================== | |
function sum(arr){ | |
return reduce(arr, (x,y) => x+y); | |
} | |
//================================================================== | |
//================================================================== | |
function sumOfSqaures(arr){ | |
return sum(map(arr, square)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment