Skip to content

Instantly share code, notes, and snippets.

View lbialy's full-sized avatar

Łukasz Biały lbialy

View GitHub Profile
@lbialy
lbialy / gist:81459de956c20307ecf1
Last active January 23, 2018 17:37
Laravel multiple implementations via IoC
######################################
<?php
// project_root/app/config/orderrepository.php
return [
'implementation' => 'Voipdeploy\Application\Database\Repositories\OrmOrderRepository'
];
?>
@lbialy
lbialy / FP-Implicits-Specification.scala
Last active October 15, 2018 09:29
Scala - multiple approaches to Specification Pattern
object Specification {
type Spec[-A] = A => Boolean
implicit class SpecOps[A](_this: Spec[A]) {
def and(that: Spec[A]) = (item: A) => _this(item) && that(item)
def or(that: Spec[A]) = (item: A) => _this(item) || that(item)
def not = (item: A) => !_this(item)
}
@lbialy
lbialy / patternmatching.ts
Created November 5, 2015 08:19
Simplified tspatternmatching, updated from simplified form from Michał Załęcki: https://gist.github.com/MichalZalecki/e4119f95c0b7fba2d51c
class MatchError extends Error {
}
function match<T, S>(value:T) {
let result:(_:T)=>S;
const context = {
caseOf(predicate:(value:T) => boolean, payload:(_:T)=>S) {
if (!result && predicate(value)) result = payload;
return context;
},
@lbialy
lbialy / metrics_factorial.scala
Last active July 24, 2016 21:03
Recursive factorial in scala with performance metrics
import org.scalameter._
object FactorialRunner {
def fact(v: BigInt): BigInt = {
def recFact(v: BigInt, acc: BigInt): BigInt =
if (v == 1) acc else recFact(v - 1, acc * v)
recFact(v, 1)
}
@lbialy
lbialy / conway.scala
Last active August 2, 2016 06:33
Conway's Game of Life in functional Scala with lazy eval
type Position = (Int, Int)
type State = Set[Position]
def gameOfLife(state: State): Stream[State] =
if (state.isEmpty) Stream.empty
else state #:: gameOfLife(nextFrom(state))
def nextFrom(state: State) = {
for {
field <- getFieldsFromState(state)
@lbialy
lbialy / SixteenElements.scala
Last active October 21, 2018 01:56
Problem with AHC-based client in http4s
import cats.effect.{Effect, IO}
import cats.syntax.flatMap.toFlatMapOps
import cats.syntax.functor.toFunctorOps
import fs2.StreamApp.ExitCode
import fs2.{Pipe, Scheduler, Stream, StreamApp}
import io.circe.Json
import io.circe.jawn.CirceSupportParser
import jawn.Facade
import jawnfs2._
import org.http4s.client.Client
@lbialy
lbialy / reflection.json
Created May 4, 2019 22:01
How to build native coursier from current master
[
{
"name" : "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl",
"allDeclaredConstructors" : true,
"allPublicConstructors" : true,
"allDeclaredMethods" : true,
"allPublicMethods" : true,
"allDeclaredClasses" : true,
"allPublicClasses" : true
}
@lbialy
lbialy / error.log
Created October 6, 2019 12:04
Doobie 0.6+ native-image compilation issue
at com.oracle.svm.core.util.VMError.unsupportedFeature(VMError.java:102)
at java.lang.ClassLoader.getClassLoader(Target_java_lang_ClassLoader.java:1510)
at java.lang.Package.getPackage(Package.java:280)
at scala.reflect.runtime.JavaMirrors$JavaMirror.packageNameToScala(JavaMirrors.scala:939)
at scala.reflect.runtime.JavaMirrors$JavaMirror.$anonfun$scala$reflect$runtime$JavaMirrors$$makeScalaPackage$1(JavaMirrors.scala:951)
at scala.reflect.runtime.JavaMirrors$JavaMirror.scala$reflect$runtime$JavaMirrors$$makeScalaPackage(JavaMirrors.scala:948)
at scala.reflect.runtime.JavaMirrors.missingHook(JavaMirrors.scala:1355)
at scala.reflect.runtime.JavaMirrors.missingHook$(JavaMirrors.scala:1348)
at scala.reflect.runtime.JavaUniverse.missingHook(JavaUniverse.scala:18)
at scala.reflect.internal.Mirrors$RootsBase.universeMissingHook(Mirrors.scala:82)
@lbialy
lbialy / LazyTypeTagGuardTest.scala
Created October 6, 2019 21:05
Checking whether native-image is fine with non-evaluated Lazy[TypeTag[A]] guards
import scala.reflect.runtime.universe.TypeTag
import shapeless._
object LazyTypeTagGuardTest {
case class Whatever()
type X
def f[A](xd: A)(implicit ev: Lazy[TypeTag[A]]) = ev.value.tpe.toString
@lbialy
lbialy / ChartJs.scala
Created December 31, 2021 11:17
GraalVM Polyglot-based Chart.js renderer for Scala + ZIO
package analytics
import analytics.ChartJs.ChartJsConfig
import logging.loggerStream
import org.apache.batik.transcoder.{SVGAbstractTranscoder, TranscoderInput, TranscoderOutput}
import org.apache.batik.transcoder.image.{ImageTranscoder, PNGTranscoder}
import org.graalvm.polyglot.{Context, EnvironmentAccess, HostAccess, PolyglotAccess, Source, Value}
import org.slf4j.LoggerFactory
import zio.blocking.Blocking
import zio.stream._