-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b2c448b
commit e95b407
Showing
5 changed files
with
40 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# [The 'if' function](https://www.codewars.com/kata/the-if-function "https://www.codewars.com/kata/54147087d5c2ebe4f1000805") | ||
|
||
Create a function called `_if` which takes 3 arguments: a value `bool` and 2 functions (which do not take any | ||
parameters): `func1` and `func2` | ||
|
||
When `bool` is truthy, `func1` should be called, otherwise call the `func2`. | ||
|
||
### Example: | ||
|
||
``` | ||
Kata._if(true, () -> System.out.println("true"), () -> System.out.println("false")); | ||
// prints "true" to the System out. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
interface Kata { | ||
static void _if(Boolean bool, Runnable func1, Runnable func2) { | ||
(bool != null && bool ? func1 : func2).run(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
|
||
class SolutionTest { | ||
@ParameterizedTest | ||
@CsvSource(textBlock = """ | ||
true, true | ||
false, false | ||
, false | ||
""") | ||
void sample(Boolean bool, boolean expected) { | ||
var actual = new AtomicBoolean(false); | ||
Kata._if(bool, () -> actual.set(true), () -> actual.set(false)); | ||
assertEquals(expected, actual.get()); | ||
} | ||
} |