Skip to content

Instantly share code, notes, and snippets.

@petekneller
petekneller / NoArgMethods.scala
Last active December 30, 2015 08:39
Some exploration of the problem Dan Bodart found and discussed in http://dan.bodar.com/2013/12/04/wat-scala/
package gotchas
object NoArgMethods {
val l = List(1, 2, 3)
val correct = l.toSet // has type Set[Int]
val wrong = l.toSet() // has type Boolean
// toSet() is converted to toSet.apply()
@petekneller
petekneller / gist:10239661
Created April 9, 2014 08:10
Multipart upload using dispatch-mime lib
import dispatch.classic./\
import dispatch.classic.mime.Mime.MimeRequestTerms
val multipart = new MimeRequestTerms(/\) <<*("counterparty-file", anyString(), bytes)
val byteStream = new ByteArrayOutputStream()
multipart.body.get.writeTo(byteStream)
Request(url, RequestMethods.POST,
Map(HttpHeaders.ContentType.value -> multipart.body.get.getContentType.getValue),
byteStream.toByteArray)
@petekneller
petekneller / JsonParser
Created June 10, 2014 14:16
Example JSON parser using Scala parser combinator
/*
Thx to Albert Latacz for passing this on to me
Think its more or less a direct lift from the Artima Programming in Scala book, chapter 33
at http://booksites.artima.com/programming_in_scala_2ed/examples/html/ch33.html
*/
import java.io.InputStreamReader
import pure.commons.store.FileUtils
import scala.util.parsing.combinator._
import scala.util.parsing.input.StreamReader
@petekneller
petekneller / gist:5b117039d2292e7c8977
Last active September 14, 2015 10:06
SBT BuildInfo example 1 - output in /target
object BuildInfo {
lazy val settings = sourceGenerators in Test <+= (streams, sourceManaged in Test, baseDirectory) map emitBuildInfo
def emitBuildInfo(logger: TaskStreams, outputDir: File, baseDir: File): Seq[File] = {
val outputFile = outputDir / "BuildInfo.scala"
logger.log.info(s"Generating build info file at: ${outputFile}")
IO.write(outputFile,
s"""
/*
* Copyright Christopher Schmidt 2010
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
class A
class A2 extends A
class B
trait M[X]
//
// Upper Type Bound
//
def upperTypeBound[AA <: A](x: AA): A = x
/** GADTs in Scala and their limitations */
/** Background: what is an algebraic data type (ADT) ?
* ADT: (possibly) recursive datatype with sums and products
* In scala - a trait with case classes (case class is product, subtyping is sum)
*/
/** Motivation: untyped embedded DSL doesn't prevent nonsensical expressions */
sealed trait Expr {
def apply(other: Expr) = Ap(this, other)
// No case classes
object Consoles {
trait ConsoleAlg[F[_]] {
def readLine: F[Option[String]]
def printLine(line: String): F[Unit]
}
trait Console[+A] {
import scalaz.effect.IO
import scalaz.{EitherT, Monad}
object Program extends ProgramInstances {
def apply[A](a: A): Program[A] = Program[A](EitherT.right[IO, Throwable, A](IO(a)))
}
trait ProgramInstances {
implicit object ProgramMonad extends Monad[Program] {
override def point[A](a: => A): Program[A] = Program[A](a)
package demo
import scalaz._
import scalaz.syntax._
import scalaz.effect.IO
import scalaz.effect.IO._
import scalaz.syntax.std.either._
object Demo {
type M[A] = EitherT[IO, Throwable, A]