Skip to content

Instantly share code, notes, and snippets.

@evazamtz
evazamtz / DbEffectsGuards.scala
Created June 28, 2018 19:06
DbEffectsGuardsScala
import scala.annotation.implicitNotFound
// Db effects
sealed trait DbEffect
trait ActualRead extends DbEffect
trait StaleRead extends DbEffect
trait Write extends DbEffect
// Db effects applicability map
@evazamtz
evazamtz / DbEffects.scala
Created June 28, 2018 18:39
DbEffects compile type compitability between nodes and actions
import scala.annotation.implicitNotFound
// Db effects
sealed trait DbEffect
trait ActualRead extends DbEffect
trait StaleRead extends DbEffect
trait Write extends DbEffect
// Db effects applicability map
def confirmCode(
userModel: UserModel,
confirmationCodeType: ConfirmationCodeType,
confirmationCode: String,
newPhone: String
): DBIO[Either[Throwable, UpdateUserResult]] = {
(for {
code <- findActiveCode(confirmationCode, confirmationCodeType) eitherT
_ <- confirmCodeByIdAndBindUserToIt(code.id, userId, now) eitherT
_ <- updateUnconfirmedPhoneOrUserPhone(confirmationCodeType, userModel, newPhone) eitherT
@evazamtz
evazamtz / pattern match.sc
Created June 13, 2018 21:41
Start of MyMap
class MyMap[K,V] private(val buckets:Array[Array[(K,V)]]) extends (K => V) {
val size = buckets.length
def apply(key:K):V = get(key).get
def get(key:K):Option[V] = {
val i = Math.abs(key.hashCode() % size)
val bucket = buckets(i)
if (bucket == null) None else {
val filtered = bucket filter {_._1 == key}
if (!filtered.isEmpty) Some(filtered.head._2) else None
@evazamtz
evazamtz / MathExprsFormatter.scala
Created June 6, 2018 19:18
Math expression cool formatter for division operation
import scala.language.postfixOps
val NL = "\r\n"
implicit class StringOps(s:String) {
def hcentered(l:Int):String = (s split "\r\n") map {s => s hcenteredSingle l} reduce {_ + "\r\n" + _}
def hcenteredSingle(l:Int):String = {
if (s.isEmpty) " " * l else {
if (s.length >= l) s else {
@evazamtz
evazamtz / pattern match.scala
Created May 29, 2018 19:54
Algebraic Expr reducer
abstract class Expr
case class Var(name: String) extends Expr {
override def toString:String = s"$name"
}
case class Number(num: Double) extends Expr {
override def toString = num.toString
}
case class UnOp(operator: String, arg: Expr) extends Expr {
@evazamtz
evazamtz / api__v2_7.yml
Created May 24, 2018 09:25
2.7 API YAML
---
# SEE: http://schemas.xmlsoap.org/wsdl/
title: PimPay Platform Api Manifest
version: 2.7
uversion: v2_7
directory: v2_7 # лежит в soap/v2_7
date: 02.09.2016
wsdl:
schemaLocation: http://platform.api.%fqdn%/v2_7/soap/wsdl
invokeLocation: http://platform.api.%fqdn%/v2_7/soap/invoke
@evazamtz
evazamtz / lesson2.scala
Created May 21, 2018 20:11
lesson2.scala
object Test {
def curry[A,B,C](f: (A, B) => C): A => (B => C) = a => f(a, _)
def uncurry[A,B,C](f: A => B => C): (A, B) => C = f(_)(_)
def compose[A,B,C](f: B => C, g: A => B): A => C = a => f(g(a))
def mul(a:Int, b:Int) = a * b
val mulC = curry(mul)
mulC(5)(10)
object Test {
def curry[A,B,C](f: (A, B) => C): A => (B => C) = a => f(a, _)
def uncurry[A,B,C](f: A => B => C): (A, B) => C = f(_)(_)
def compose[A,B,C](f: B => C, g: A => B): A => C = a => f(g(a))
def mul(a:Int, b:Int) = a * b
val mulC = curry(mul)
mulC(5)(10)
@evazamtz
evazamtz / lub.scala
Created May 14, 2018 18:16
LUBConstraint
trait LUB[L <: HList, B] {
def apply(value:L):Boolean
}
object LUB {
def apply[L <: HList, B](implicit inst: LUB[L, B]): LUB[L, B] = inst
implicit def hlistLub[L <: HList, B, S0 <: HList, F0 <: HList, N <: Nat](implicit
subtype: SubtypeUnifier.Aux[L, B, S0],
filter: FilterNot.Aux[S0, B, F0],
len: Length.Aux[F0, N],