Skip to content

Commit

Permalink
Ternary if (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
lbressler13 authored Sep 26, 2022
1 parent 5d5b84a commit c7c4f49
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/basic_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:

jobs:
build:
name: "Lint and Test"
runs-on: ubuntu-latest

steps:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/main_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:

jobs:
build:
name: "Build and Test"
runs-on: ubuntu-latest

steps:
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ See [here](https://kotlinlang.org/docs/type-aliases.html) for general informatio
### Classes
Reusable classes and data classes.

### General utils
Functions that are not associated with a specific class.

### Extension functions
Functions that extend existing classes and interfaces.
Extensions are added to the broadest possible class/implementation in order to be more widely usable.
Expand All @@ -39,6 +42,7 @@ In general, these can still be associated with a single class, as this package d
│ │ │ ├── kotlin
│ │ │ │ ├── kotlinutils <-- Source code for kotlin-utils module
│ │ │ │ │ ├── classes <-- New classes defined by this package
│ │ │ │ │ ├── general <-- Generic util functions not related to a class
│ │ │ │ │ ├── sample <-- Sample of a class that already exists in Kotlin
│ │ │ │ │ ├── sample2
│ │ ├── test
Expand Down
2 changes: 1 addition & 1 deletion kotlin-utils/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "xyz.lbres"
version = "0.2.0"
version = "0.2.1"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package xyz.lbres.kotlinutils.general

/**
* Function to emulate use of ternary operator for a boolean check
*
* @param check [Boolean]
* @param trueValue [T]: value to return if [check] is true
* @param falseValue [T]: value to return if [check] is false
* @return [trueValue] if the check is true, [falseValue] otherwise
*/
fun <T> ternaryIf(check: Boolean, trueValue: T, falseValue: T): T {
return if (check) {
trueValue
} else {
falseValue
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package xyz.lbres.kotlinutils.general

import xyz.lbres.kotlinutils.int.ext.isNegative
import kotlin.math.sqrt
import kotlin.test.Test
import kotlin.test.assertEquals

class UtilsTest {
@Test
fun testTernaryIf() {
var expected = true
var result = ternaryIf(true, true, false)
assertEquals(expected, result)

expected = false
result = ternaryIf(false, true, false)
assertEquals(expected, result)

expected = false
result = ternaryIf(true, false, true)
assertEquals(expected, result)

var testInt = 12
var expectedInt = 1
var resultInt = ternaryIf(testInt.isNegative(), -1, 1)
assertEquals(expectedInt, resultInt)

testInt = -12
expectedInt = -1
resultInt = ternaryIf(testInt.isNegative(), -1, 1)
assertEquals(expectedInt, resultInt)

val isPerfectSquare: (Int) -> Boolean = {
val float = it.toFloat()
val sqrt = sqrt(float).toInt()
sqrt * sqrt == it
}

testInt = 49
expectedInt = 49
resultInt = ternaryIf(isPerfectSquare(testInt), testInt, -1)
assertEquals(expectedInt, resultInt)

testInt = 50
expectedInt = -1
resultInt = ternaryIf(isPerfectSquare(testInt), testInt, -1)
assertEquals(expectedInt, resultInt)
}
}

0 comments on commit c7c4f49

Please sign in to comment.