Skip to content

Instantly share code, notes, and snippets.

@anabastos
Created July 21, 2017 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anabastos/ddb71fe68c5b2f2437a22bc252b20153 to your computer and use it in GitHub Desktop.
Save anabastos/ddb71fe68c5b2f2437a22bc252b20153 to your computer and use it in GitHub Desktop.
super reduced string
// 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