Difference between Kotlin arrow IO, IO.fx, IO !effect

Legendary_Hunter

I am trying to use arrow in kotlin

Arrow has three functions

IO {}
IO.fx {}
IO.fx { !effect}

I want to know the difference between these. I know IO.fx and IO.fx {!effect} help us use side effects but then whats the difference between the two and why would I use one over the other

El Paco

While this is going to change shortly, on version 0.11.X:

  • IO { } is a constructor that takes a suspend function, so you can call any suspend function inside. It's a shortcut for IO.effect { }
suspend fun bla(): Unit = ...

fun myIO(): IO<Unit> = IO { bla() }

fun otherIO(): IO<Unit> = IO.effect { bla() }
  • IO.fx { } is the same as IO except it adds a few DSL functions that are shortcuts for other APIs of IO. The most important one is ! or bind, which executes another IO inside.
fun myIO(): IO<Unit> = IO.fx { bla() }

fun nestIO(): IO<IO<Unit>> = IO.fx { myIO() }

fun unpackIO(): IO<Unit> = IO.fx { !myIO() }

Another function it enables is the constructor effect from the first point. So what you're effectively doing is adding an additional layer of wrapping that may not be necessary.

fun inefficientNestIO(): IO<IO<Unit>> = IO.fx { effect { bla() } }

fun inefficientUnpackedIO(): IO<Unit> = IO.fx { !effect { bla() } }

We frequently see that inefficientUnpackedIO from people who come to the support channels, and it's easily replaceable by just IO { bla() }.

Why have two ways of doing the same in effect and fx? It's something we're looking to improve on the next releases. We recommend using the least powerful abstraction wherever possible, so reserve fx only when using other IO-based APIs such as scheduling or parallelization.

IO.fx {
  val id = getUserIdSuspend()
  val friends: List<User> = 
    !parMapN(
      userFriends(id),
      IO { userProfile(id) },
      ::toUsers
    )
  !friends.parTraverse(IO.applicative()) { user ->
   IO { broadcastStatus(user) } 
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What's the difference between '_io' and 'io'?

Difference between buffered io and unbuffered io

Difference between next.angular.io and angular.io

Difference between java.io.PrintWriter and java.io.BufferedWriter?

What's the difference between io.Copy and io.CopyBuffer?

What is the difference between io:format and io:fwrite in Erlang?

Difference between io.quarkus and io.quarkus.platform

Difference between usage of Dispatcher IO and Default

What's the difference between Task and IO in Scalaz?

What is difference between WebSockets and Socket.io?

socket.io rooms difference between to and in

The difference between IO and UIO in scalaz ZIO

What is the difference between Socket.IO and Firebase?

socket.io-Difference between emit functions

What is the difference between io.sockets.emit and socket.emit in io.on('connection', function(socket){})?

What's the proper way to switch between IO and UI in Kotlin coroutine?

What's the difference between socketIOClient and webSocket in socket.io in nodejs?

Unexpected huge difference in IO speed between 2 methods

What is the difference between node.js and io.js?

what is the difference between io.cucumber and info.cukes

What is the difference between DMA and memory-mapped IO?

Difference between mutual exclusion and blocked-IO in kernel programming?

What is the difference between append and write methods of java.io.writer?

What are IO ports, serial ports and what's the difference between them?

Difference between `open` and `io.BytesIO` in binary streams

Difference between io.open vs open in python

Difference between load of librosa and read of scipy.io.wavfile

What's the difference between ResponseWriter.Write and io.WriteString?

What is the difference between API key and Build Secret in Fabric.io?

difference between VM_IO and VM_RESERVED