Skip to content

Instantly share code, notes, and snippets.

View kciesielski's full-sized avatar

Krzysiek Ciesielski kciesielski

  • SoftwareMill
  • Lublin, Poland
View GitHub Profile
@kciesielski
kciesielski / RebateDecorator.java
Created November 22, 2012 20:29
Decorator Pattern example
public abstract class RebateDecorator implements RebatePolicy {
protected RebatePolicy decorated;
protected RebateDecorator(RebatePolicy decorated){
this.decorated = decorated;
}
}
@kciesielski
kciesielski / FunctionalDecorator.scala
Created November 22, 2012 20:49
Functional decorator
object Rebates {
type RebatePolicy = (Product, Int, Money) => Money
}
object standardRebate extends ((Double, Int) => RebatePolicy) {
def apply(rebate: Double, minimalQuantity: Int) = {
(product, quantity, regularCost) => {
val rebateRatio = BigDecimal(rebate / 100)
if (quantity >= minimalQuantity)
package com.mtgox.api;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Mac;
@kciesielski
kciesielski / aggregate2.scala
Created September 25, 2015 19:25
Functional aggregate example with Kleislis replaced by simple monads + flatMaps
object aggregate {
type ValidationStatus[S] = \/[String, S]
type ProcessingStatus[S] = \/[String, S]
type ReaderTStatus[A, S] = ReaderT[ValidationStatus, A, S]
object ReaderTStatus extends KleisliInstances with KleisliFunctions {
def apply[A, S](f: A => ValidationStatus[S]): ReaderTStatus[A, S] = kleisli(f)
}
@kciesielski
kciesielski / freemonads.scala
Last active April 12, 2019 00:09
Free Monads example
package com.softwaremill.freemonads
import cats.free.Free
import cats.~>
import cats._, cats.std.all._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
sealed trait External[A]
case class Tickets(count: Int) extends AnyVal
package com.softwaremill.react.kafka;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.util.Arrays;
import java.util.Properties;
import java.util.UUID;
package akka.kafka.scaladsl
import java.util.concurrent.TimeUnit
import java.util.{Properties, UUID}
import akka.actor.ActorSystem
import akka.kafka.{ConsumerSettings, ProducerSettings}
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Sink
import akka.testkit.TestKit
object AroundDirectives {
val timeoutResponse = HttpResponse(StatusCodes.NetworkReadTimeout, entity = "Unable to serve response within time limit.")
def aroundRequest(onRequest: HttpRequest => Try[RouteResult] => Unit)(implicit ec: ExecutionContext): Directive0 = {
extractRequestContext.flatMap { ctx =>
{
val onDone = onRequest(ctx.request)
mapInnerRoute { inner =>
withRequestTimeoutResponse(
import io.circe.semiauto._
import io.circe.syntax._
final case class Address(street: String, zipCode: String, city: String)
object Address {
implicit val addressEcncoder: Encoder[Address] = deriveEncoder[Address]
implicit val addressDecoder: Decoder[Address] = deriveDecoder[Address]
}
@kciesielski
kciesielski / CassandraJornalContextStream.scala
Last active September 23, 2019 08:12
How to stream from Cassandra and pass the context around using FlowWithContext
case class MsgMetadata(offset: query.Offset, persistenceId: String, seqNum: Long)
trait EventsJournalOffsetDao {
def offsetFor(projection: ProjectionId): Future[Option[query.Offset]]
def saveOffset(projection: ProjectionId, currentOffset: query.Offset): Future[Unit]
}
class EventsStreamFactory(
config: EventsStreamConfig,
val projectionId: ProjectionId,