Ties
10/17/2022, 6:35 PMreactormonk
10/21/2022, 12:53 PMwhen
statement?
Aka
when (Pair(x, y)) {
Pair(true, false) -> ...
}
reactormonk
10/28/2022, 11:41 AMPair
match?
val mimes = rec.mimeType.split("/")
when (mimes[0] to mimes[1]) {
"text" to "vcard" -> {}
"video" to _ -> {}
}
^ the second one isn't really working out.Kristian Nedrevold
11/09/2022, 8:24 PMstruct Circle {
radius: u32,
}
impl Circle {
fn new(radius: u32) -> Circle {
Circle { radius }
}
}
trait Shape {
fn area(&self) -> f64;
fn perimeter(&self) -> f64;
}
impl Shape for Circle {
fn area(&self) -> f64 {
std::f64::consts::PI * (self.radius as f64).powi(2)
}
fn perimeter(&self) -> f64 {
2.0 * std::f64::consts::PI * (self.radius as f64)
}
}
fn pretty_print<T: Shape>(shape: T) {
println!("Area: {}", shape.area());
println!("Perimeter: {}", shape.perimeter());
}
fn main() {
let circle = Circle::new(1);
pretty_print(circle);
}
So the trait is like a Kotlin Interface. And now my function pretty_print can take any type(struct) that implements Shape. This seems like a very powerful feature to me as it means I can extend types. How can I achieve the same thing in Kotlin?gsala
12/02/2022, 8:34 AMfun ((A, B) -> Unit).whatShouldThisBeNamed(b: B): (A) -> Unit {
return { a -> this(a, b) }
}
(A function that returns a simpler function by providing one of the parameters to the first function)Szymon Sasin
12/05/2022, 7:02 PMval s = sequenceOf("Lorem", "ipsum", "dolor", "sit", "amet")
What’s the best way to limit elements so total size is smaller than desired.
For example:
val s2 = s.someFunction(maxSize = 17) { it.length }
println(s2.toList()) // [Lorem, ipsum, dolor]
Marius Kotsbak
12/08/2022, 12:09 PMinterface ServiceLocator<I> {
// fun getRealImpl(): I
fun getEmulatedImpl(): I
// fun getStubbedImpl(): I
}
context(ServiceLocator<I>)
inline fun <reified I> locateEmulated(): I = getEmulatedImpl()
object StringServiceLocator : ServiceLocator<String> {
override fun getEmulatedImpl(): String = "test"
}
object LongServiceLocator : ServiceLocator<Long> {
override fun getEmulatedImpl(): Long = 5
}
object StringLongLocatorFactory: ServiceLocatorFactory {
// In the future (scope properties): with val ...
inline fun <reified I> locate(): I {
with(LongServiceLocator) {
with(StringServiceLocator) {
return locateEmulated<I>()
}
}
}
}
No required context receiver found: Cxt { context(ServiceLocator<I>) public inline fun <reified I> locateEmulated(): I defined in [...] in file ServiceLocatorFactory.kt[SimpleFunctionDescriptorImpl@1aa4cbc9] }Kristian Nedrevold
12/27/2022, 2:22 PMfun <A, B> memoize (f: (A) -> B) = { a: A ->
val cache = mutableMapOf<A, B>()
cache.computeIfAbsent(a) { f(a) }
}
reactormonk
12/29/2022, 2:00 PMjulian
01/05/2023, 6:14 PMBreaker ACT
02/01/2023, 3:00 AMPiotr Krzemiński
02/08/2023, 7:51 AMvar
or for
loops (mutability in general), with a way to opt out in certain cases. Think: a step towards more pure FP. I’m wondering if you’d be keen to use it and, if yes, what approach you’d suggest to implement it. I’m thinking about a detekt rule or a compiler plugin that would emit an error or a warning (configurable)Teimatini Marin
02/10/2023, 4:39 PMfun main() = runBlocking {
var lastline = ""
readHugeFile().collect { value ->
val lines = (lastline+value).lines()
lines.dropLast(1).forEach {
println(it)
}
lastline = lines.last()
}
println(lastline)
}
fun readHugeFile(): Flow<String> = flowOf(
"""line 1
|line 2
|li""".trimMargin(),
"""ne 3
|line 4
|lin""".trimMargin(),
"""e 5
|line 6
""".trimMargin()
)
dave08
02/13/2023, 4:50 PMdave08
02/14/2023, 12:15 PMclass Foo : IFoo
instead of fun Foo() = object : Foo
?dave08
02/16/2023, 12:08 PM?.
and ?:
be overridable operators for a non-null type... then it could avoid all the `bind()`s and `flatMap`s and hacks that Arrow is doing... did someone ever propose such a thing? (or maybe some similar operator if people might find it too confusing and think that they're dealing with nullables.)João Gabriel Zó
02/16/2023, 5:23 PMResult<Unit>
the best thing to do here or is there anything else in the std library?dave08
02/20/2023, 10:43 AMRuntime.getRuntime().addShutdownHook(hook)
, is that only SIGINT? It seems like in your video it was clearly using SIGINT, but in my current version not...Mazhar Ibna Zahur
02/23/2023, 10:58 AMNorbi
02/25/2023, 9:16 AMNorbi
03/01/2023, 9:26 AMraise()
functionality and I try to find a library function similar to the following hypothetical orElse()
:
context(Raise<String>)
fun f1() = 1
context(Raise<String>)
fun f2(): Nothing = raise("error")
suspend inline fun <R, A> Effect<R, A>.orElse(noinline block: suspend (R) -> A) =
fold(
recover = block,
transform = ::identity
)
val v1 = effect { f1() }.orElse { 33 }
assertEquals(1, v1)
val v2 = effect { f2() }.orElse { 33 }
assertEquals(33, v2)
I use arrow 1.1.6-alpha.36.
Thanks.Kristian Nedrevold
03/07/2023, 9:18 PMdori
03/10/2023, 10:24 AMjean
03/17/2023, 11:26 AMdata class SomeData(val someValue: String) {
companion object {
fun someFunction() {}
}
}
// OR
data class SomeData(val someValue: String) {
companion object
}
fun SomeData.Companion.someFunction() {}
João Gabriel Zó
03/17/2023, 6:00 PMfunctionOne: Result<Something>
functionTwo: Result<Unit>
I’ll only call functionTwo if the first one returns a Success, and return a Result<Something>
in case both of them return a Success.
What’s the best way to do it using std lib? I thought the nested folds and maps and ifs turned out a bit uglyNorbi
05/19/2023, 7:58 AMNathan Bedell
06/27/2023, 4:19 PM(State, Action) -> State
, but my question was more about how to actually action this reducer to do stuff with the UI.
Currently we are taking in a Flow of Actions, an initial state, and updating that state (as a StateFlow) by launching a collect block on the action flow to update the state. The body of the collect statement on the flow is synchronized with a mutex so actions should (I was hoping anyway) get executed in order sequentially.
In practice, this seems to work. However, I've noticed in tests this sometimes fails, which makes me wonder if my setup of this is actually optimal. I was thinking maybe a channel of user actions might be more robust, but open to ideas!Uberto Barbini
10/08/2023, 12:21 AMreactormonk
11/22/2023, 6:31 PMreduce
where I can pass the initial acc
?reactormonk
11/22/2023, 6:34 PMS
of the result even come from? O.o