Skip to content

Instantly share code, notes, and snippets.

View superChing's full-sized avatar
🤒
Out sick

林煒清(Lin Wei-Ching) superChing

🤒
Out sick
  • Taiwan
View GitHub Profile
@retronym
retronym / generalised-type-constraints.scala
Created November 8, 2009 08:02
Demo of generalised type constraints in Scala 2.8
// scala 2.7 simple type constraint. This can only constrain a type parameter of this function.
// Below, in ListW.sumint28, we can't use this approach because we want to constrain T,
// a type param of the enclosing trait.
def sumint27A[T <: Int](l: List[T]) : Int = l.reduceLeft((a: Int, b: Int) => a + b)
trait IntLike[X] extends (X => Int)
object IntLike {
implicit val intIntLike: IntLike[Int] = new IntLike[Int] { def apply(x: Int) = identity(x) }
}
@arnolddevos
arnolddevos / gist:574873
Created September 11, 2010 05:36
Idea for generator and suspendable wrapper
import scala.util.continuations._
class Generator[A] extends Iterator[A] with (A => Unit @ suspendable) {
private var a: Option[A] = None
private var k: Option[Unit => Unit] = None
def next = {
val a0 = a.get
val k0 = k.get
a = None
@sebfisch
sebfisch / gist:931419
Created April 20, 2011 14:10
Type Safe Interpreter for Simple Functional Language in Haskell
This literate Haskell program implements a type safe interpreter for a
simple functional language. Such an interpreter is the "hello world"
example for dependently typed languages and this program mimics it in
Haskell. Generalized algebraic data types and type families are
sufficient.
> {-# LANGUAGE GADTs #-}
> {-# LANGUAGE TypeFamilies #-}
> {-# LANGUAGE TypeOperators #-}
> {-# LANGUAGE MultiParamTypeClasses #-}
@kencoba
kencoba / Decorator.scala
Created February 21, 2012 11:27
Decorator pattern (Design Patterns in Scala)
// http://en.wikipedia.org/wiki/Decorator_pattern
trait Coffee {
def cost:Double
def ingredients: String
}
class SimpleCoffee extends Coffee {
override def cost = 1
override def ingredients = "Coffee"
@loicdescotte
loicdescotte / Forcomptran.md
Last active May 27, 2023 06:27
Scala for comprehension translation helper

Scala for comprehension translation helper

"For comprehension" is a another syntaxe to use map, flatMap and withFilter (or filter) methods.

yield keyword is used to aggregate values in the resulting structure.

This composition can be used on any type implementing this methods, like List, Option, Future...

@helena
helena / TrySamples.scala
Last active July 13, 2021 15:03
Try introduced in Scala 2.10.0
// Start by adding these
import scala.util.{ Try, Success, Failure }
/**
* Sample runs code that raises the exception:
* java.lang.ArithmeticException: / by zero
*/
object Sample {
def main(args: Array[String]): Unit = {
/**
* "Select" off the first future to be satisfied. Return this as a
* result, with the remainder of the Futures as a sequence.
*
* @param fs a scala.collection.Seq
*/
def select[A](fs: Seq[Future[A]])(implicit ec: ExecutionContext): Future[(Try[A], Seq[Future[A]])] = {
@tailrec
def stripe(p: Promise[(Try[A], Seq[Future[A]])],
heads: Seq[Future[A]],
@bootandy
bootandy / python_logging.yaml
Last active August 26, 2021 07:14
Sample logging config file for python logging module
# use with: logging.config.dictConfig(yaml.load(open('logging.yaml', 'r')))
# Formatters detailed here: http://docs.python.org/2/library/logging.html#logrecord-attributes
version: 1
formatters:
simple:
format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
detail:
format: '%(asctime)s - %(levelname)s - File: %(filename)s - %(funcName)s() - Line: %(lineno)d - %(message)s'
@cobusc
cobusc / postgresql_date_function_index_howto.md
Last active July 26, 2024 09:16
Short explanation of the issues faced when trying to create a PostgreSQL index using the date() function and how to resolve it.
@markSci5
markSci5 / Git checkout remote branch
Created July 3, 2013 07:08
Git checkout remote branch
//To fetch a branch, you simply need to:
git fetch origin
//This will fetch all of the remote branches for you. With the remote branches
//in hand, you now need to check out the branch you are interested in, giving
//you a local working copy:
git checkout -b test origin/test