Ramda - Forming a string based on array of word positions I saw this question and wondered how it'd be best to accomplish this using Ramda.

I'm new to the library so I thought it'd be an interesting learning exercise.

I have a working solution but it seems overly complicated and verbose. I'd be grateful for any suggestions on what other ways it would be possible to achieve this output using Ramda.

I'm aware that this doesn't need a library; as I say, I approached it as a learning exercise.

Here's what I have:



const { addIndex, always, fromPairs, join, map, sort, toPairs, unnest } = R;

const song = {
  "99": [0, 7],
  "bottles": [1, 8],
  "of": [2, 9],
  "beer": [3, 10],
  "on": [4],
  "the": [5],
  "wall": [6]
};

const toSentence = join(' ');

const parsed = toSentence(
    map(
      ([_, word]) =&gt; word,
      sort(
        ([a, _], [b, __]) =&gt; a &lt; b ? -1 : 1,
        unnest(
          map(
            ([word, indices]) =&gt; map(
              index =&gt; [index, word],
              indices,
            ),
            toPairs(song)
          )
        )
      )
    )
  )
  
console.dir(parsed)
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"&gt;&lt;/script&gt;
This is a great question.

I found no easy Ramda functional way to solve this, but you could just use a forEach approach. I find it easier to read.

const {forEachObjIndexed, forEach} = R;

const song = {
  "99": [0, 7],
  "bottles": [1, 8],
  "of": [2, 9],
  "beer": [3, 10],
  "on": [4],
  "the": [5],
  "wall": [6]
};

let words = [];

forEachObjIndexed((indexes, word) =&gt; forEach(idx =&gt; words[idx] = word, indexes), song);

console.log(words.join(' '));