Skip to content

Instantly share code, notes, and snippets.

@lukkaslt
Last active August 11, 2019 02:23
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 lukkaslt/608519018b3c29d1ce9a3ef349e63741 to your computer and use it in GitHub Desktop.
Save lukkaslt/608519018b3c29d1ce9a3ef349e63741 to your computer and use it in GitHub Desktop.
(ecoC xs) es la lista obtenida a partir de la listaxsrepitiendo cada elemento tantasveces como indica su posición: el primer elemento se repite 1 vez, el segundo 2 veces y así sucesi-vamente
module Algos where
-- eco "abc" == "abbccc"
-- eco xs = concat [replicate i x | (i,x) <- zip [1..] xs]
-- eco xs = concat [replicate 1 "a" | (1,"a") <- zip [1..] "abc"]
-- eco xs = concat ["a" , replicate 2 "b" | (2,"b") <- zip [2..] "bc"]
-- eco xs = concat ["a" , "bb" replicate 3 "c" | (3,"c") <- zip [3..] "c"]
-- eco xs = concat ["a" , "bb", "ccc" | []]
-- eco xs = concat ["a" , "bb", "ccc"]
-- eco xs = "abbccc"
eco :: [a] -> [a]
eco xs = concat [replicate i x | (i,x) <- zip [1..] xs]
@lukkaslt
Copy link
Author

lukkaslt commented Aug 8, 2019

Solução com JavaScript

const repeat = (n, x, aux=[]) => {
  return n !== 0 && repeat(n-1, x, aux.concat(x)) || aux;
}

var range = function( x, y ) {
  return ( x < (y-1) ) && [ x+1, ...range( x+1, y ) ]
    ||   ( (y+1) < x ) && [ x-1, ...range( x-1, y ) ] 
    ||   [];
};

const zip = function(ar1, ar2, zipper) {
  return zipper 
    && ar1.map((value, index) => zipper(value, ar2[index]))
    || ar1.map((value, index) => [value, ar2[index]])
  ;
}

const eco = (xs) => {
  return (function aux([i, ...xs]) {
    return i !== void 0 && [...repeat(...i), ...aux(xs)] || []
  }(zip(range(0, xs.length+1), xs)))

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment