Created
July 21, 2017 14:56
-
-
Save anabastos/ddb71fe68c5b2f2437a22bc252b20153 to your computer and use it in GitHub Desktop.
super reduced string
This file contains hidden or 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
// versao one line | |
const str = 'aaabccddd' | |
str.split('').reduce((acc, res) => res == acc.slice(-1)[0] ? acc.slice(0, -1) : acc.concat(res), []).join('') || 'Empty String' //'abd' | |
//versao composed | |
const lastItem = arr => arr.slice(-1)[0] | |
const removeLast = arr => arr.slice(0, -1) | |
const reduceArray = arr => { | |
return arr.reduce((acc, res) => res == lastItem(acc) ? removeLast(acc) : acc.concat(res), []) | |
} | |
const applyFunctionToStrAsArr = (str, func) => { | |
return func(str.split('')).join('') | |
} | |
const checkIfNotEmpty = str => str || 'Empty String' | |
const compose = str => checkIfNotEmpty(applyFunctionToStrAsArr(str, reduceArray)) | |
//compose(str) //'abd' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment