Skip to content

Instantly share code, notes, and snippets.

View rudogma's full-sized avatar

Mikhail Savinov rudogma

View GitHub Profile
@rudogma
rudogma / 01 tag3.scala
Last active May 24, 2017 06:59
Tagging & Multitagging
import scala.language.higherKinds
object tag3 {
private def cast[T, U](v:U):T = v.asInstanceOf[T]
type Tag[T, +U] = { type Raw = T ; type Gag <: U }
type @@[T, +U] = T with Tag[T, U]
type Tagged[T, +U] = T with Tag[T, U]
@rudogma
rudogma / 01 tag package.scala
Last active May 25, 2017 19:05
Tagging example for Folex
import scala.language.higherKinds
package object tag {
private def cast[T, U](v: U): T = v.asInstanceOf[T]
type Tag[T, +U] = {type Raw = T ; type Gag <: U }
type @@[T, +U] = T with Tag[T, U]
type Tagged[T, +U] = T with Tag[T, U]
def tag[T, U](value:T):T @@ U = cast(value)
@rudogma
rudogma / 0.Test.scala
Last active May 30, 2017 19:33
Danger! High voltage!
import scala.language.dynamics
object Test extends App {
implicit class MyVal[T](val value:T) extends AnyVal
class Test1 extends Dynamic {
def selectDynamic(field:String):String = "Hello"
}
@rudogma
rudogma / FutureFlattenPlusShapelessNatCounter.scala
Last active June 13, 2017 20:39
Count nested implicits level using shapeless.Nat (flatten future as example)
import shapeless.ops.nat.ToInt
import shapeless.{Nat, Succ}
import scala.concurrent.Promise
import scala.util.{Failure, Success}
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits._
@rudogma
rudogma / MultiDimensional.scala
Last active June 16, 2017 12:14
Using tagged types as alternative (with no runtime overhead) to Compile time dimensional analysis at http://typelevel.org/blog/2017/06/13/libra.html
import supertagged._
/*** ONE TIME PREDEF **/
trait Opp {
type Operation
type Divide <: Operation
type Multiply <: Operation
}
@rudogma
rudogma / OverTagged0_F.scala
Last active June 24, 2017 12:09
OverTagged: typelevel inheritance(like extend, but with tags on typelevel)
import org.scalatest.{FlatSpec, Matchers}
import shapeless.test.illTyped
import TestOverTagged2._
import supertagged.{OverTagged, Tag, Tagged, TaggedType, TaggedTypeF}
import scala.language.higherKinds
class TestOverTagged2 extends FlatSpec with Matchers {
@rudogma
rudogma / 0-prepare.md
Last active June 26, 2017 19:01
Scala native tagged types + superquants 40x slower then jvm ((
clone https://github.com/milessabin/shapeless.git
cd shapeless
sbt
-- in sbt:
++2.11.11
coreNative/publishM2
exit

clone https://github.com/Rudogma/scala-superquants.git
import scala.language.implicitConversions
object Test extends App {
testShapeless()
TestSupertagged.testSupertagged()
def testShapeless(){
import shapeless.newtype
@rudogma
rudogma / 01.source.scala
Created September 22, 2017 15:32
Difference between Newtype and TaggedType
-- package.scala --
object Booter extends TaggedType[Int]
type Booter = Booter.Type
implicit class StepOps(val __v:Int) {
def next:Step = Step @@ (__v + 1)
def +(v2:Int):Step = Step @@ (__v + v2)
}
object Step extends NewType[Int, StepOps]
@rudogma
rudogma / TestNewtypes.scala
Last active January 1, 2019 17:24
Newtype upgrading for implicits & implicit conversions without imports
import scala.language.higherKinds
import scala.language.implicitConversions
object TestNewtypes extends App {
/**
Changed lines:
type NewType = Newtype[T, Tag0] with superduper.Tag[T, this.type]
def apply[TagIn, Sub, C](c: C)(implicit tagger: Tagger[TagIn, Type, Tag, Sub, C]): NewType = c.asInstanceOf[T with NewType]