Skip to content

Instantly share code, notes, and snippets.

View arjanblokzijl's full-sized avatar

Arjan Blokzijl arjanblokzijl

  • Zürich, Switzerland
View GitHub Profile
@arjanblokzijl
arjanblokzijl / AutoDiff.scala
Created January 11, 2020 07:37 — forked from TiarkRompf/AutoDiff.scala
Reverse-mode automatic differentiation
package autodiff
import org.scalatest._
import org.scalatest.Assertions._
import scala.collection.mutable._
class AutoDiffSpec extends FunSuite {
// computation graph (list of nodes)
@arjanblokzijl
arjanblokzijl / CanBuildFrom.md
Created December 18, 2018 06:22 — forked from soronpo/CanBuildFrom.md
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).

@arjanblokzijl
arjanblokzijl / ParallelFoldMonoid.scala
Created July 9, 2018 08:38 — forked from SystemFw/Test.scala
Parallel foldMonoid with fs2
object Test {
import cats.implicits._
import cats.kernel.CommutativeMonoid
import cats.effect._
import fs2._
import scala.concurrent.ExecutionContext
// same as parallelFoldMonoid, but with some logging to show the parallelism
def loggedParallelFoldMonoid[F[_], A: CommutativeMonoid](n: Int)(
input: Stream[F, A])(implicit F: Effect[F], ec: ExecutionContext) =
@arjanblokzijl
arjanblokzijl / example.scala
Created July 5, 2018 07:49 — forked from SystemFw/example.scala
Running fs2 streams in parallel and collect their result in sequence, with queues
object Example {
import cats._, implicits._
import cats.effect._
import fs2._
import scala.concurrent.ExecutionContext
// start N streams concurrently, and stream their results in order
// e.g. download a file from a server in N parts concurrently, and stream it
// this approach is good for showcasing usage of concurrent abstractions,
// but a lower level implementation using Pull is likely to be much more performant
import fs2._
object OrderedJoin {
/**
* Joins two streams, that are assumed to be sorted, and keeps the ordering intact.
* For example:
* join(Stream(1,1,2,4,4,5), Stream(1,1,2,3,4) will return Stream(1,1,1,1,2,2,3,4,4,4,5)
*
* @param left the left stream
@arjanblokzijl
arjanblokzijl / gist:7feee095093d3229d824
Created July 9, 2015 11:17
parallel call to futures
package test
import annotation.tailrec
object ConcurrentTest extends App {
import concurrent._, duration._, ExecutionContext.Implicits.global
def banner(str: String) = {
val line = "=" * 80
@arjanblokzijl
arjanblokzijl / gist:2651683
Created May 10, 2012 07:29 — forked from xeno-by/gist:2559714
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.
===Common_1.scala===
@arjanblokzijl
arjanblokzijl / hashmap-bench.scala
Created April 10, 2012 06:07 — forked from erikrozendaal/hashmap-bench.scala
Scala TreeMap benchmarks
import collection.mutable.ArrayBuffer
import collection.immutable.HashMap
object HashMapTest {
val random = new util.Random(1234)
def time(block: => Unit): Double = {
val start = System.nanoTime()
block
@arjanblokzijl
arjanblokzijl / conduits_pipes.hs
Created March 28, 2012 07:35 — forked from sjoerdvisscher/conduits_pipes.hs
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)
@arjanblokzijl
arjanblokzijl / fix-sbt-compiler-interface.sh
Created March 6, 2012 06:15 — forked from gkossakowski/fix-sbt-compiler-interface.sh
Fix sbt's compiler interface for 2.10.0-M1
#!/usr/bin/env bash
# This is very hacky remedy to following error people using sbt
# and 2.10.0-M1 release of Scala are going to see:
#
# API.scala:261: error: type mismatch;
# found : API.this.global.tpnme.NameType
# (which expands to) API.this.global.TypeName
# required: String
# sym.isLocalClass || sym.isAnonymousClass || sym.fullName.endsWith(LocalChild)
#