Ramda: Does Ramda sort have sideffects?

Created on 30 Mar 2020  路  3Comments  路  Source: ramda/ramda

I used R.sort in a Vue project inside a computed propert, which does not allow for mutations.
The Eslinter threw an error for R.sort, even though it does not make mutations.

I Forwarded the question to the Vue team, and solved the problem by ignoring linter error with a single line comment.

Most helpful comment

... and in fact, sort is one of Ramda's simplest implementations, essentially doing

(comparator) => (arr) => arr .slice (0) .sort (comparator)

But note that Ramda does not prevent you from creating side-effects. That is, while Ramda will not mutate your data, it make no attempt to prevent you from doing so. For instance,

const xs = [{size: 12}, {size: 7}, {size: 4}, {size: 24}, {size: 10}, {size: 21}, {size: 6}]

const sortedXs = sort ((a, b) => {
  a.type = a.size < 10 ? 'small' : a.size > 20 ? 'large' : 'medium' // side-effect
  b.type = b.size < 10 ? 'small' : b.size > 20 ? 'large' : 'medium' // side-effect
  return a.size - b.size
}) (xs)

sortedXs //=>
//    {size: 4,  type: "small"},
//    {size: 6,  type: "small"}
//    {size: 7,  type: "small"},
//    {size: 10, type: "medium"},
//    {size: 12, type: "medium"},
//    {size: 21, type: "large"},
//    {size: 24, type: "large"},
// ]

// but your original data has also been changed

xs //=>
// [
//    {size: 12, type: "medium"},
//    {size: 7,  type: "small"},
//    {size: 4,  type: "small"},
//    {size: 24, type: "large"},
//    {size: 10, type: "medium"},
//    {size: 21, type: "large"},
//    {size: 6,  type: "small"}
// ]

All 3 comments

Hi @sugoidesune! Are you asking whether sort mutates the input Array? It does not. Ramda will never mutate your input.

... and in fact, sort is one of Ramda's simplest implementations, essentially doing

(comparator) => (arr) => arr .slice (0) .sort (comparator)

But note that Ramda does not prevent you from creating side-effects. That is, while Ramda will not mutate your data, it make no attempt to prevent you from doing so. For instance,

const xs = [{size: 12}, {size: 7}, {size: 4}, {size: 24}, {size: 10}, {size: 21}, {size: 6}]

const sortedXs = sort ((a, b) => {
  a.type = a.size < 10 ? 'small' : a.size > 20 ? 'large' : 'medium' // side-effect
  b.type = b.size < 10 ? 'small' : b.size > 20 ? 'large' : 'medium' // side-effect
  return a.size - b.size
}) (xs)

sortedXs //=>
//    {size: 4,  type: "small"},
//    {size: 6,  type: "small"}
//    {size: 7,  type: "small"},
//    {size: 10, type: "medium"},
//    {size: 12, type: "medium"},
//    {size: 21, type: "large"},
//    {size: 24, type: "large"},
// ]

// but your original data has also been changed

xs //=>
// [
//    {size: 12, type: "medium"},
//    {size: 7,  type: "small"},
//    {size: 4,  type: "small"},
//    {size: 24, type: "large"},
//    {size: 10, type: "medium"},
//    {size: 21, type: "large"},
//    {size: 6,  type: "small"}
// ]

I am very sorry, during writing of the question, i decided to look into the source code and found my answer. And did not intend to post the question, i suppose it save anyway.
My Issue was, with eslinter that throws a "mutation error", for R.sort.
It was in a Nuxt project using computed properties.

Thank you for still answering! Amazing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lupuszr picture lupuszr  路  3Comments

CrossEye picture CrossEye  路  3Comments

cjohansen picture cjohansen  路  4Comments

woss picture woss  路  3Comments

Luxcium picture Luxcium  路  3Comments