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
@p-pavel
p-pavel / SbtScala3
Last active May 19, 2024 13:02
My take on how sbt syntax api can be implemented in pure scala 3
trait PseudoSbt:
type Setting[+_]
type Key[+_]
type Project
/** context to extract value of type A from Setting[A]*/
type ValueExtractor
/** Location in source file to be used to track definitions*/
type Location
@p-pavel
p-pavel / jobs.scala
Created September 11, 2023 04:35
parallel jobs simplest demo
//>using lib "org.typelevel::cats-effect:3.5.1"
import cats.effect.*
import cats.implicits.*
object HowToRunJobsInParallel extends IOApp.Simple:
def jobConstructor(r: Ref[IO, Long])(jobId: Any) = r
.modify(n => (n + 1, n))
.flatMap(v => IO.println(s"Job $jobId processed task $v"))
def run =
for
@p-pavel
p-pavel / pdp11.scala
Last active August 31, 2023 08:14
PDP-11 architecture
import compiletime.ops.int.S
type Nat[n <: Int] =
n match
case 0 => 0
case S[n] => S[n] | Nat[n]
type Octal = Nat[7]
object Registers:
@p-pavel
p-pavel / flipflop.scala
Last active August 28, 2023 16:40
flip flop
def flipFlopChain: List[Boolean] => List[Boolean] =
case Nil => Nil
case a :: rest =>
val a1 = !a
val rest1 = if a1 then flipFlopChain(rest) else rest
a1 :: rest1
def chainToString(l: Seq[Boolean]): String =
String(l.toArray.map { case true => '1'; case false => '0' })
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) // не компилируется
}