Skip to content

Instantly share code, notes, and snippets.

@LenKIM
Created July 20, 2019 03:27
Show Gist options
  • Save LenKIM/c0038a7a940dddae3715d4ee20be5bda to your computer and use it in GitHub Desktop.
Save LenKIM/c0038a7a940dddae3715d4ee20be5bda to your computer and use it in GitHub Desktop.
함수형프로그래밍에서의 전략
package com.nealford.ft.patterns
import org.junit.Test
import static groovy.util.GroovyTestCase.assertEquals
// BEGIN groovy_calc
interface Calc {
def product(n, m)
}
class CalcMult implements Calc {
def product(n, m) { n * m }
}
class CalcAdds implements Calc {
def product(n, m) {
def result = 0
n.times {
result += m
}
result
}
}
// END groovy_calc
// BEGIN groovy_strategy_test
class StrategyTest {
def listOfStrategies = [new CalcMult(), new CalcAdds()]
@Test
public void product_verifier() {
listOfStrategies.each { s ->
assertEquals(10, s.product(5, 2))
}
}
// BEGIN groovy_functional_strategy
@Test
public void exp_verifier() {
def listOfExp = [
{i, j -> Math.pow(i, j)},
{i, j ->
def result = i
(j-1).times { result *= i }
result
}]
listOfExp.each { e ->
assertEquals(32, e(2, 5))
assertEquals(100, e(10, 2))
assertEquals(1000, e(10, 3))
}
}
// END groovy_functional_strategy
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment