can somebody link me real world example of using a...
# arrow
m
can somebody link me real world example of using arrow and functional programming? not something that just uses
Either
and calls itself functional programming nor something too complicated for purposes of showing theory in practice.
s
Hey Marko, What would you consider a real world example? For me that means “back-end”, and that typically is mostly
suspend
+
Either
with some concurrency/parallelism on top. Sometimes
Validated
to accumulate errors when validating a form, or
Ior
when I need to figure out all failures and all success. Besides that I use Arrow Fx Coroutines a lot, there is a lot of useful operators to compose
suspend
in parallel, or repeating/retrying with compose-able Schedules, compose-able Resources (concurrent/parallel), etc
s
maybe this helps: https://github.com/arrow-kt/frdomain.kt it's based on an example from a Scala book ported to Kotlin and Arrow
👍 1
s
I’m also releasing a example back-end project soon. Using GCP, TestContainers, Ktor, Arrow (Core/Fx), Kotest, Exposed, etc
👍🏻 1
👍 11
t
@simon.vergauwen cant wait for that 🙏
🙌 1
s
I’m trying to clean it up a bit, and share the first version. It’s something I hope to keep improving as a working example of Arrow in the backend.
💯 1
m
@stojan thank you
@simon.vergauwen I should’ve made myself more clear. I want some app, Android or backed that is not just CRUD app, has some more logic to it not just write/read from database or network requests. some library that’s written FP way etc.
👍 1
looking forward to example you mentioned
i
Hi @Marko Novakovic I’m sharing this library that is focusing on functional domain modeling (optimized for event-sourcing and CQRS). It could inspire you 😉 https://github.com/fraktalio/fmodel It was inspired by Arrow and it is using Arrow on the application layer.
👀 3
Sharing some demos/usage https://github.com/fraktalio/fmodel-demos It is all very new, but I have learned a lot in the process of adopting functional
style.
Kotlin is wonderful, and Kotlin idiomatic Arrow is a cherry on the top 😉
❤️ 6
c
I will dedicate August to study this 👨‍🎓
👨‍🎓 3
🙌 1
m
@Ivan Dugalic thank you very much!
👍 1
r
@Ivan Dugalic Fraktalio is awesome!, thanks for sharing this. Does Flow ever show up to model anything in there? Many apps that use functional domain modeling in places like Scala and others end up relying in fs2 for streams or have some kind of data type to represent their streams and interactions with other systems. I was wondering how would you connect something like a Kafka stream with Fraktalio following this arch.
❤️ 1
i
Thank you @raulraja Domain lib (
<https://github.com/fraktalio/fmodel/tree/main/domain>
) is pure computation and behaviour. The Application lib (
<https://github.com/fraktalio/fmodel/tree/main/application>
) is more opinionated in regarding to the runtime target. At the moment, I am proposing couple of interfaces (eg.
<https://github.com/fraktalio/fmodel/blob/main/application/src/main/kotlin/com/fraktalio/fmodel/application/EventRepository.kt>
) with `suspend`ing only. I guess that Flow API is something that can be added as well. Thanks for suggestion!
I am also hoping that this will inspire people to create they own Application and Infrastructure layers (with or without Reactive API)
🙌 2
s
I’ve been very interested in doing something similar, but based on what I’m used to from functional streaming. A
Flow<A>
or a sequence of
suspend ()  -> A
composes in all situations, and
suspend () -> A
only composes for single element effects. So
Flow<A>
can also seen as a “abstraction” over
suspend () -> A
where you don’t know the amount of elements it will result in, when using such approaches I’ve seen that it almost always results in nicer/better leaner apps. Since you gain control over the sequencing of suspension, you can gain more power over pull-based things. It becomes much easier to add rate limiters etc, which is composition around a sequence of
suspend () -> A
.
It’s all also achievable with
suspend () -> A
, but I found that in places where you need to deal with things such as rate limiters the resulting code always gets rather complex while it can be implemented as a generic operation over
Flow<A>
.
i
Hi @simon.vergauwen Yes, indeed! You are right. Adding
Flow/Flux
to the `suspend () -> A`/`Mono` is an important step to more robust streaming API.
s
Looking forward to see this evolve!
1
i
Thanks again for your feedback. I appreciate it!
Thinking/Learning Kotlin Flow exception handling (in relation to Arrow Either).
Copy code
interface EventRepository<C, E> {
    suspend fun C.fetchEvents1(): Either<Error.FetchingEventsFailed, Flow<E>>
    fun C.fetchEvents2(): Flow<Either<Error.FetchingEventsFailed, E>>
    fun C.fetchEvents3(): Flow<E>
}
What approach to take 🙂 ?
Option 3 sounds good. Option 2 looks promising.
Option 1 is naive 🙂 I would say
s
Well these 3 are completely different 😄 1. I think this signature is super rare. It’s when building the reference of
Flow<E>
requires side-effects, and it can fail with
Error.FetchingEventsFailed
2. This is probably the most promising signature yes. It’s
Grrrr Slack constantly breaks formatting across ` and I get into an incorrect state
The 3rd one you have no explicit domain errors, but that could be valid depends on the use-cases. 2 and 3 will play nice together anyway 🙂
The first one you can probably drop the
suspend
also by wrapping that
suspend
into
Flow
before you actually emit things
Copy code
flow { setUp() }.concatFlatMap { flowOfEvents() }
i
Thanks Simon. I will stick with 2 and 3….and going with the Flow.
Iterable
is no more present.
😍 1
s
Feel free to share the PR here, I’d love to make some time to take a look
i
Essentially, it will be one Flow from fetching current Events/State from the repo till the moment you save new Events/State.
I am implementing it now, and it will be ready till the end of the week most probably…(being busy with other stuff )
Will share of course, thanks again!
s
Sounds really nice. Always happy to help 🙂
i
Switching from Iterable to Flow PR
<https://github.com/fraktalio/fmodel/pull/33>
. It is still work in progress and sorry for one big commit 😞 (it make sense to browse the branch directly:
<https://github.com/fraktalio/fmodel/tree/feature/flow>
)
@simon.vergauwen I was in hurry moving this forward. The change is major, and it will upgrade the lib version to 2.0.0. I plan to merge it soon and experiment with the API publicly (increasing minor versions)
j
@Ivan Dugalic 👏 for the plans for 2.0 Is the 2.0.0-SNAPSHOT stable enough, to give upgrading the fmodel-demos a try ? PS: thanks for using Maven 😄
👍 1
i
It is stable, please use it and share the experience. Upgrading the fmodel-demos would be awesome!!! Thanks!