Christian Janev
07/09/2020, 11:10 PMtoBePercent = (toBePercent.toFloat() / 100).toString()
I have this line of code and whenever I make toBePercent into 5 it converts it to "0.05" (good) but whenever I make toBePercent into 10 or above it outputs "0.0"Slackbot
07/10/2020, 7:42 AMKarlo Lozovina
07/10/2020, 6:15 PMdropLast
like function, that modifies list inplace, instead of returning a new one?Mark
07/11/2020, 2:17 AMAlexey Anufriev
07/14/2020, 11:52 PMMark
07/15/2020, 9:19 AMpoohbar
07/16/2020, 8:19 PMit.get
is expecting Nothing
.. why?Christian Sousa
07/17/2020, 3:39 PMlet finished = false
for (let i = 0; i < restrictions.length && !finished; i++) {
finished = ...
}
Christian Janev
07/18/2020, 7:50 PMoverride fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.convert -> startActivity(Intent(this, unitConvert::class.java))
}
return super.onOptionsItemSelected(item)
}
Praveen Talari
07/19/2020, 2:36 PMJoe Masilotti
07/20/2020, 12:47 AMChristian Sousa
07/20/2020, 4:31 PMtype CustomReturnType = (a: String, b: Int) => String
export class Foo {
private aggregator: CustomReturnType[] = [
(a: String): String => a,
(a: String, b: Int) => {
...
return "something"
}
]
}
On Kotlin, I did something like this:
class Foo {
var aggregator: MutableList<(a: String, b: Int) -> String> = mutableListOf()
fun first(a: String): String {
return a
}
fun second(a: String, b: Int) {
...
return "something"
}
constructor() {
aggregator.add(first)
aggregator.add(second)
}
}
Problem is I’m getting a Type mismatch
Required: (String, Int) -> String
Found: String
Does anyone know why I can’t or is there something I’m doing wrong?Gus
07/20/2020, 5:41 PMsequence {}
but it feels like an overkill to use a couroutine for this.
Consider the code below:
data class MessageWithExpiry(
val messageSerialized: ByteArray,
val expiryDate: ZonedDateTime
)
data class Batch(
val messages: Array<ByteArray>,
val latestMessageExpiryDate: ZonedDateTime
)
fun batchMessages(messages: Sequence<MessageWithExpiry>): Sequence<Batch> = TODO()
Each Batch
should contain an array of messages (taken from MessageWithExpiry.messageSerialized
) which collectively don't exceed 8 MiB. If messages
is empty, the result should be empty too. If all the messages
fit in one batch (i.e., they don't exceed the 8 MiB limit collectively), then only one batch should be output -- otherwise, multiple `Batch`es should be output. Additionally, each batch should have the latest expiry date of any message in the batch.
Intuitively, I think I should split this in two parts: One for the batching itself (using something like Sequence.scan
but not that per se), and another for the computation of each Batch.latestMessageExpiryDate
(using Sequence.map
). But I don't think Sequence.scan
, Sequence.reduce
, Sequence.fold
or any of their variations would help me do the first part.
Any pointers on whether/how I can do this using a functional approach?Christian Sousa
07/22/2020, 1:40 PMexport interface Item {
[label: string]: string[];
}
export interface SequenceMap {
'1': string[] | Item ;
'2': string[] | Item ;
}
I know that I can use a sealed class
to mimic the string[] | Item
. But what is really bugging me is how can I do something like the Item interface?
I’m not that used to Kotlin but this is something somewhat trivial in Typescript.
Any help would be great!
Thanks!Doug
07/24/2020, 5:36 AMval urlIds = ArrayList<Long?>
for (u in urls) {
urlIds.add(u.id)
}
Mervil
07/24/2020, 7:47 PMJu
07/25/2020, 12:51 PMManuel Pérez Alcolea
07/26/2020, 2:09 AMfun File.asSequence(): Sequence<String> {
val reader = this.bufferedReader()
return sequence {
while (true) {
yield(reader.readLine() ?: break)
}
}
}
and is combining scopes frown upon? for example
fun File.asSequence2(): Sequence<String> = this.bufferedReader().let {
// now sequence { } allows me to have `it` for the reader and `this` for the sequence
}
(ignore the fact that that first line in particular got really long)blue
07/27/2020, 2:56 PMvar
) property during declaration?
*T*he study material says -
full syntax for any property in Kotlin:
var <propertyName>[: <PropertyType>] [= <property_initializer>]
[<getter>]
[<setter>]
Initializer, getter, and setter are optional here, but you need to initialize your variable if you don’t want to specify its type. Otherwise, the compiler will warn you.But my code snippet is throwing compile error. Did I miss something?
class User {
var firstName: String
var lastName: String
}
Carrascado
07/27/2020, 5:08 PMCarrascado
07/27/2020, 5:08 PMCarrascado
07/27/2020, 5:18 PMCannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6. Please specify proper '-jvm-target' option
and it got fixed setting the project to compile into Java 8 with kotlinOptions
What I understand from the error message is that some part of the library is directly injecting bytecodes meant for the JVM 1.8?
I'd like to know more about this kind of details but I don't find much info searching by myself, maybe I'm not writing the right keywords when searchingmatt tighe
07/27/2020, 7:47 PMinit
block in this case warn: Leaking 'this' in constructor of non-final class A
but not in the .apply
case? Is the receiver block guaranteed to run after construction of A
completes somehow, or is this just a case that isn’t caught by the linter?
abstract class A(injectedB: B) {
init {
injectedB.observeA(this)
}
val constructedB = B().apply {
observeA(this@A)
}
}
class B {
fun observeA(a: A) {}
}
queen-of-idleness
07/28/2020, 9:33 AMGiven
Two external services should be called successively.
Calling service B depends on the response of service A.
The responses are wrapped in this monad result type.
There are two places where an error response can happen. In that case the flow should be aborted immediately and the error should be converted to a http response with a human readable body.
We want to stay with the standard library, http4k, result4k and coroutines.
So we don't want to use a library like arrow or similar.
Issues
1. Is there a better mechanism to chain the flow?
2. Should calling external services be wrapped in some structure like Future
or Deferred
? We might have to wait for their result but can't do anything in parallel in the meantime.
This is the simplified, compiling pseudo code:
data class HttpResponse(val status: Int, val body: String)
typealias RequestA = String
typealias ResponseA = String
typealias ErrorA = String
typealias RequestB = String
typealias ResponseB = String
typealias ErrorB = String
fun someFunction(
callServiceA: (RequestA) -> Result<ResponseA, ErrorA>,
callServiceB: (RequestB) -> Result<ResponseB, ErrorB>,
input: String
): HttpResponse =
callServiceA(input)
.mapFailure { HttpResponse(409, "please provide a valid id") }
.flatMap { responseA ->
callServiceB(responseA)
.mapFailure { HttpResponse(401, "you don't exist") }
.map { responseB -> HttpResponse(200, responseB.capitalize()) }
}.get()
Christian Sousa
07/28/2020, 10:46 AM"\b[01]+\b".toRegex().matches("11010011")
Basically what I’m trying to do is just check if there is something else besides 0 or 1 in the string. I tried also with
"/^[0-1]+$/".toRegex().matches("11010011")
But with the same result..matt tighe
07/28/2020, 9:43 PMthis::class.java.simpleName
using reflection?Christian Sousa
07/29/2020, 9:31 AMlet binaryString = "001110110001010001110100110111100100"
console.log(parseInt(binaryString, 2))
// output: 15859142116
But not on Kotlin:
var binaryNumber:String = "001110110001010001110100110111100100"
try {
var retr = binaryNumber.toInt(2)
println(retr)
} catch(error: Throwable){
println(error)
}
// java.lang.NumberFormatException: Invalid number format: '001110110001010001110100110111100100'
Konstantin Petrukhnov
07/30/2020, 4:39 PMCarrascado
07/30/2020, 5:50 PMmatt tighe
07/31/2020, 7:55 PM