|
| 1 | +/* |
| 2 | + * Copyright (c) 2015 Typelevel |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 | + * this software and associated documentation files (the "Software"), to deal in |
| 6 | + * the Software without restriction, including without limitation the rights to |
| 7 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 8 | + * the Software, and to permit persons to whom the Software is furnished to do so, |
| 9 | + * subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 16 | + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 17 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 18 | + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 19 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + */ |
| 21 | + |
| 22 | +package cats.tests |
| 23 | + |
| 24 | +import cats.syntax.all.* |
| 25 | +import cats.Functor |
| 26 | +import cats.FunctorFilter |
| 27 | + |
| 28 | +class FunctorFilterSuite extends CatsSuite { |
| 29 | + |
| 30 | + test("withFilter alias allows for-comprehensions with guards") { |
| 31 | + // Explicitly use FunctorFilter to provide the withFilter method |
| 32 | + // to prove that for-comprehension guards work on any FunctorFilter |
| 33 | + def filterEvens[F[_]: FunctorFilter, A](fa: F[A])(implicit |
| 34 | + ev: A =:= Int |
| 35 | + ): F[A] = { |
| 36 | + implicit val F: Functor[F] = FunctorFilter[F].functor |
| 37 | + for { |
| 38 | + a <- fa |
| 39 | + if ev(a) % 2 == 0 |
| 40 | + } yield a |
| 41 | + } |
| 42 | + |
| 43 | + val list = List(1, 2, 3, 4, 5) |
| 44 | + val evens = filterEvens(list) |
| 45 | + |
| 46 | + assertEquals(evens, List(2, 4)) |
| 47 | + } |
| 48 | + |
| 49 | + test("withFilter is lazy and does not evaluate until map is called") { |
| 50 | + var evaluationCount = 0 |
| 51 | + |
| 52 | + val list = List(1, 2, 3) |
| 53 | + |
| 54 | + def getWrapper[F[_]: FunctorFilter, A](fa: F[A])(f: A => Boolean) = |
| 55 | + fa.withFilter(f) |
| 56 | + |
| 57 | + // Create the lazy WithFilter wrapper. |
| 58 | + // If it were strict, it would iterate immediately. |
| 59 | + val lazyWrapper = getWrapper(list) { x => |
| 60 | + evaluationCount += 1 |
| 61 | + x > 1 |
| 62 | + } |
| 63 | + |
| 64 | + // It has not been mapped yet, so the evaluation count should be 0. |
| 65 | + assertEquals(evaluationCount, 0) |
| 66 | + |
| 67 | + // Now we map over it. This forces the evaluation. |
| 68 | + val result = lazyWrapper.map(_ * 2) |
| 69 | + |
| 70 | + // The list has 3 elements, so the predicate should be called 3 times. |
| 71 | + assertEquals(evaluationCount, 3) |
| 72 | + assertEquals(result, List(4, 6)) |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments