Building a compose function with reduce: const compose = (.fns) => (value) => fns.reduceRight((acc, fn) => fn(acc), value) Īnd now using it to compose splitByTilde and first functions. If not specified, splice () will only remove elements from the array. , itemN (optional) - The elements to add to the start index. deleteCount (optional) - The number of items to remove from start. So we can compose those functions to build our final getName function. The splice () method takes in: start - The index from where the array is changed. The algorithm is: split by the colon and then get the first element of the given list. Let's build a first function: const first = (list) => list To get the first element we can use the list operator. Example: splitByTilde("john smith~123 Street~Apt 4~New York~NY~12345") // Returns the value of the first element in an array that pass a test. Creates a new array with every element in an array that pass a test. Copies array elements within the array, to and from specified positions. So now we can use our specialized splitByTilde function. Returns the function that created the Array objects prototype. You cant use splice on a string because it works for arrays as it doesn’t work on strings because strings are immutable (edit after Sebastian Simons comment) You can use split method to convert a string into array of characters. We want to make this "john smith~123 Street~Apt 4~New York~NY~12345" into this const split = (separator) => (text) => text.split(separator) Although this is not a great way but this is just to give you idea how things works in javascript. So the first thing would be the split function. This string.split("~") gets things done.Īnother functional approach using curry and function composition.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |