Skip to content

Instantly share code, notes, and snippets.

View fanf's full-sized avatar

François Armand fanf

View GitHub Profile
import scalaz.zio._
import scalaz.zio.syntax._
import utest._
class TestService(data: Ref[Map[Int, String]]) extends Database.Service {
def lookup(id: Int) = for {
map <- data.get
// I assume the semantic is to fail if key is not defined
v <- map.get(id) match {
@fanf
fanf / TestSocket.scala
Last active June 19, 2019 10:34
Example of an async socket handling multiple connection in ZIO + nio
import scalaz.nio._
import scalaz.nio.channels.{AsynchronousServerSocketChannel, AsynchronousSocketChannel}
import scalaz.zio.console._
import scalaz.zio._
object TestSocket extends App {
override def run(args: List[String]): ZIO[Environment, Nothing, Int] = {
theSocket.foldM(
@fanf
fanf / fiber_died.txt
Created June 8, 2019 09:13
ZIO fiber died
Fiber failed.
A checked error was not handled.
Chained(Error when calculating configuration for node 'relay2.rudder.local' (66fbf984-e1a9-4fea-83fe-ee14b15355b5),Chained(Error for node '66fbf984-e1a9-4fea-83fe-ee14b15355b5' bundle creation,Unexpected(We could not generate policies for server '66fbf984-e1a9-4fea-83fe-ee14b15355b5', therefore making updates for nodes behind it unavailable. Maybe you are missing 'scale out' plugin?)))
Fiber:189998 was supposed to continue to:
a future continuation at scalaz.zio.ZIO.run(ZIO.scala:1026)
a future continuation at scalaz.zio.Semaphore.withPermit(Semaphore.scala:60)
Fiber:189998 execution trace:
at com.normation.errors$IOChainError.chainError(ZioCommons.scala:119)
@fanf
fanf / ZioExecSemantic.scala
Last active April 22, 2019 21:00
Show how ZIO behave with/without fiber fork.
/*
* This test shows that without a fork, execution is purely mono-fiber and sequential.
*/
object TestZioExecSemantic {
val rt = new DefaultRuntime {}
trait LOG {
def apply(s: String): UIO[Unit]
}
def makeLog = UIO(new LOG {
val zero = System.currentTimeMillis()
/*
* My application is splitted in several modules, possibly independently built and
* distributed.
* I have a Base error trait for the whole application.
*
* Each module has its own business error defined by a module base error trait which
* extends BaseError.
*
* I want to have a common framework to manage bridging errors between modules so
* that a module can use the other ones and subsume error type to its own, especially
package challenge
// libraryDependencies += "org.scalaz" %% "scalaz-zio" % "0.11"
import com.softwaremill.quicklens._
// libraryDependencies += "com.softwaremill.quicklens" %% "quicklens" % "1.4.11"
import scalaz.zio._
import scalaz.zio.syntax._
/******************************************************************
* UPDATED VERSION HERE:
* https://gist.github.com/fanf/5b6edced8422e127ea0bf4ad2ed1b7a2
*
******************************************************************/
/*
* The below version is kept to show how clumsy and non optimal things can be
* when you use the incorrect abstraction. Please go see the link above to
* see a much better, cleaner, shorter version of the solution.
/*
This piece of commented code aims to clarify some
misconceptions between several advanced concepts
in pure functional programming/category theory:
free monads, finally tagless approach, algebraic
effects.
These concepts are actually very close. They rely
on similar concepts and even represent the "same"
object (up to isomorphism!) from the theoretical
@fanf
fanf / Test13256.scala
Created August 28, 2018 12:29
How to load Rudder LDAP entries to validate #13256
package com.normation.rudder
import com.normation.ldap.sdk.LDAPEntry
import com.normation.ldap.sdk.RWPooledSimpleAuthConnectionProvider
import com.unboundid.ldap.sdk.Attribute
import com.unboundid.ldap.sdk.DN
/*
* See:
* https://www.rudder-project.org/redmine/issues/13256