Hi! I have an application that essentially resolve...
# arrow
c
Hi! I have an application that essentially resolves around making requests to various servers (for example, I can ask to query a specific documents to multiple servers, and they might all reply, or only a few reply, or none reply). The difficult thing is that some servers might request additional user information (for example, for some files a server might need authentication, but not for others, or it might ask the user for confirmation before doing something, or it might ask the user for their age... Or anything else). The ‘questions' from servers are usually yes/no questions, but sometimes can be more involved. Currently, the application is written like this: • ‘search' requests are functions that return flows • ‘action' requests are just suspended functions • when a server requests additional information, that function suspends and asks the user, then resumes and sends the information to the server. Since this project already uses some Arrow features (
Either
...), I'm wondering how I could go around to make it ‘more functional'. I see that functions that return flows are better than simple suspend functions returning a list because the side effect doesn't happen during the function execution but is ‘stored' within the type. I don't really see a way to do the same with ‘action' requests, apart from returning a Flow with a single value (or would you say that's good code?) If I understand, that's what
IO
was for, but since it seems like it has been replaced by
arrow-fx-coroutines
, I'd rather learn the ‘new version' The other problem is the user interaction: currently, the user interaction consists of a few suspend functions that are called deeply inside the program. This way, it's not really possible for the ‘client' of the application to use a different UI. Usually this would be solved with Dependency Injection, but the Arrow wiki implies Typeclasses can assume that role in a better way. Essentially, which steps would you take to change the architecture of this application so it's more functional? This particular project only runs on the JVM, if that changes anything, but I'd like to learn about techniques that work everywhere if that's possible. I've already went through and made all objects immutable, which should make the translation easier?
j
Rather than simply making your program more functional, it sounds like what you're reaching for is actually deeper - modeling your problem functionally. Unfortunately there's no quick and simple recipe for that. It requires exploration of your domain and requirements using functional thinking. There are examples of how to go about this, but applied to other domains, and not necessarily using arrow fx coroutines. But the abstract principles still apply. One such is from @Gopal S Akshintala Video -

https://www.youtube.com/watch?v=QVuMSsIUw6M&feature=youtu.be

Code - https://github.com/overfullstack/railway-oriented-validation-kotlin Another is the book Functional Programming in Kotlin. In particular, Part 2, which deals with functional design. https://www.manning.com/books/functional-programming-in-kotlin Lastly, you may want to look into Arrow fx Streams as a functional alternative to Flow.
👍 1
👌🏼 1
g
I can recommend the book ‘Domain Modeling Made Functional’. It covers how to model and design the core behavior and data of your application in a functional way. Amongst other things this helps you to design the system such that you can have different UI while still using the same business logic. https://www.oreilly.com/library/view/domain-modeling-made/9781680505481/
2
c
Thanks to all of you, I'll read/watch these ^^
👍 1