Skip to content

Instantly share code, notes, and snippets.

View cvogt's full-sized avatar

Jan Christopher Vogt cvogt

  • Symbiont.io
View GitHub Profile
@cvogt
cvogt / gist:8672a3fd97bc97714822
Last active August 8, 2023 13:55
Default values for type parameters in Scala
scala> :paste
class :=[T,Q]
trait Default_:={
/** Ignore default */
implicit def useProvided[Provided,Default] = new :=[Provided,Default]
}
object := extends Default_:={
/** Infer type argument to default */
implicit def useDefault[Default] = new :=[Default,Default]
}
@cvogt
cvogt / gist:9193220
Last active February 13, 2022 13:50 — forked from ruescasd/gist:7911033
Slick: Dynamic query conditions using the **MaybeFilter** (Updated to support nullable columns)
import scala.slick.lifted.CanBeQueryCondition
// optionally filter on a column with a supplied predicate
case class MaybeFilter[X, Y](val query: scala.slick.lifted.Query[X, Y]) {
def filter[T,R:CanBeQueryCondition](data: Option[T])(f: T => X => R) = {
data.map(v => MaybeFilter(query.filter(f(v)))).getOrElse(this)
}
}
// example use case
import java.sql.Date
@cvogt
cvogt / pre-commit-hook-install.sh
Created June 16, 2015 19:27
git pre-commit hook for Scala compile and format checking (put both in your git project root, needs to be installed in each clone)
#!/bin/sh
cd "$(dirname "$0")"
touch .git/hooks/pre-commit
rm .git/hooks/pre-commit
ln -s ../../pre-commit-hook.sh .git/hooks/pre-commit
@cvogt
cvogt / gist:9239494
Last active September 9, 2019 01:30
Slick app architecture cheat sheet
// Please comment in case of typos or bugs
import scala.slick.driver.H2Driver._
val db = Database.for...(...)
case class Record( ... )
class Records(tag: Tag) extends Table[Record](tag,"RECORDS"){
...
def * = ... <> (Record.tupled,Record.unapply)
// place additional methods here which return values of type Column
@cvogt
cvogt / gist:0b681c6edfce504ca3553b336ca55ef7
Created January 9, 2019 19:56 — forked from jfeilbach/gist:fd109c7dbc9798ce6e47358b82d0be76
DNS over TLS (knot resolver) setup using 1.1.1.1 on macOS
# Configuring DNS-over-TLS on macOS
# Worked on macOS 10.13.4
brew -v update
brew -v doctor
# Next two commands are optional
sudo chown -R $(whoami) $(brew --prefix)/*
echo 'export PATH="/usr/local/sbin:$PATH"' >> ~/.bash_profile
# Install DNS client
brew install knot-resolver
# Should be installed to something like: /usr/local/Cellar/knot-resolver/2.3.0/sbin/kresd
@cvogt
cvogt / vimeo-download.py
Last active December 26, 2018 08:29 — forked from alexeygrigorev/vimeo-download.py
Downloading segmented video from vimeo
#!/usr/bin/env python2.7
# https://gist.github.com/cvogt/1e46597e39bfe0e0aee8d5348123ca03
# based on https://gist.github.com/alexeygrigorev/a1bc540925054b71e1a7268e50ad55cd#file-vimeo-download-py
# also see https://github.com/eMBee/vimeo-download
import requests
import base64
import sys
from tqdm import tqdm

Getting Started in Scala

This is my attempt to give Scala newcomers a quick-and-easy rundown to the prerequisite steps they need to a) try Scala, and b) get a standard project up and running on their machine. I'm not going to talk about the language at all; there are plenty of better resources a google search away. This is just focused on the prerequisite tooling and machine setup. I will not be assuming you have any background in JVM languages. So if you're coming from Python, Ruby, JavaScript, Haskell, or anywhere…  I hope to present the information you need without assuming anything.

Disclaimer It has been over a decade since I was new to Scala, and when I was new to Scala, I was coming from a Java and Ruby background. This has probably caused me to unknowingly make some assumptions. Please feel free to call me out in comments/tweets!

One assumption I'm knowingly making is that you're on a Unix-like platform. Sorry, Windows users.

Getting the JVM

@cvogt
cvogt / gist:d9049c63fc395654c4b4
Created May 17, 2014 20:04
This code implements a facility for Slick 2.0 to inject custom SQL instead of the Slick produced SQL to be used when running a query
import scala.slick.lifted.{TableQuery => _}
import scala.slick.ast._
import scala.slick.driver._
import scala.language.implicitConversions
/** Extends QueryInvoker to allow overriding used SQL statement when executing a query */
trait OverridingInvoker extends JdbcDriver{
// get the extended QueryInvoker into the .simple._ implicits
override val Implicit: Implicits = new Implicits
override val simple: Implicits with SimpleQL = new Implicits with SimpleQL
@cvogt
cvogt / foo.scala
Created February 1, 2018 18:43
versioned serializers in scala
case class Foo( bar: Bar )
case class Bar( bar: Baz )
case class Baz( bar: Booyah )
case class Booyah( i: Int, s: String )
object V1Serializers{
implicit def FormatFoo[Foo] = ...
implicit def FormatBar[Bar] = ...
implicit def FormatBaz[Baz] = ...
implicit def FormatBooyah[Booyah] = ...
package com.pretend.myforklift
import com.liyaos.forklift.slick.{ SlickCodegen, SlickMigrationCommandLineTool, SlickMigrationCommands, SlickMigrationManager }
import scala.io.StdIn
trait MyMigrationCommands extends SlickMigrationCommands
with MyMigrationFilesHandler {
this: SlickMigrationManager with SlickCodegen =>