Skip to content

Instantly share code, notes, and snippets.

withConnection :: (Connection -> IO c) -> ReaderT Config IO c
withConnection f = do
url <- connectionUrl
liftIO $ bracket (connectPostgreSQL url) (close) f
-- this should be pure!
connectionUrl :: ReaderT Config IO B.ByteString
connectionUrl = do
config <- ask
return $ BC.pack (url config)
connectionUrl :: Reader Config B.ByteString
-- withConnection
-- :: (Database.PostgreSQL.Simple.Internal.Connection -> IO c)
-- -> ReaderT Config Data.Functor.Identity.Identity (IO c)
withConnection f = do
url <- connectionUrl
return $ bracket (connectPostgreSQL url) (close) f
@jg
jg / Parser.hs
Last active August 29, 2015 14:03
Applicative Ignore
module AParser where
import Control.Applicative
import Data.Char
import Prelude as P
newtype Parser a = Parser { runParser :: String -> Maybe (a, String) }
satisfy :: (Char -> Bool) -> Parser Char
module.exports = function(grunt) {
grunt.initConfig({
ts: {
dev: { // a particular target
src: ["app/assets/javascripts/**/*.ts"], // The source typescript files, http://gruntjs.com/configuring-tasks#files
html: ["app/assets/templates/**/*.tpl.html"], // The source html files, https://github.com/basarat/grunt-ts#html-2-typescript-support
out: 'public/javascripts/app.js', // If specified, generate an out.js file which is the merged js file
watch: 'app/assets/javascripts', // If specified, watches this directory for changes, and re-runs the current target
reference: 'app/assets/javascripts/references.ts',
options: { // use to override the default options, http://gruntjs.com/configuring-tasks#options
# Clone rbenv into ~/.rbenv
git clone git@github.com:sstephenson/rbenv.git ~/.rbenv
# Add rbenv to your PATH
# NOTE: rbenv is *NOT* compatible with rvm, so you'll need to
# remove rvm from your profile if it's present. (This is because
# rvm overrides the `gem` command.)
echo 'export PATH="$HOME/.rbenv/bin:$HOME/.rbenv/shims:$PATH"' >> ~/.bash_profile
exec $SHELL
@jg
jg / SourcesTable.scala
Last active December 19, 2015 15:39
Scala Slick Type Problem
import scala.slick.driver.PostgresDriver.simple._
import scala.slick.session.{Database => SlickDatabase}
import Database.threadLocalSession
import scala.slick.jdbc.{GetResult, StaticQuery => Q}
import Q.interpolation
import scala.slick.lifted.CanBeQueryCondition
object SourcesTable extends Table[Source]("sources") {
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def name = column[String]("name")
// simple example of the cake pattern
// abstract DAO trait
trait Repository[A, B]{
// saves an entity, returns an ID
def save(entity: A): B
// more features..
}
trait RdbmsRepository extends Repository[MyUserCaseClass, Long]{
@jg
jg / typeclass.scala
Last active December 15, 2015 20:29
object TypeSerializers {
implicit object LongPropertySerializer extends PropertySerializer[Long] {
def sqlType = "long"
}
implicit object StringPropertySerializer extends PropertySerializer[String] {
def sqlType = "string"
}
}
@jg
jg / logcat.scala
Created March 30, 2013 09:09
java.lang.NoSuchMethodError: scala.collection.immutable.StringLike.toString
E/AndroidRuntime(30638): FATAL EXCEPTION: main
E/AndroidRuntime(30638): java.lang.NoSuchMethodError: scala.collection.immutable.StringLike.toString
E/AndroidRuntime(30638): at scala.collection.immutable.StringLike$class.toLong(StringLike.scala:230)
E/AndroidRuntime(30638): at scala.collection.immutable.StringOps.toLong(StringOps.scala:31)
E/AndroidRuntime(30638): at com.android.todoapp.Task$$anonfun$deserialize$1.apply(Task.scala:162)
E/AndroidRuntime(30638): at com.android.todoapp.Task$$anonfun$deserialize$1.apply(Task.scala:160)
E/AndroidRuntime(30638): at scala.collection.immutable.List.foreach(List.scala:309)
E/AndroidRuntime(30638): at com.android.todoapp.Task.deserialize(Task.scala:160)
E/AndroidRuntime(30638): at com.android.todoapp.Task$.deserialize(Task.scala:41)
E/AndroidRuntime(30638): at com.android.todoapp.Tasks$$anonfun$getTasks$1.apply(Tasks.scala:36)