Skip to content

Instantly share code, notes, and snippets.

View arjanblokzijl's full-sized avatar

Arjan Blokzijl arjanblokzijl

  • Zürich, Switzerland
View GitHub Profile
@soronpo
soronpo / CanBuildFrom.md
Last active December 18, 2018 06:22
Scala Collections CanBuildFrom explanation by Stefan Zeiger @szeiger

Taken from https://gitter.im/scala/contributors?at=5c0981af80986419d54dd08d

Stefan Zeiger @szeiger:

I'll try to give a high-level explanation (with imprecise types): In order to build something (like the result of 1.to(10).map(identity)) you use a Builder[E, To] to which you add elements of type E and eventually get a result To. That way a single implementation of map can build different result types. In order to get such a Builder you need a factory, i.e. a () => Builder[E, To]. We call this type CanBuild and pass an implicit instance of it to map. You usually don't care who's doing the building (Range in this case) but for the sake of finding an implicit CanBuild you want the source collection to determine the result type To (e.g. calling map on a List should build another List).

@retronym
retronym / compile.log
Last active December 15, 2015 21:39
patmat-budget.scala
~/code/scala qbin/scalac -Dscalac.patmat.analysisBudget=512 -unchecked sandbox/test.scala
sandbox/test.scala:27: warning: Cannot check match for unreachability.
(The analysis required more space than allowed. Please try with scalac -Dscalac.patmat.analysisBudget=1024 or -Dscalac.patmat.analysisBudget=off.)
def foo(z: Z) = z match {
^
one warning found
@AlecZorab
AlecZorab / ACaseClass.scala
Created August 28, 2012 23:06
Hacky attempt at representing Bra-Ket notation. Term simplification could be best described as "unstructured". (or appalling, your choice)
object BraKet3_SmokeyIsTheBraket extends App {
sealed trait Expr {
e1 =>
def *(e2: Expr): Expr = Prod(e1, e2)
def +(e2: Expr): Expr = Sum(e1, e2)
def simplify: Expr = this
// a.scala
// Fri Jun 29 16:15:08 PDT 2012
import scala.reflect.makro.Context
import collection.mutable.ListBuffer
import collection.mutable.Stack
import language.experimental.macros
object Macros {
val conversionChars = "%BbCcdEefGgHhinosuXx"
@jboner
jboner / latency.txt
Last active June 29, 2024 19:54
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers (~2012)
----------------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
@xeno-by
xeno-by / gist:2559714
Created April 30, 2012 16:19
Mixing in a trait dynamically
Answers http://stackoverflow.com/questions/10373318/mixing-in-a-trait-dynamically.
Compile as follows:
scalac Common_1.scala Macros_2.scala
scalac Common_1.scala Test_3.scala -cp <path to the result of the previous compilation>
Tested in 2.10.0-M3, will most likely not compile by the time 2.10.0 final is released, because we're actively rehashing the API.
However the principles will remain the same in the final release, so the concept itself is okay.
upd. Code updated for 2.10.0-M7.
upd. Code updated for 2.10.0-RC1.
@retronym
retronym / implicitly-either.scala
Created April 20, 2012 22:24
implicitly-either
scala> def implicitlyEitherImpl[A: c.TypeTag, B: c.TypeTag](c: reflect.makro.Context): c.Expr[Either[A, B]] = {
| def i[X](implicit X: c.TypeTag[X]): c.Tree = c.inferImplicitValue(X.tpe, silent = false)
| i[A] match {
| case c.mirror.EmptyTree =>
| i[B] match {
| case c.mirror.EmptyTree =>
| sys.error("Neither A nor B are available implicitly")
| case b =>
| c.reify(Right[A, B](c.Expr[B](b).eval))
| }
@purefn
purefn / gist:2284779
Created April 2, 2012 16:27
Encoding Product and Coproduct in Scala
type Product[F[_], G[_], A] = (F[A], G[A])
trait Prod[F[_], G[_]] {
type and[H[_]] = Prod[F, ({type λ[α] = Prod[G, H, α]})#λ]
type apply[A] = (F[A], G[A])
}
type product[F[_], G[_]] = Prod[F, G]
type Coproduct[F[_], G[_], A] = Either[F[A], G[A]]
@sjoerdvisscher
sjoerdvisscher / conduits_pipes.hs
Created March 24, 2012 22:58
Bijection between Twan van Laarhoven's Pipe and the data types from Conduit
-- http://www.reddit.com/r/haskell/comments/rbgvz/conduits_vs_pipes_using_void_as_an_input_or/
import Control.Monad
import Data.Void
data Pipe m i o r =
NeedInput (i -> Pipe m i o r) (Pipe m Void o r)
| HaveOutput (Pipe m i o r) (m r) o
| Finished (Maybe i) r
| PipeM (m (Pipe m i o r)) (m r)
@adriaanm
adriaanm / virtpatmat_scala.scala
Created February 24, 2012 11:03
Introducing virtpatmat
trait VirtualizeMatch {
type Pure[T] = T
type Monad[T] = Option[T]
object __match {
def zero: Monad[Nothing] = None
def one[T](x: Pure[T]): Monad[T] = Some(x)
def guard[T](cond: Pure[Boolean], then: => Pure[T]): Monad[T] = if(cond) Some(then) else None
def runOrElse[T, U](in: Pure[T])(matcher: Pure[T] => Monad[U]): Pure[U] = matcher(in) getOrElse (throw new MatchError(in))
def isSuccess[T, U](x: Pure[T])(f: Pure[T] => Monad[U]): Pure[Boolean] = !f(x).isEmpty