Skip to content

Commit

Permalink
Add tests cases for the partial application
Browse files Browse the repository at this point in the history
  • Loading branch information
melvic-ybanez committed Sep 24, 2023
1 parent 63e4176 commit bb1bbd5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
16 changes: 16 additions & 0 deletions examples/partial_application.dry
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
let sum = lambda(x, y) { return x + y };
let onePlus = sum(1, _);

assert_equal("Supply first function arg only", onePlus(3), 4);
assert_equal("Supply second function arg only", sum(_, 3)(7), 10);

class HasMinus {
def minus(x, y) {
return x - y;
}
}

let tenMinus = HasMinus().minus(10, _);

assert_equal("Supply first method arg only", tenMinus(6), 4);
assert_equal("Supply second method arg only", HasMinus().minus(_, 4)(15), 11);
1 change: 1 addition & 0 deletions src/main/scala/com/melvic/dry/parsers/ExprParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.melvic.dry.result.Failure.ParseError

import scala.annotation.nowarn

//noinspection ScalaWeakerAccess
private[parsers] trait ExprParser { _: Parser =>
def expression: ParseResult[Expr] =
assignment
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/com/melvic/dry/parsers/StmtParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.melvic.dry.ast.Stmt.Loop.While
import com.melvic.dry.ast.Stmt.{BlockStmt, ExprStmt, Import, ReturnStmt}
import com.melvic.dry.ast.{Decl, Expr, Stmt}

//noinspection ScalaWeakerAccess
private[parsers] trait StmtParser { _: Parser with DeclParser =>
def statement: ParseResult[Stmt] =
select(
Expand Down
22 changes: 18 additions & 4 deletions tests/test_calls.dry
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
def test_partial_application() {
def test_function_partial_application() {
let sum = lambda(x, y) { return x + y };
let onePlus = sum(1, _);

assert_equal("Supply first param only", onePlus(3), 4);
assert_equal("Supply second param only", sum(_, 3)(7), 10);
assert_equal("Supply first function arg only", onePlus(3), 4);
assert_equal("Supply second function arg only", sum(_, 3)(7), 10);
}

test_partial_application();
def test_method_partial_application() {
class HasMinus {
def minus(x, y) {
return x - y;
}
}

let tenMinus = HasMinus().minus(10, _);

assert_equal("Supply first method arg only", tenMinus(6), 4);
assert_equal("Supply second method arg only", HasMinus().minus(_, 4)(15), 11);
}

test_function_partial_application();
test_method_partial_application();

0 comments on commit bb1bbd5

Please sign in to comment.