https://kotlinlang.org logo
Docs
Join the conversationJoin Slack
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by Linen
arrow
  • t

    tavish pegram

    05/27/2020, 12:50 AM
    Hi all! For Arrow.Fx does it make sense to have all impure functions have IO return types (in addition to the
    suspend
    modifier) or is it best practice to just use IO to run the effects at the boundary of the system and stick to
    suspend
    to indicate impurity? For example
    suspend fun printAString(s: String): Unit
    suspend fun printAString(s: String): IO<Unit>
    At the moment I am attempting to stick to a ports and adapters architecture with
    suspend
    for impure functions generally,
    IO
    return types at the service level, and actually unwrapping/running the IO at the controllers. The data is also the boundary but it didn't make a lot of sense to me to unwrap anything there so I've just been using
    suspend
    in the repos. As a follow-up, does anyone know if it is possible to custom alias the modifier to something like
    impure
    to distinguish from coroutine suspend methods? Or am I thinking about that incorrectly? Like
    impure fun printAString(s: String): Unit
    Is that something we might want to use Arrow.Meta for? Thanks!
    m
    t
    s
    • 4
    • 8
  • p

    Peter

    05/27/2020, 3:30 PM
    hi - is there a
    foreach
    equivalent for
    Either
    in arrow?
    p
    • 2
    • 6
  • s

    streetsofboston

    05/27/2020, 3:39 PM
    The
    fold
    works, but it always seems to be a bit wrong. The
    fold
    method returns a value; it doesn’t seem fit to execute a side-effect like a
    forEach
    would do….
    p
    • 2
    • 3
  • m

    Marcin Gryszko

    05/27/2020, 4:22 PM
    @pakoito thanks for pointing me to
    arrow-fx-mtl
    . After writing my message I started to implement my own
    Concurrent
    for
    EitherT
    🙂
    😄 1
    p
    • 2
    • 2
  • p

    pakoito

    05/27/2020, 4:44 PM
    suspend fun bla(): Either<SomeError, Result> = Either.fx {
      val ble = somethingWithEither().bind()
      somethingElse(ble)
    }
    k
    • 2
    • 3
  • p

    pakoito

    05/27/2020, 4:44 PM
    that's either-based flows
    c
    k
    • 3
    • 5
  • p

    pakoito

    05/27/2020, 7:49 PM
    for 0.11 we’re bringing them together, rewriting some APIs from IO to use suspend () -> Either instead
    m
    c
    • 3
    • 3
  • f

    Fred Friis

    05/28/2020, 12:00 AM
    Hi, I have a question about bracket
    
    <https://arrow-kt.io/docs/0.10/fx/typeclasses/bracket>
    Now, let's say we want to open a file, do some work with it, and then close it. With Bracket, we could make that process look like this: val safeComputation = openFile("data.json") .bracket( release = { file -> closeFile(file) }, use = { file -> fileToString(file) }) This would ensure the file gets closed right after completing the use operation, which would be fileToString(file) here. If that operation throws an error, the file would also be closed.
    I understand this, but what if the risky thing we were doing was not in the fileToString() but also in openFile? Eg if the underlying file library throws an exception because the file doesn't exist?
    s
    p
    • 3
    • 3
  • r

    raulraja

    05/28/2020, 12:49 PM
    https://twitter.com/JorgeCastilloPr/status/1265895432129056768
    🤩 7
    👏 17
    🔝 1
    p
    t
    • 3
    • 14
  • k

    kushalp

    05/28/2020, 8:51 PM
    Are there any common examples that show how to use
    Valid
    ,
    Invalid
    and
    Either
    seamlessly? I'm thinking that this must be a fairly common pattern and would like to understand how others have done this in a simple way: 1. Make request to remote API that returns
    Either<ErrorException, ResponseData>
    where
    ResponseData
    is some
    data class
    2. Validate all of the fields in
    ResponseData
    such that they can be converted into
    Invalid
    or
    Valid
    3. Operate on the happy/sad paths
    s
    t
    • 3
    • 6
  • p

    pakoito

    05/29/2020, 1:46 AM
    mySet.k().sequence(Either.applicative())
    may just work, it should be a member or extension function actually
    p
    s
    • 3
    • 5
  • m

    Marcin Gryszko

    05/29/2020, 6:46 AM
    Continuing my exploration of
    Concurrent
    - I tried in my program replacing
    Concurrent.parMapN
    with a
    ParApplicative
    instance (obtained by
    Concurrent.parApplicative(ctx)
    ). Then, I replaced
    parMapN
    with
    mapN
    and checked which threads effects passed to
    mapN
    were executed in. To my surprise, they were executed in the caller's thread:
    C.parApplicative(dispatchers().io()).run {
        mapN(getAthleteActivities(), getAthlete()) { (apiActivities, apiAthlete) -> /* transform */ }
    }
    I checked quickly the implementation; to have a concurrent execution, I have to use
    map2
    extension function:
    C.parApplicative(dispatchers().io()).run {
        getAthleteActivities().map2(getAthlete()) { (apiActivities, apiAthlete) -> /* transform */ }
    }
    Is this a conscious design decision? Should
    mapN
    be delegated to
    parMapN
    ?
    s
    • 2
    • 6
  • t

    than_

    05/29/2020, 10:39 AM
    hi, am I missing something or is this a bug?
    val list = listOf(1,2,3,4).k()
    
    ListK.monadFilter().fx.monadFilter {
        list.bindWithFilter { it % 2 == 0 }
    }.let(::println)
    crashes with NPE. But this works fine
    val list = listOf(1,2,3).k()
    
    ListK.monadFilter().fx.monadFilter {
        list.bindWithFilter { it % 2 == 0 }
    }.let(::println)
    r
    a
    • 3
    • 7
  • j

    Jörg Winter

    05/29/2020, 11:05 AM
    Is there an example of the Haskell analog 'mapM with IO' in arrow core or arrow fx ? I want to build a List (or Traversable, ..) of IO actions, and run them by mapM'ing over that data. is it only foldMapM from ListKFoldable ? or is there a more compact way somewhere ?
    s
    • 2
    • 4
  • r

    rt

    05/29/2020, 2:16 PM
    Is there a shortcut to create a lens for a generic class? I guess I’m looking for plens, but I’d like to avoid writing everything manually
    r
    s
    • 3
    • 16
  • j

    julian

    05/29/2020, 2:35 PM
    Scala has implicit parameter injection that provides typeclasses to methods. Is there a way with arrow not to have to explicitly provide typeclasses in function calls?
    p
    b
    i
    • 4
    • 6
  • k

    kushalp

    05/29/2020, 8:23 PM
    What is a
    Kind<ForListK, String>
    and how can I convert it into a
    List<String>
    ?
    s
    • 2
    • 1
  • c

    Chris Cordero

    05/30/2020, 2:29 AM
    Is it possible to compose one
    IO.fx
    object in another
    IO.fx
    object?
    s
    • 2
    • 8
  • c

    Chris Cordero

    05/30/2020, 2:41 AM
    I'm a little confused as to when you would use
    IO.fx
    and when you would just use
    IO
    s
    c
    r
    • 4
    • 9
  • s

    simon.vergauwen

    06/01/2020, 2:22 PM
    If you’d want an additional delay first.
    IO.sleep(5.seconds).flatMap { 
      IO.effect { println("${Instant.now()}: test") }
        .repeat(IO.concurrent(), Schedule.spaced(IO.monad(), 5.seconds)) 
    }
    k
    • 2
    • 7
  • c

    Chris Cordero

    06/01/2020, 4:15 PM
    huh, what is that
    .k()
    doing?
    b
    p
    a
    • 4
    • 6
  • c

    Chris Cordero

    06/02/2020, 4:10 AM
    If you have a
    IO<List<A>>
    is there a great way to transform it into a
    List<IO<A>>
    instead of doing:
    val foo: IO<List<A>> = ...
    val (bar) = foo
    val final = bar.map { IO.just(it) }
    j
    • 2
    • 2
  • t

    tim

    06/02/2020, 9:12 AM
    Hi folks, can anyone recommend a good article on flatMap vs map? I'm finding that i'm almost always using map in my code and so suspect I'm missing something as I learn FP
    s
    a
    +2
    • 5
    • 28
  • p

    pakoito

    06/02/2020, 9:17 PM
    If you change the first line to
    val oe: Eq<Option<Int>> =
    instead of the inferred `OptionEq<A>`then the second line will stop working
    j
    • 2
    • 2
  • j

    julian

    06/03/2020, 1:31 AM
    There's a pattern that appears a lot when I try to code more functionally.
    sealed class A {
        data class B(val b: String) : A()
        data class C(val c: Int) : A()
    }
    
    class D
    
    val a: A = TODO()
    
    fun f(a: A): D {
        when (a) {
            is A.B -> { TODO() }
            is A.C -> { TODO() }
        }
    }
    I end up with so many of these `when`s throughout my code. Am I doing something wrong? Or is my allergy to it due to coming from doing only OOP for years? With OOP
    A
    would have some abstract method that
    B
    and
    C
    would implement.
    b
    b
    p
    • 4
    • 15
  • c

    CLOVIS

    06/03/2020, 9:53 AM
    I guess the same style could be used to define
    Validated<E, A>
    as
    Union2<Success<A>, Collection<E>>
    • 1
    • 1
  • t

    thanerian

    06/03/2020, 9:54 AM
    I see Union like anonymous sealed class
    👍🏾 1
    c
    j
    • 3
    • 8
  • r

    raulraja

    06/03/2020, 2:39 PM
    As soon as we have stream we can model those more sane
    k
    s
    • 3
    • 6
  • t

    tim

    06/03/2020, 5:37 PM
    Hi all, another newb question ​sor​ry​ 🙂 I'm trying to figure out how to combine multiple Eithers into one Either where their Rs form a list... Something like this:
    val a = Either.right(1) // or left
    val b = Either.right(2) // or left
    val c = Either.right(3) // or left
    
    val d = // magic
    
    d // Either<Nothing, List<Int>>
    I've tried playing with flatMap, but i end up with flatMaps inside flapMaps ... which isn't very nice:
    val d = a.flatMap { a ->
     b.flatMap {
       Either.right(listOf(a, it))
     }
    }
    ...
    I think I'm just missing something basic here as I cant imagine my use case here is unique?
    a
    • 2
    • 10
  • t

    tim

    06/03/2020, 6:46 PM
    @julian the one from my scratch pad is this:
    val a = Either.right(1)
    val b = Either.right(2)
    val c = Either.right(3)
    
    val d = Either
        .applicative<Nothing>()
        .mapN(a, b, c) { (a, b, c) ->
            listOf(a, b, c)
        }
        .fix()
        .fold(
            ifLeft = { println("try again :(") },
            ifRight = { println("success: $it") }
        )
    But I'm now integrating it into my actual use case which is about parsing variables from a url path
    j
    p
    • 3
    • 11
