Skip to content

Instantly share code, notes, and snippets.

View p-pavel's full-sized avatar
💭
In pursuit for the meaning

Pavel Perikov p-pavel

💭
In pursuit for the meaning
View GitHub Profile
import scala.annotation.targetName
import cats.*
import cats.implicits.*
import cats.data.{StateT, State}
trait CRUD:
type Obj
type Identity
type Criteria
@p-pavel
p-pavel / parametricityNotNaturality.scala
Last active August 20, 2023 18:42
parametricity does not imply naturality in scala — beware implied equality
package com.perikov
import cats.*
import cats.implicits.*
import cats.laws.*
import org.scalacheck.Arbitrary
import org.scalacheck.Prop.forAll
import cats.laws.*
import cats.laws.discipline.*
<html>
<head>
<script>
let counter = 0;
let lastCounter = counter;
let lastTime = Date.now();
function showFPS() {
const elem = document.getElementById("FPS");
const currentTime = Date.now();
@p-pavel
p-pavel / test.m
Last active October 18, 2020 20:53
factorial[n_] :=
Block[{$IterationLimit = \[Infinity]}, factorial[n, 1]];
factorial[0, res_] := res;
factorial[n_, res_] := factorial[n - 1, n res];
Length @ IntegerDigits @ factorial[100000] // AbsoluteTiming (*{0.96279, 456574}*)
Length @ IntegerDigits @ (100000!) // AbsoluteTiming (* {0.050641, 456574} *)
enum Maybe[+A]:
case Just(a:A)
case None
given Functor[Maybe]:
def [A,B](fa: Maybe[A]).map(f: A ⇒ B): Maybe[B] =
import Maybe._
fa match
case Just(a) ⇒ Just(f(a))
import scala.reflect.ClassTag
import scala.compiletime._
inline def alloc[T: ClassTag](n: Int) = if n > 0 then new Array[T](n) else error("Array size should be positive")
def main(args: Array[String]): Unit = {
println(alloc[Int](100).length)
alloc[Int](0) // не компилируется
}
// Тест текущего компилятора Scala3/Dotty и освоение возможностей
// Где взять, как пользоваться VSCode в качестве IDE и
// дока по языку -- здесь: https://dotty.epfl.ch/docs/index.html
/** Основыные алгебраические структур (без законов)
*/
object AlgebraicStructures {
trait Semigroup[T] {
def op(a: T, b: T): T
/**
* Не уверен, что это то, что нужно, но это самое общее решение
* Никак не опираемся на стандартную библиотеку
* У него, возможно или наверняка, будут проблемы с боксингом, но идея такая. Дальше можно
* запинать для производительности
*
* @see https://gist.github.com/p-pavel/2708ee0e7f5beac0a71551bfd885ef67
*/
object CustomCoercion {
trait HList {
def ::[A](x: A): A :: this.type
}
trait ::[+Head, +Tail <: HList] extends HList {
val head: Head
val tail: Tail
def ::[A](x: A): A :: this.type = HCons(x,this)
}
final case class HCons[+Head, +Tail <: HList](head: Head, tail: Tail) extends (Head :: Tail) {
override def toString: String = s"$head :: $tail"
@p-pavel
p-pavel / UI.scala
Last active August 20, 2023 20:32
Входы-выходы и логика
trait IncrementDecrement[F[_]] { // Входы/выходы приложения инкремента-декремента
val increment: Stream[F,Unit] // Поток заявок на инкремент
val decrement: Stream[F,Unit] // Поток заявок на декремент
val total: Pipe[F,Int, INothing] // Переработка текущих значений в чистые побочные эффекты
}
object Logic {
import scala.concurrent.duration._
// Как работать с IncrementDecrement
def run[F[_]:Concurrent: Timer](app: IncrementDecrement[F]): Stream[F,INothing] = {