Ricky Clarkson [G]
08/16/2022, 6:18 AMMuhammad Talha
08/16/2022, 6:29 AMsimon.vergauwen
08/16/2022, 7:13 AMreactormonk
09/02/2022, 1:22 PMkotlin
@JsonClass(generateAdapter = true)
data class Gallery<T>(val galleryItems: List<GalleryItem<T>>)
@JsonClass(generateAdapter = true, generator = "sealed:type")
sealed interface GalleryItem<T> {
@TypeLabel("photo")
@JsonClass(generateAdapter = true)
data class Photo<T>(val url: T): GalleryItem<T>
@TypeLabel("video")
@JsonClass(generateAdapter = true)
data class Video<T>(val url: T): GalleryItem<T>
}
And
val x: Gallery<String>
as well as
fun parse(x: String): HttpUrl
and I would like a Gallery<HttpUrl>
in the end. In Haskell, I'd add a deriving Functor
to the type declaration and then I could use all the fun combinatorics functions. How would I do that in Kotlin?Nathan Bedell
09/28/2022, 9:01 PMTies
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 uglyJoã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 uglyf1().map { something ->
f2().map { it }
something
}
will it work?Emil Kantis
03/17/2023, 6:16 PMf1().mapCatching { t ->
f2().onFailure { throw it}
t
}
might be easiestJoão Gabriel Zó
03/17/2023, 6:30 PMephemient
03/18/2023, 6:09 AMResult
, it would look like
run {
val x = f1()
f2()
x
}
(or something simpler like f1().also { f2() }
)Result
, you just translate that to include monadic bind, e.g. `.getOrThrow()`:
runCatching {
val x = f1().getOrThrow()
f2().getOrThrow()
x
}
(or runCatching { f1().getOrThrow().also { f2().getOrThrow() } }
)either {
val x = f1().bind()
f2().bind()
x
}
(or the short version) is the intended usage: https://arrow-kt.io/docs/patterns/monad_comprehensions/pakoito
03/20/2023, 10:04 AMone().flatMap { it -> two.map { it } }
which is what flatTap
does in Arrow