Skip to content

Instantly share code, notes, and snippets.

import cats.Applicative
import cats.effect.Concurrent
import cats.effect.concurrent.{Deferred, Ref}
import cats.syntax.flatMap._
import cats.syntax.functor._
trait CountDownLatch[F[_]] {
def countDown: F[Unit]
def await: F[Unit]
}
import zio.{Promise, Ref, UIO, ZIO}
trait CountDownLatch {
def countDown: UIO[Unit]
def await: UIO[Unit]
}
object CountDownLatch {
def make(count: Int): UIO[CountDownLatch] = for {
ready <- Promise.make[Nothing, Unit]
@amitayh
amitayh / BrowserE2E.scala
Created September 16, 2019 19:33
Simple example of browser E2E tests using free monad
package example
import cats.effect.IO
import cats.free.Free
import cats.free.Free.liftF
import cats.implicits._
import cats.~>
object BrowserE2E extends App {
import scalaz.zio._
import scalaz.zio.clock.Clock
import scalaz.zio.console.Console
import scalaz.zio.duration._
object KafkaClone extends App {
type Consumer[R, A] = A => ZIO[R, Nothing, _]
trait Topic[A] {
; Abstract methods for monadic chaining
(defprotocol Chainable
(fmap [this f])
(bind [this f]))
; Helper methods for "either" monad
(defrecord Right [value])
(defrecord Left [error])
(extend-type Right
import scala.io.StdIn.readLine
trait IO[+A] {
def run(): A
def flatMap[B](f: A => IO[B]): IO[B] = IO(f(run()).run())
def map[B](f: A => B): IO[B] = flatMap(a => IO(f(a)))
}
object IO {
def apply[A](a: => A): IO[A] = new IO[A] {
case class Node(value: Int,
left: Option[Node] = None,
right: Option[Node] = None)
object Serializer {
private val pattern = """^(\d+)\((.*)\)$""".r
private val treeOpen = '('
private val treeClose = ')'
private val separator = ','
private val separatorLength = 1
package com.wixpress.quotes.common
import java.util.UUID
object CQRS {
////////////////////////////////////////////////////////////////////////////////
// Example application
////////////////////////////////////////////////////////////////////////////////
@amitayh
amitayh / perm.js
Created February 10, 2015 14:06
ES6 permutations calculator
function * permutations(str) {
var length = str.length;
if (length <= 1) {
yield str;
} else {
for (var i = 0; i < length; i++) {
var ch = str[i], substr = str.substr(0, i) + str.substr(i + 1);
for (var permutation of permutations(substr)) {
yield ch + permutation;
}
<?php
function match($string, $pattern) {
$automata = new Automata($pattern);
return $automata->match($string);
}
function get_chars($string) {
for ($i = 0; $i < strlen($string); $i++) {
yield $string[$i];