|
| 1 | +package com.baeldung.scala.currying |
| 2 | + |
| 3 | +import org.junit.Test |
| 4 | +import org.scalatest.Matchers |
| 5 | + |
| 6 | +class CurryingUnitTest extends Matchers { |
| 7 | + |
| 8 | + @Test |
| 9 | + def givenMultipleArgumentsFunction_whenCurried_thenReturnCurriedFunction() { |
| 10 | + val sum: (Int, Int) => Int = (x, y) => x + y |
| 11 | + val curriedSum: Int => Int => Int = sum.curried |
| 12 | + |
| 13 | + sum(1, 2) shouldBe 3 |
| 14 | + curriedSum(1)(2) shouldBe 3 |
| 15 | + } |
| 16 | + |
| 17 | + @Test |
| 18 | + def givenMultipleArgumentsMethod_whenCurried_thenReturnCurriedFunction() { |
| 19 | + def sum(x: Int, y: Int): Int = x + y |
| 20 | + |
| 21 | + val curriedSum: Int => Int => Int = (sum _).curried |
| 22 | + |
| 23 | + sum(1, 2) shouldBe 3 |
| 24 | + curriedSum(1)(2) shouldBe 3 |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + def givenMultipleArgumentListsMethod_whenCurried_thenReturnCurriedFunction() { |
| 29 | + def sum(x: Int)(y: Int): Int = x + y |
| 30 | + |
| 31 | + val curriedSum: Int => Int => Int = sum |
| 32 | + |
| 33 | + sum(1)(2) shouldBe 3 |
| 34 | + curriedSum(1)(2) shouldBe 3 |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + def givenCurriedFunction_whenPartialApplied_thenReturnLowerArityFunction() { |
| 39 | + val sum: Int => Int => Int = x => y => x + y |
| 40 | + val increment: Int => Int = sum(1) |
| 41 | + |
| 42 | + increment(1) shouldBe 2 |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + def givenMultipleArgumentListsMethod_whenPartialApplied_thenReturnLowerArityMethod() { |
| 47 | + def sum(x: Int)(y: Int): Int = x + y |
| 48 | + |
| 49 | + val increment: Int => Int = sum(1) |
| 50 | + |
| 51 | + increment(1) shouldBe 2 |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + def givenMultipleArgumentsFindMethod_whenCalled_thenPredicateFunctionNeedsExplicitType() { |
| 56 | + def find[A](xs: List[A], predicate: A => Boolean): Option[A] = { |
| 57 | + xs match { |
| 58 | + case Nil => None |
| 59 | + case head :: tail => |
| 60 | + if (predicate(head)) Some(head) else find(tail, predicate) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + find(List(1, 2, 3), (x: Int) => x % 2 == 0) shouldBe Some(2) |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + def givenMultipleArgumentListFindMethod_whenCalled_thenPredicateFunctionDoesNotNeedExplicitType() { |
| 69 | + def find[A](xs: List[A])(predicate: A => Boolean): Option[A] = { |
| 70 | + xs match { |
| 71 | + case Nil => None |
| 72 | + case head :: tail => |
| 73 | + if (predicate(head)) Some(head) else find(tail)(predicate) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + find(List(1, 2, 3))(x => x % 2 == 0) shouldBe Some(2) |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + def givenGenericMultipleArgumentListSumMethod_whenPartialApplied_thenReturnSimpleMethods() { |
| 82 | + def sumF(f: Int => Int)(x: Int, y: Int): Int = f(x) + f(y) |
| 83 | + val sum: (Int, Int) => Int = sumF(identity) |
| 84 | + val sumSquare: (Int, Int) => Int = sumF(x => x * x) |
| 85 | + val increment: Int => Int = sum.curried(1) |
| 86 | + val decrement: Int => Int = sum.curried(-1) |
| 87 | + |
| 88 | + sum(1, 2) shouldBe 3 |
| 89 | + sumSquare(1, 2) shouldBe 5 |
| 90 | + increment(2) shouldBe 3 |
| 91 | + decrement(2) shouldBe 1 |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + def givenMultipleArgumentsFunction_whenUseItAsOneArgumentFunction_thenNeedsExplicitArgumentPassing() { |
| 96 | + val sum: (Int, Int) => Int = (x, y) => x + y |
| 97 | + val numbers: List[Int] = List(1, 2, 3) |
| 98 | + |
| 99 | + numbers.map(n => sum(1, n)) shouldBe List(2, 3, 4) |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + def givenCurriedFunction_whenUseItAsOneArgumentFunction_thenDoesNotNeedExplicitArgumentPassing() { |
| 104 | + val curriedSum: Int => Int => Int = x => y => x + y |
| 105 | + val numbers: List[Int] = List(1, 2, 3) |
| 106 | + |
| 107 | + numbers.map(curriedSum(1)) shouldBe List(2, 3, 4) |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments