Skip to content

Instantly share code, notes, and snippets.

@ericnormand
Created January 11, 2021 15:12
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 ericnormand/965c5004944a62296c481aa9ac3fff5a to your computer and use it in GitHub Desktop.
Save ericnormand/965c5004944a62296c481aa9ac3fff5a to your computer and use it in GitHub Desktop.
410 - PurelyFunctional.tv Newsletter

Nested lists

Write a function that nests the elements of a list one level deeper by repeating that element inside a new list a given number of times.

Examples

(nest [:a :b :c] 2) ;=> ((:a :a) (:b :b) (:c :c))
(nest [] 10) ;=> ()
(nest [1 2 3 4] 1) ;=> ((1) (2) (3) (4))
(nest [1 2 3] 0) ;=> (()()())

Thanks to this site for the challenge idea where it is considered Hard in Java.

Please submit your solutions as comments on this gist.

@prairie-guy
Copy link

(defn nest [ns k] (map (partial repeat k) ns))

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