{{ message }}

Instantly share code, notes, and snippets.

# ericnormand/00 Nested lists.md

Created Jan 11, 2021
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.

### green-coder commented Jan 11, 2021 • edited

``````(defn nest [elements n]
(map (partial repeat n) elements))
``````

There was no design process, the hardest part was to understand what the function was supposed to do, as it is out of context.

### steffan-westcott commented Jan 11, 2021

```(defn nest [xs n]
(map #(repeat n %) xs))```

### mchampine commented Jan 11, 2021

`(defn nest [l n] (for [e l] (repeat n e)))`

### souenzzo commented Jan 11, 2021 • edited

```(letfn [(nest [els n]
(for [el els]
(repeat n el)))]
{:a-a+b-b+c-c (nest [:a :b :c] 2)
:empty       (nest [] 10)
:1+2+3+4     (nest [1 2 3 4] 1)
:3.empty     (nest [1 2 3] 0)})     ```

`BONUS`

`let nest = ([... els], n) => els.map(el => Array(n).fill(el))`

### steffan-westcott commented Jan 11, 2021 • edited

As a point of comparison, here is my answer in Java. I did not enjoy wrestling with the types:

```import java.util.Arrays;
import java.util.stream.*;

class Main {
public static Object[] nest(Object[] xs, int n) {
return Arrays.stream(xs).map(x -> Stream.generate(() -> x).limit(n).toArray()).toArray();
}
}```

### sztamas commented Jan 11, 2021 • edited

```(defn nest [xs n]
(map (partial repeat n) xs))```

### kschltz commented Jan 11, 2021

```(defn nest [es n]
(->> es (map #(repeat n %))))```

### proush42 commented Jan 11, 2021

```(defn nest [coll cnt]
(map #(repeat cnt %) coll))```

### NPException commented Jan 11, 2021 • edited

Looks like I ended up with the same thing like most people:

```(defn nest [col n]
(map (partial repeat n) col))```

Here's my solution to the slightly different original problem: https://edabit.com/challenge/5mANMR3X6gEfWephD

```(defn array-multiplier [col]
(map (partial repeat (count col)) col))```

### diavoletto76 commented Jan 11, 2021

```(defn nest [xs n]
(map (partial repeat n) xs))```

### chopmo commented Jan 11, 2021 • edited

```(defn nest [list num]
(map #(repeat num %) list))```

### zackteo commented Jan 12, 2021 • edited

Happy to be able to make my first submission for this :)

```(defn nest [l n]
(map #(repeat n %) l))```

### frndmg commented Jan 12, 2021

well, trying to bring functional to python too (ignore the imports 😉).

disclaimer: not as beautiful as the clojure version

```from itertools import repeat
from functools import reduce, partial

compose = lambda *F: reduce(lambda f, g: lambda x: f(g(x)), F)
nest = lambda xs, n: compose(list, partial(map, compose(list, partial(repeat, times=n))))(xs)```

### sztamas commented Jan 12, 2021

@frndmg I see what you're trying to do :-), but for python I would prefer the more idiomatic:

```def nest(xs, n):
return [[x] * n for x in xs]```

### ndonolli commented Jan 14, 2021

My clojure solution was essentially identical to many solutions posted here already. So for fun, I'll show an ES6 javascript solution:

`const nest = (xs, n) => xs.map(x => new Array(n).fill(x));`

### Toni-zgz commented Jan 17, 2021 • edited

My clojure solution. I know isn't the best but i think is clearer than other solutions for beginners as me.

```(ns nest)
(require '[clojure.test :as test])

(defn nest [vect num-rep]
(let [funcion (fn [elt] (repeat num-rep elt))]
(map funcion vect)))

(test/deftest nest-test
(test/is (= '((:a :a) (:b :b) (:c :c))  (nest [:a :b :c] 2)))
(test/is (= '() (nest [] 10)))
(test/is (= '((1) (2) (3) (4))  (nest [1 2 3 4] 1)))
(test/is (= '(()()()) (nest [1 2 3] 0))))

(test/run-tests 'nest)```

### cristiano-belloni commented Jan 19, 2021

Javascript oneliner for comparison:

`(a, n) => a.map(a => new Array(n).fill(a)) `

### steffan-westcott commented Jan 20, 2021

@Toni-zgz Welcome to Clojure! Since you asked for some suggestions:

• To write formatted Clojure code in Github comments, write ````clojure` as the first line and ````` as the last line. See the Github docs on syntax hightlighting
• `map` on an empty collection will evaluate to an empty sequence. You don't need to explicitly check for an empty input collection in your answer.

### Toni-zgz commented Jan 20, 2021

@steffan-westcott thanks for your suggestions

### prairie-guy commented Apr 7, 2021

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

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