Powered by Linen
Title
t

tim

06/03/2020, 6:46 PM
@julian the one from my scratch pad is this:
val a = Either.right(1)
val b = Either.right(2)
val c = Either.right(3)

val d = Either
    .applicative<Nothing>()
    .mapN(a, b, c) { (a, b, c) ->
        listOf(a, b, c)
    }
    .fix()
    .fold(
        ifLeft = { println("try again :(") },
        ifRight = { println("success: $it") }
    )
But I'm now integrating it into my actual use case which is about parsing variables from a url path
j

julian

06/03/2020, 6:49 PM
Thanks @tim!
👍 1
t

tim

06/03/2020, 6:53 PM
Hmm I've tried to expand this a bit but no joy:
val a = getEither(1)
val b = getEither(2)
val c = getEither(3)

val d = Either
    .applicative<Int>()
    .mapN(a, b, c) { (a, b, c) ->
        listOf(a, b, c)
    }
    .fix()
    .fold(
        ifLeft = { println("try again :(") },
        ifRight = { println("success: $it") }
    )

fun getEither(value: Int) : Either<String, Int> {
    return if(Random.nextBoolean()) {
        Either.left("ouch!")
    } else {
        Either.right(value)
    }
}
I'm getting this .. its fine with Left = Nothing
p

pakoito

06/03/2020, 6:53 PM
^^^ That last one is
Either.conditionally
that warning, if you squint
is telling you you’re expecting string on the left
and the applicative expects Int on the left instead
t

tim

06/03/2020, 6:56 PM
Ahhh
So whats in applicative is the Left value of the either
haha yep that fixed it straight away thanks!
I'll adjust my original code above as that's not correct then
👍🏾 1
View count: 2