George Nacouzi
07/31/2021, 10:49 AMStefan Oltmann
08/02/2021, 8:33 AMAndrea Giuliano
08/02/2021, 12:59 PMdata class Range(
val start: Int,
var steps: Int,
var repeat: Int
)
In practice a Range(2, 2, 5) represents the following sequence: 2, 4, 6, 8, 10
Now I want to write a function intersect(other: Sequence): Sequence
that given 2 sequences is able to intersect them.
Now I’m aware that I can go into the Kotlin ranges and/or build an array and do intersect on them. However what I was trying to do is to find a more efficient (constant) solution just using math.
So I came up with this implementation
data class Range(
val start: Int,
var steps: Int,
var repeat: Int
) {
fun intersect(other: Range): Range {
val delta = leastCommonMultiple(steps, other.steps)
val maxStart = max(start, other.start)
val minStart = min(start, other.start)
val start = minStart + (delta * ((maxStart - minStart + delta - 1)/delta)) //this is wrong
val repeat = stepsTo(maxStart, delta, min(getLast(), other.getLast()))
return Range(
start,
delta,
repeat
)
}
private fun stepsTo(start: Int, delta: Int, target: Int): Int {
return (target - start + delta - 1)/delta
}
private fun greatestCommonDivisor(a: Int, b: Int): Int {
return if (a == 0) b else greatestCommonDivisor(b % a, a)
}
private fun leastCommonMultiple(a: Int, b: Int): Int {
return a / greatestCommonDivisor(a, b) * b
}
private fun getLast() =
start + steps * (repeat - 1)
}
But still the way start is calculated is wrong since it doesn’t place the number in the sequence..
I wonder if anyone has solved this problem already and in case you have any suggestion?Benoît Liessens
08/02/2021, 6:59 PMaction
parameter optional by giving it a default "no-op" value
fun move(predicate: (Node<T> -> Boolean), action: (Node<T> -> Unit)) {
// body
}
Tower Guidev2
08/04/2021, 2:40 PMMisileLab
08/04/2021, 3:06 PMSergey Permiakov
08/04/2021, 7:48 PMzsperske
08/04/2021, 8:02 PMinterface SchemaFactory<in E: Event, out S: Schema> {
fun create(event: E) : S
}
class TestEventFactory : SchemaFactory<TestEvent, TestEventSchema>
private val factories: Map<Class<out Event>, SchemaFactory<Event, Schema>> = mapOf(TestEvent::class.java to TestEventFactory())
However I get the following error message on `factories`:
Type mismatch.
Required:
Map<Class<out Event>, SchemaFactory<Event, Schema>>
Found:
Map<Class<out Event>, TestEventFactory>
MisileLab
08/05/2021, 12:39 AMfun changeIntToDouble(number: Int) {
return number.toDouble()
}
but my ide said Type mismatch: inferred type is Int but Unit was expected
How can I fix it?MisileLab
08/05/2021, 5:04 AMfun kotlinlol {
println("1")
// sleep 1 sec
println("2")
}
Ayfri
08/05/2021, 11:08 AMMisileLab
08/06/2021, 4:34 PMGordon Child
08/06/2021, 8:08 PMline.split(": ", 2)
(line is a String). I've tried line.split(": ", false, 2)
and line.split(delimiters = ": ", limit = 2)
Ayfri
08/08/2021, 2:42 AMclass Test<T = string> {
readonly thing: T
}
const a = new Test() // this works
const b = new Test<number>() // this too
Slackbot
08/09/2021, 3:05 AMalthaf
08/13/2021, 1:19 PMenum class Role(val letter: Char) {
APPROVER('U'),
ADMIN('D'),
INQUIRER('R'),
OPERATOR('O');
}
we have create this enum for code readability and we don't have direct control over how server is returning constants for the user roles, so they have done it this way 'U' , R etc
apiRespose.role = "U"
Role.valueOf(apiResponse.role) will not work ,as it is expecting 'APPROVER' as input . How can i achieve same benefit of Role.valueOf( ) wrt ot the 'letter' attribute in the enumalthaf
08/14/2021, 3:09 PMDavid Khol
08/15/2021, 9:37 AMCoroutineScope
.
Real life example which I came across so many times can be a convenience method like this:
fun launchWithLoading(action: suspend () -> Unit) {
scope.launch {
try {
loading = true
action()
} finally {
loading = false
}
}
}
Nothing prevents the developer from calling it in a suspending context like:
suspend fun alreadySuspending() {
launchWithLoading {
// do stuff
}
}
or like
fun first() {
launchWithLoading {
another()
}
}
fun another() {
launchWithLoading {
// calculate something
}
}
although rarely they intend to break the structured concurrency.
I wonder if there is something like Android Lint but that is purely for Kotlin?
Almost everything I've found on the internet points me in the direction of Android Lint. But since this unrelated to Android, I would like to solve it without coupling the solution to Android or Android Studio, so that it can be potentially used in IntelliJ IDEA as well.Klitos Kyriacou
08/16/2021, 2:58 PMopen class KotlinPractice(val id: Int) {
override fun equals(other: Any?) =
this === other ||
other != null &&
other::class == KotlinPractice::class &&
(other as KotlinPractice).id == id
override fun hashCode() = id
}
Is there a reason other
is not smart-cast and instead I have to cast it to KotlinPractice manually?wrongwrong
08/16/2021, 8:06 PMKFunction
without kotlin-reflect
(using kotlinx-metadata-jvm
), but I couldn't get Metadata
with valid information, so I have a question.
What I want to achieve is as follows.
1. get the information of arguments and return values from KFunction
.
2. get javaConstructor
or javaMethod
from KFunction
.Lucas Marques
08/18/2021, 5:27 PMresources/application.yml
as the config file.
I want to create a specific package for each of this kinds of tests: unit, integration and end-to-end, but now I have a package only for unit tests.
The problem is: every time I run the unit tests, my Spring Boot application try to connect with Kafka and Postgres (but for this purpose, I don't need them). How can I avoid to connect with these tools when running my unit tests? Also, do you know some blog post or documentation that shows how to create distinct packages for each kind of tests? Thanks in advance 🙂Danish Ansari
08/19/2021, 7:06 AMDavid Smith
08/19/2021, 10:21 AMPitel
08/19/2021, 11:33 AMenum class SomeHeaders(val label: String)
(for headers of different tables). How can I write a type parameter (like val a: ???WHAT???
) so that it will be clear that it's enum
(so I can use values()
, etc.) and it had the label
parameter. The parameter has easty solution: interface, but what about the enum
limitation?William Reed
08/19/2021, 1:26 PMkotlin.contracts.ExperimentalContracts
specificallysmallufo
08/19/2021, 3:40 PMsealed interface
implementations ? ( Maybe classes or objects )nilTheDev
08/19/2021, 4:49 PMfun main() {
val strs = listOf(
"line1",
"line2"
)
val writer = File("src/main/resources/logging/temp.txt")
.writer()
strs.forEach { writer.appendLine(it) }
writer.close()
}
The temp.txt
file is empty when I run this program.
After that the contents of the temp.txt
file looks something like this,
line1
line2
This is a perfectly reasonable output. However, things go wrong when I run a slightly modified version of the code for the second time.
fun main() {
val strs = listOf(
"line3",
"line4"
)
val writer = File("src/main/resources/logging/temp.txt")
.writer()
strs.forEach { writer.appendLine(it) }
writer.close()
}
This is the new version. But this time the temp.txt
file wasn't empty. It had the output of the first program saved into it.
However, after running the new version the temp.txt
file looks like this,
line3
line4
The contents written by the first program are completely wiped out.
Isn't the temp.txt
file supposed to look like this?
line1
line2
line3
line4
If not then what's even the point of using append
over write
? Isn't append
expected to write new content after the previously added content instead of wiping out and starting afresh?
Or, am I missing something?Florian
08/19/2021, 11:07 PMfixedRateTimer
function appropriate and accurate enough to create a countdown timer? And is there any problem with launching a coroutine inside the timer action block?
fun startTimer() {
timer?.cancel()
applicationScope.launch {
val selectedTask = selectedTask.first()
if (selectedTask != null) {
taskDao.updateLastActiveTimestamp(selectedTask.id, System.currentTimeMillis())
startTimerService()
timerRunningFlow.value = true
timer = fixedRateTimer(period = TICK_DELAY) {
applicationScope.launch {
taskDao.increaseMillisCompletedToday(selectedTask.id, TICK_DELAY)
}
}
}
}
}
nilTheDev
08/20/2021, 5:26 AMkotlin.io.path
. Tried invalidating caches and restart. Still no luck. Any idea on how to make this work?Ayfri
08/20/2021, 6:26 AMLong::longValue()
: https://www.geeksforgeeks.org/long-longvalue-method-in-java/ ?Ayfri
08/20/2021, 6:26 AMLong::longValue()
: https://www.geeksforgeeks.org/long-longvalue-method-in-java/ ?ephemient
08/20/2021, 7:24 AMAyfri
08/20/2021, 7:25 AM