https://kotlinlang.org logo
Join SlackCommunities
Powered by
# functional
  • d

    dave08

    02/16/2023, 12:08 PM
    Little thought that probably wouldn't happen... if Kotlin ever decided to let
    ?.
    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.)
    s
    • 2
    • 9
  • j

    João Gabriel Zó

    02/16/2023, 5:23 PM
    I have a function which makes an external call and returns nothing, but I need to do something based on if it succeeded or not. Is returning
    Result<Unit>
    the best thing to do here or is there anything else in the std library?
    w
    a
    s
    • 4
    • 5
  • d

    dave08

    02/20/2023, 10:43 AM
    @simon.vergauwen It seems like Ktor uses
    Runtime.getRuntime().addShutdownHook(hook)
    , is that only SIGINT? It seems like in your video it was clearly using SIGINT, but in my current version not...
    s
    • 2
    • 8
  • m

    Mazhar Ibna Zahur

    02/23/2023, 10:58 AM
    how can i get the stacktrace of an error in functional style? throwing exception is not a right thing in FP.
    t
    s
    w
    • 4
    • 8
  • n

    Norbi

    02/25/2023, 9:16 AM
    Recently I've become a fan of explicit error handling using Result<V, E> (using https://github.com/michaelbull/kotlin-result). Yesterday I was shocked by the thought that handling errors this way somehow reminds me of Java's checked exceptions, with different syntax 😐 Is this analogy really correct? EDIT: I know that in Java some things are not possible like nice chaining of function calls. But in Kotlin it would be possible because of the language's "almost everything is an expression" paradigm.
    👍🏼 1
    👍 3
    r
    s
    t
    • 4
    • 18
  • n

    Norbi

    03/01/2023, 9:26 AM
    I'm experimenting with Arrow's
    raise()
    functionality and I try to find a library function similar to the following hypothetical
    orElse()
    :
    Copy code
    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.
    s
    • 2
    • 6
  • k

    Kristian Nedrevold

    03/07/2023, 9:18 PM
    I have been writing a lot of Scala 3 recently and it really has made me wish Kotlin had a bit less brackets! Kotlin feels so noisy now. Especially when writing in a functional style
    😮 3
    r
    k
    m
    • 4
    • 13
  • d

    dori

    03/10/2023, 10:24 AM
    Anyone here doing functional Kotlin on Android? I am interested to hear what wider architecture you are using.
    s
    • 2
    • 1
  • j

    jean

    03/17/2023, 11:26 AM
    Is there a preferred way to approach companion function with a functional style ?
    Copy code
    data class SomeData(val someValue: String) {
      companion object {
        fun someFunction() {}
      }
    }
    
    // OR
    
    data class SomeData(val someValue: String) {
      companion object
    }
    
    fun SomeData.Companion.someFunction() {}
    s
    • 2
    • 4
  • j

    João Gabriel Zó

    03/17/2023, 6:00 PM
    I have 2 functions which return Results.
    functionOne: 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 ugly
    e
    e
    p
    • 4
    • 8
  • n

    Norbi

    05/19/2023, 7:58 AM
    I finally had some time to experiment with my very old 🙂 idea of mutating immutable object graphs using Kotlin (with KSP help). I've created a short readme about its usage, I'd like to hear your opinions about the idea itself (eg. whether it is worth to continue development or not): https://github.com/kotlinw/kotlinw/tree/main/kotlinw-immutator
  • n

    Nathan Bedell

    06/27/2023, 4:19 PM
    Hey, wondering if anyone has best practices tips / examples for implementing a "Tool" (i.e. asynchronous user workflow with a specific start / end, as opposed to something with an open-ended lifecycle like a view) in a MVU/Elm style using Kotlin coroutines. We currently have a sealed class "action" interface for representing actions the user can perform to interact with the tool, and a reducer function
    (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!
  • u

    Uberto Barbini

    10/08/2023, 12:21 AM
    My book on functional Kotln and DDD is now available on Amazon: https://www.amazon.co.uk/Objects-Functions-Software-Functional-Programming/dp/1680508458
    👍 10
    🎉 7
    ❤️ 3
    g
    c
    • 3
    • 15
  • r

    reactormonk

    11/22/2023, 6:31 PM
    Does kotlin have a
    reduce
    where I can pass the initial
    acc
    ?
    k
    • 2
    • 1
  • r

    reactormonk

    11/22/2023, 6:34 PM
    ... where does the default
    S
    of the result even come from? O.o
    k
    e
    • 3
    • 8
  • y

    Youssef Shoaib [MOD]

    01/13/2024, 10:23 PM
    Am I right in thinking that molecule basically provides monad comprehension over lists? Because it allows you to deal with each element of a flow as they come in and make a flow out of that, and you can do that for any number of flows you want, and so it's a monad comprehension, right?
    n
    • 2
    • 5
  • j

    Jun Sekine

    01/15/2024, 5:49 AM
    Hello I’m working on an hobby oss project, ‘partial-function-kt’, which is a Kotlin analogue of Scala’s PartialFunction. Would you kindly be willing to share your thoughts, suggestions, or any feedback on this implementation? Or, if you have any good precedents, could you please share them with me? https://github.com/jsoizo/partial-function-kt
    d
    • 2
    • 6
  • t

    Thomas

    01/17/2024, 2:46 AM
    Hello all, just a quick question, I wonder if such a function exists in the Kotlin standard library? if not how should it be named? I'm calling it "combine" for now 🤔
    Copy code
    fun <A, B, R> Collection<A>.combine(other: Collection<B>, transformation:(A, B)->R): List<R> = 
        ArrayList<R>(this.size * other.size).also{
            for(a in this){
                for(b in other){
                    it.add(transformation(a, b))
            	}
            }
        }
    Example:
    Copy code
    fun main(){
       println( 
           listOf(1,2,3).combine(listOf("a", "b", "c")){a, b -> "$a-$b"} 
       )
    }
    Output:
    Copy code
    [1-a, 1-b, 1-c, 2-a, 2-b, 2-c, 3-a, 3-b, 3-c]
    e
    d
    k
    • 4
    • 30
  • t

    Thomas

    01/17/2024, 2:51 AM
    maybe
    cartesianProduct
    ?
  • c

    Christopher Hübner

    02/05/2024, 2:43 PM
    Nested functions/ nested functions as data: Top or flop?
    y
    k
    • 3
    • 5
  • r

    reactormonk

    02/05/2024, 3:04 PM
    I've got an ADT with a whole lot of members, some recursive, some not. Now that the code is evolving, I've got some members which are possible at some stages of the program, and some others at other stages. What's the best way to model this? Use multiple inheritance to tag them for the specific stages?
    y
    • 2
    • 1
  • c

    Christopher Hübner

    02/15/2024, 8:41 AM
    I am curious, if purly functional, serverside kotlin has any effects, except from confusing any java developer and making its developer irreplaceable. Any effects on cpu/ram/ other performance issues?
    g
    t
    +3
    • 6
    • 10
  • k

    Kev

    03/19/2024, 10:59 AM
    Is there a way to pass multiple context receiver scopes to a single
    with(..)
    clause?
    y
    s
    • 3
    • 3
  • e

    Emre

    04/24/2024, 4:37 PM
    I'm creating a new backend API server in Kotlin and I was wondering what best practices users here recommend around using Arrow and Kotlin.Result. What features, if any, of Arrow do you find indispensable and why, taking into consideration the fact that new programmers may not be familiar with functional programming or even Kotlin, and just Java instead? Which ones would you introduce first? As you might suspect, I'm thinking of Kotlin.Result or Arrow.Either.
    k
    t
    k
    • 4
    • 6
  • r

    reactormonk

    05/25/2024, 6:47 PM
    My local lens dealer's gonna be arrow?
    y
    s
    • 3
    • 27
  • y

    Youssef Shoaib [MOD]

    06/04/2024, 1:40 PM
    Can any FP wizard here show me how the reverse state monad would work in Kotlin? I can't seem to wrap my head around the laziness needed. I have a version that uses
    suspend
    and a
    Channel
    to pass a value "backwards" in time, but I don't trust that it's actually "correct"
    👀 2
  • r

    reactormonk

    06/19/2024, 7:28 AM
    How would I do a
    when
    on two items? Use a
    Pair
    and deconstruct using
    to
    ?
    e
    b
    • 3
    • 2
  • p

    Pablichjenkov

    06/27/2024, 10:51 PM
    Can someone explain why this whole thing about creating sealed classes with different types and all that, why is called
    algebraic data types
    ? What's the relationship with algebra?
    solved 1
    f
    g
    • 3
    • 5
  • r

    reactormonk

    11/28/2024, 12:51 PM
    I've got a list maps, and I'd like to merge them into a single map, with a value merge function. Is there one such a collection operation?
    e
    s
    • 3
    • 6
  • u

    Ulrich Schuster

    12/25/2024, 1:36 PM
    I'd like to reference a partially-applied function with receiver. Is this possible? Say, I have a function
    f(x: X, (X) -> Y): Y
    that takes some argument
    x: X
    and a function
    (X) -> Y
    . And I have a concrete function with receiver, say
    Int.makeY(x: X): Y
    . I would like to partially apply
    makeY
    to a given receiver and pass it to
    f
    , like
    Copy code
    val foo = 5
    f(x) { foo::makeY }
    However, this results in an ArgumentTypeMismatch compilation error, stating that
    Y
    was expected instead of the provided function reference. Am I simply making a mistake in my declaration, or is it simply not possible to pass a partially applied function with receiver as reference?
    e
    • 2
    • 2