Skip to content

Instantly share code, notes, and snippets.

Francois Armand fanf

Block or report user

Report or block fanf

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@fanf
fanf / selectAndExecCallback.scala
Last active Sep 14, 2019
Select a callback given a value, and exec an associated callback
View selectAndExecCallback.scala
class Callbacks[A, E, CE] {
// callbacks are all returning Units with a generic callback error `CE` type.
type READER = IO[E, (Int, IO[CE, Unit])]
def reader1(): READER = ???
def reader2(): READER = ???
def reader3(): READER = ???
// manager function call readers, does its business decision algo and return according callback
def manager(readers: List[READER]): READER = {
// default callback (if no readers // no reader with positive value) does nothing
@fanf
fanf / CountSetMemory.scala
Last active Sep 5, 2019
Scala keySet keep reference on self, forbig GC
View CountSetMemory.scala
package test
import net.sourceforge.sizeof.SizeOf
object CountSetMemory {
def bigArray = new Array(1024*1024)
def main(args: Array[String]): Unit = {
val map = Map(1 -> bigArray, 2 -> bigArray, 3 -> bigArray)
val keys1 = map.keySet
val keys2 = map.keySet.toSeq.toSet
View zio-deadlock.scala
import zio._
import zio.syntax._
// ZIO version 2.12-1.0.0-RC9.4
object Deadlock {
val runtime = new DefaultRuntime {}
trait Logger {
@fanf
fanf / setLogbackAppender.scala
Created Jul 17, 2019
Programatically set ByteArrayOutputStream appender for logback logger in scala
View setLogbackAppender.scala
// this was driving me crazy, so here we are, for everyone.
// inspiration from https://stackoverflow.com/questions/19058722/creating-an-outputstreamappender-for-logback
import ch.qos.logback.classic.Logger
import ch.qos.logback.classic.LoggerContext
import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.core.OutputStreamAppender
import org.slf4j.LoggerFactory
View learnzio.scala
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 Jun 19, 2019
Example of an async socket handling multiple connection in ZIO + nio
View TestSocket.scala
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(
View fiber_died.txt
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 Apr 22, 2019
Show how ZIO behave with/without fiber fork.
View ZioExecSemantic.scala
/*
* 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()
View bridge_errors.scala
/*
* 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
View 02-fp-concurrency-challenge.scala
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._
You can’t perform that action at this time.