https://kotlinlang.org logo
#arrow-contributors
Title
# arrow-contributors
a

addamsson

07/21/2019, 5:29 PM
@simon.vergauwen do you think that persistent data structures synergize really well with lenses / optics?
im' just reading the docs and it occurred to me
s

simon.vergauwen

07/21/2019, 7:10 PM
Optics are meant to be used with immutable data so it's a perfect fit. However it's solves a different problem than the typeclass hierarchy.
a

addamsson

07/22/2019, 7:25 AM
it solves the problem of mutating deeply nested immutable structures, no?
like
assoc-in
in clojure
s

simon.vergauwen

07/22/2019, 7:27 AM
Not only that, it also solves the problem that those operations aren't reusable or composable.
Not sure if that's similar in Clojure or not tho.
a

addamsson

07/22/2019, 7:57 AM
does this also solve thread safety?
like atoms/refs in clojure?
s

simon.vergauwen

07/22/2019, 7:58 AM
We have
Ref
in Arrow Fx 🙂
a

addamsson

07/22/2019, 7:59 AM
what does it do?
s

simon.vergauwen

07/22/2019, 8:00 AM
It’s a functional atomic ref (or a higher kind version of AtomicReference). It’s built on top of effects so we can take into account exceptions and async jumps and we can guarantee safety across that.
a

addamsson

07/22/2019, 8:19 AM
how about performance?
s

simon.vergauwen

07/22/2019, 8:20 AM
It’s uses a similar principle as typeclasses. All Optics are interfaces so you can delegate to whatever preforms best but in general we can derive all this boilerplate at compile time. I.e. data classes just use
copy
to update immutable fields and
copy
gets generated by the compiler.
Sealed classes use
when
which is equivalent to
TABLE_SWITCH
so super fast
Complexer structures with Optics like
Fold
or
Traversal
can be derived from
Foldable
and
Traverse
so they always should be the highest performing.
The idea is also that you use code gen with Optics so you get composable (re-useable) and boilerplate free code to work with immutable data.
A very nice example when working with a complex structure like Json here. https://github.com/47deg/helios/blob/master/helios-sample/src/main/kotlin/helios/sample/sample.kt
I need to work on docs there 2…. but the code for Optics is vey small (less than 200 loc) so definitely interesting to check out if you’re curios about the internals. I hand wrote everything there to produce an artefact with small bytecode 🙂
how about performance?
Or did you mean something else?
a

addamsson

07/22/2019, 9:12 AM
yep I understand how the code is generated / can be overridden
I was asking about the atomic ref
i'm interested in the performance characteristics of ref vs actor or simple thread confinement
s

simon.vergauwen

07/22/2019, 9:12 AM
Oh, it just wraps
AtomicReference
. And for MPP we’ll wrap kotlinx.atomicfu which is also a simply a typealiased
AtomicReference
for Java.
i’m interested in the performance characteristics of ref vs actor or simple thread confinement
Oh, that’s more related to architecture than it’s related to individual building blocks of Arrow. In Arrow Fx you’ll typically find all low level building blocks but in the context of
F
.
Ref
is
AtomicReference
,
Promise
has many use-cases but a good example is that you can use it as a latch to do synchronisation and
Semaphore
. So it’s all low level building blocks concurrency people are typically familiar with.
Wish I could comment more on your actual question but I am not knowledge enough on those subjects.
Feel free to share any lecture you find 🙂
a

addamsson

07/22/2019, 11:55 AM
i'll write some prototypes after my vacation
i've tried Kotlin's
actor
but its performance is abysmal
i need shared mutable state
but the state needs to remain consistent
s

simon.vergauwen

07/22/2019, 11:55 AM
Ye, I imagine. I have an
actor
implementation on top of
IO
and concurrency tools in Arrow
a

addamsson

07/22/2019, 11:56 AM
that's why i need the persistent data structures
so I don't have to confine reads only writes
but with atomic-fu I might not even need to confine writes
s

simon.vergauwen

07/22/2019, 11:56 AM
Then my solution might work for you, although it’s rather an example as an actual fast impl.
It’s a combination of
Queue
,
Ref
and
Promise
. Where you basically have a
Queue
of
Promise
and you can
take
in the actor to act on a message and you can
put
in the
Queue
to send a message.
a

addamsson

07/22/2019, 11:57 AM
i see
i figured something similar
that writes are wrapped in an
Eval
and put in a queue
and a single threaded coroutine executes them in sequence
i don't even need a queue/eval for this since this functionality is provided by coroutines out of the box
s

simon.vergauwen

07/22/2019, 11:59 AM
I based it on this. https://github.com/vlovgr/actors-cats-effect-fs2 The 20min live coding session is very enlightening on how to build advance concurrency stuff with Arrow Fx (Cats-effects).
a

addamsson

07/22/2019, 11:59 AM
thanks I'll take a look
👍
did you have a chance to review the changes to my PR?
s

simon.vergauwen

07/22/2019, 12:07 PM
I am reviewing it now 🙂
a

addamsson

07/22/2019, 12:08 PM
👍