scala-collection-compat icon indicating copy to clipboard operation
scala-collection-compat copied to clipboard

TraversableOnce rewritten as IterableOnceIterableOnce

Open OndrejSpanel opened this issue 4 years ago • 1 comments

A simple source with TravesableOnce results in a strange IterableOnceIterableOnce identifier (the correct identifier is repeated twice):

package com.github.ondrejspanel.scafi

object TraversableOnce {

  def shuffle[T](random: scala.util.Random, src: TraversableOnce[T]): List[T] = {
    src.foldLeft(List.empty[T]) { (list, item) =>
      val len = list.length
      val index = (random.nextDouble() * (len + 1)).toInt
      list.patch(index, List(item), 0)
    }
  }

}

Using scalafix Collection213Upgrade was rewritten as:

package com.github.ondrejspanel.scafi

import scala.IterableOnce
object TraversableOnce {

  def shuffle[T](random: scala.util.Random, src: IterableOnceIterableOnce[T]): List[T] = {
    src.foldLeft(List.empty[T]) { (list, item) =>
      val len = list.length
      val index = (random.nextDouble() * (len + 1)).toInt
      list.patch(index, List(item), 0)
    }
  }

}

Complete project available at https://github.com/OndrejSpanel/ScalaFixSimple

OndrejSpanel avatar Aug 10 '21 08:08 OndrejSpanel

Note that this is only a "good first issue" for those with an interest in working on the Scalafix rules, as opposed to the main part of the library.

SethTisue avatar Nov 03 '22 13:11 SethTisue