Skip to content

Instantly share code, notes, and snippets.

haskell> :set -XGeneralizedNewtypeDeriving
haskell> newtype Possibilities a = AnyOf { possibilities :: [a] } deriving (Functor, Applicative, Monad, Eq, Show)
haskell>
haskell> flipACoin = AnyOf ["head", "tail"]
haskell> throwADice = AnyOf [1, 2, 3, 4, 5, 6]
haskell>
haskell> import Control.Applicative
haskell>
haskell> (,) <$> flipACoin <*> throwADice
AnyOf {possibilities = [("head",1),("head",2),("head",3),("head",4),("head",5),("head",6),("tail",1),("tail",2),("tail",3),("tail",4),("tail",5),("tail",6)]}
@missingfaktor
missingfaktor / sbt.sh
Created September 21, 2018 11:44 — forked from alexandru/sbt.sh
#!/usr/bin/env bash
#
# Script that detects if Scala's SBT is installed and if
# not then it automatically downloads and installs it at
# a specified path.
#
# Author: Alexandru Nedelcu (https://alexn.org)
#
set -e
import shapeless.Lub
sealed trait EarlyExitableEither[+F, +D, +C] extends Product with Serializable {
import EarlyExitableEither._
final def map[C1](f: C => C1): EarlyExitableEither[F, D, C1] = {
this match {
case Continue(value) => Continue(f(value))
case Done(value) => Done(value)
case Fail(error) => Fail(error)
object ShapelessMonoid {
import shapeless._
import shapeless.syntax._
import shapeless.ops.hlist.IsHCons
implicit def monoidHNil: Monoid[HNil] = new Monoid[HNil] {
def empty: HNil = HNil
def combine(x: HNil, y: HNil): HNil = HNil
}
implicit class RichQuery[E, U, C[_]](val underlying: Query[E, U, C]) {
/**
* To group by elements by some columns, say "column1" and "column2", and then pick latest in each group, you
* would normally "ORDER BY column1, column2, created_at DESC" and then "DISTINCT ON (column1, column2)".
* However Slick currently does not support DISTINCT ON. There are a number of alternatives that can be used to
* circumvent this limitation. The following function uses "sub-select method" described here:
* https://robots.thoughtbot.com/ordering-within-a-sql-group-by-clause#sub-select-method
*
* This function only deals with arity 2. You will need to define variations of it for different arities. We
* attempted abstracting over arity but could not get it working.
import scala.collection.generic.CanBuildFrom
import scala.collection.mutable.ArrayBuffer
import scala.collection.{IndexedSeqLike, mutable}
/**
* An ordered collection type that ensures it always contains unique elements.
*/
final class UniqueSeq[+A] private(_input: IndexedSeq[A])
extends IndexedSeq[A]
with IndexedSeqLike[A, UniqueSeq[A]] {
@missingfaktor
missingfaktor / gist:ecce566c54e6ad6e55b6d0656afdef74
Created April 16, 2018 22:29 — forked from cvogt/gist:9193220
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
@missingfaktor
missingfaktor / Remove all git tags
Created April 13, 2018 00:09 — forked from okunishinishi/Remove all git tags
Delete all git remote tags
#Delete local tags.
git tag -l | xargs git tag -d
#Fetch remote tags.
git fetch
#Delete remote tags.
git tag -l | xargs -n 1 git push --delete origin
#Delete local tasg.
git tag -l | xargs git tag -d
@missingfaktor
missingfaktor / .configure_capybara.rb
Created February 8, 2018 23:40
Running Capybara tests with Chrome, both locally and on Travis
require 'selenium-webdriver'
require 'capybara/rspec'
require 'capybara/dsl'
require 'capybara-screenshot/rspec'
Capybara.register_driver :chrome do |app|
options = Selenium::WebDriver::Chrome::Options.new
options.add_preference :download, {
prompt_for_download: false,
default_directory: 'tmp/downloads'
akar.try-out=> (macroexpand-1 '(match n
(:and [(!div-by 3)][(!div-by 5)]) "Fizzbuzz"
[(!div-by 3)] "Fizz"
[(!div-by 5)] "Buzz"
:_ n))
(akar.primitives/match* n
(akar.primitives/or-else
(akar.primitives/clause* (akar.combinators/!and (!div-by 3) (!div-by 5)) (clojure.core/fn [] "Fizzbuzz"))
(akar.primitives/clause* (!div-by 3) (clojure.core/fn [] "Fizz"))