From a479b7ad59d3a894e40e4869627fb9821c23ef76 Mon Sep 17 00:00:00 2001 From: NthPortal Date: Tue, 26 Jun 2018 00:33:01 -0400 Subject: [PATCH 1/2] Add gitignore --- .gitignore | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..6bf5f1af --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +classes +target +.idea +.idea_modules +*.icode +project/local.sbt \ No newline at end of file From 7f01ed6c9261fcde6c2e8a2f5770ceb9a6d8b571 Mon Sep 17 00:00:00 2001 From: NthPortal Date: Tue, 26 Jun 2018 00:33:22 -0400 Subject: [PATCH 2/2] Add SeqDecorator.replaced --- README.md | 1 + .../scala/collection/decorators/SeqDecorator.scala | 12 ++++++++++++ .../collection/decorators/SeqDecoratorTest.scala | 6 ++++++ 3 files changed, 19 insertions(+) diff --git a/README.md b/README.md index 978cdce8..4f640811 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ The following operations are provided: - `Seq` - `intersperse` + - `replaced` - `Map` - `zipByKey` / `join` / `zipByKeyWith` - `mergeByKey` / `fullOuterJoin` / `mergeByKeyWith` / `leftOuterJoin` / `rightOuterJoin` diff --git a/src/main/scala/scala/collection/decorators/SeqDecorator.scala b/src/main/scala/scala/collection/decorators/SeqDecorator.scala index d9e4aa33..4dce7a22 100644 --- a/src/main/scala/scala/collection/decorators/SeqDecorator.scala +++ b/src/main/scala/scala/collection/decorators/SeqDecorator.scala @@ -41,4 +41,16 @@ class SeqDecorator[C, S <: HasSeqOps[C]](coll: C)(implicit val seq: S) { def intersperse[B >: seq.A, That](start: B, sep: B, end: B)(implicit bf: BuildFrom[C, B, That]): That = bf.fromSpecificIterable(coll)(new View.IntersperseSurround(seq(coll), start, sep, end)) + /** Produces a new sequence where all occurrences of some element are replaced by + * a different element. + * + * @param elem the element to replace + * @param replacement the replacement element + * @tparam B the element type of the returned $coll. + * @return a new sequence consisting of all elements of this sequence + * except that all occurrences of `elem` are replaced by + * `replacement` + */ + def replaced[B >: seq.A, That](elem: B, replacement: B)(implicit bf: BuildFrom[C, B, That]): That = + bf.fromSpecificIterable(coll)(new collection.View.Map(seq(coll), (a: seq.A) => if (a == elem) replacement else a)) } diff --git a/src/test/scala/scala/collection/decorators/SeqDecoratorTest.scala b/src/test/scala/scala/collection/decorators/SeqDecoratorTest.scala index 26acb32e..de7f1d2c 100644 --- a/src/test/scala/scala/collection/decorators/SeqDecoratorTest.scala +++ b/src/test/scala/scala/collection/decorators/SeqDecoratorTest.scala @@ -39,4 +39,10 @@ class SeqDecoratorTest { def typed[T](t: => T): Unit = () + @Test def testReplaced(): Unit = { + val s = Seq(1, 2, 3, 2, 1) + assertEquals(s.replaced(2, 4), Seq(1, 4, 3, 4, 1)) + assertEquals(s.replaced(3, 4), Seq(1, 2, 4, 2, 1)) + assertEquals(s.replaced(4, 4), s) + } }