This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]] { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |