Skip to content

Instantly share code, notes, and snippets.

View odd's full-sized avatar
💭
Functioning

Odd Möller odd

💭
Functioning
View GitHub Profile
@johnhungerford
johnhungerford / dependency-injection.md
Last active October 17, 2024 08:16
ZIO-like dependency injection using implicit resolution

ZIO-like dependency injection using implicit resolution

Daniel Ciocîrlan recently published a video showcasing a dependency-injection (DI) approach developed by Martin Odersky that uses Scala's implicit resolution to wire dependencies automatically. (See also his reddit post.)

The basic pattern for defining services in Odersky's approach is as follows:

class Service(using Provider[(Dep1, Dep2, Dep3)])
import org.apache.ivy.core.cache.ArtifactOrigin
import sbt.*
import sbt.Keys.*
import sbt.Project.inConfig
import sbt.internal.RemoteCache.*
import sbt.internal.inc.{HashUtil, JarUtils}
import sbt.internal.librarymanagement.IvyActions.mapArtifacts
import sbt.nio.Keys.*
import sbt.plugins.SemanticdbPlugin
@SethTisue
SethTisue / scala-2.13.0-M5.md
Last active August 28, 2018 02:31
Scala 2.13.0-M5 draft release notes

Scala 2.13 is getting closer and closer!

We've been polishing the improved and simplified Scala collections library that first shipped in 2.13.0-M4. We expect the API to remain stable now, though there may still be minor changes in RC1.

M5 is our feature-freeze release for 2.13. From here forward, we’ll close existing open loops but not embark on or accept new work.

Collections changes

The major collections changes already landed in M4. See the M4 release notes for a summary.

@mads-hartmann
mads-hartmann / anything-dirty.sh
Created March 20, 2018 19:58
🤖 Tiny script to check if I had any dirty repos or un-pushed branches before sending my MBP to repair.
#!/bin/bash
set -euo pipefail
function is-git-repository {
[[ -d "$1/.git" ]] || (cd "$1" && git rev-parse --git-dir > /dev/null 2>&1)
}
function echo-is-dirty {
cd "$1"
if [[ ! -z $(git status -s) ]]
@ case class Cross[T](items: T*){
def flatMap[V](f: T => Cross[V]): Cross[(T, V)] = {
val flattened = for{
i <- items
k <- f(i).items
} yield (i, k)
Cross(flattened:_*)
}
def map[V](f: T => V): Cross[(T, V)] = {
Cross(items.map(i => i -> f(i)):_*)
@DavidDudson
DavidDudson / Opt.scala
Created November 7, 2017 22:04
Allocationless Option type that converts to/from scala.Option and java.Optional
/**
* An Option class, without a few Ion's
*
* Never allocates, uses null instead.
*
* No primitives allowed.
*
* Flatmap is allowed, but nesting is not (no flatten)
*/
object Opt {
@cdelaitre
cdelaitre / docker-compose-tick.yml
Last active March 22, 2021 20:01
Monitor Docker Swarm with the InfluxData TICK Stack
version: '3'
services:
# FRONT
chronograf:
# Full tag list: https://hub.docker.com/r/library/chronograf/tags/
image: chronograf
deploy:
replicas: 1
placement:
constraints:
@jriecken
jriecken / ec.scala
Created March 3, 2017 17:29
Prepare ExecutionContext without prepare method
// Something like this will likely allow propagation of thread-local information
val originalEc: ExecutionContext = ... // the actual execution context
// This will get called every time an implicit EC is necessary, effectively preparing the context
implicit def wrappedEc: ExecutionContext = {
val ctx = ...// capture thread local context here
new ExecutionContext {
override def execute(r: Runnable): Unit = originalEc.execute(new Runnable() {
override def run(): Unit {
val oldCtx = ... // get old context

Applied Functional Programming with Scala - Notes

Copyright © 2016-2018 Fantasyland Institute of Learning. All rights reserved.

1. Mastering Functions

A function is a mapping from one set, called a domain, to another set, called the codomain. A function associates every element in the domain with exactly one element in the codomain. In Scala, both domain and codomain are types.

val square : Int => Int = x => x * x
import com.amazonaws.HttpMethod
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.model._
...
val s3Client = new AmazonS3Client
private def getFlow(pathRaw: String, method: HttpMethod) = {
// clean the path
val path = pathRaw.dropWhile(_ == '/').trim