Skip to content

Instantly share code, notes, and snippets.

Greg Weber gregwebs

Block or report user

Report or block gregwebs

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@gregwebs
gregwebs / go-2-error-handling-draft-design-issues.md
Last active Sep 13, 2018
A critique of go 2 error handling draft design proposal
View go-2-error-handling-draft-design-issues.md

go 2 has laid out the problem of error handling (Please read first). A wiki page was suggested for feedback. The feedback overwhelmingly suggested ways to not use scoped handlers, essentially creating alternative proposals

It is amazing to see the error handling problem being properly addressed. The existing proposal is good, but I think we can iterate to make it better. I think we can simplify it and address these issues:

  • Defining handlers as a stack is presumptious: it forces handlers to be side-effecting, and ruins interop with defer.
  • return statements are placed in handlers, ruining composition.
  • using check as a prefix function reduces code clarity.
  • a new anonymous function syntax is introduced into the proposal, but only avail
@gregwebs
gregwebs / error-inspection-composition.md
Last active Sep 19, 2018
Go 2 error inspection for error groups.
View error-inspection-composition.md

The current error proposal for inspection is great. I have two minor suggestions:

  • rename Wrapper to Unwrapper
  • change the function name from "As" to "Find"

The words "As" someties indicates casting, but we are just doing a retrieval.

Handling error groups

The current error inspection proposal laments not having a way to deal with error groups.

@gregwebs
gregwebs / go2-error-handling-functions-with-check.md
Last active Oct 3, 2018
Go 2 error handling alternative: handlers as normal functions and an error return?
View go2-error-handling-functions-with-check.md

Its amazing to see the error handling problem being properly addressed. The existing proposal is good, but I think we can iterate to make it better. I think we can simplify it and address these issues:

  • Defining handlers as a stack is presumptious: it forces handlers to be side-effecting, and ruins interop with defer.
  • return statements are placed in handlers, ruining composition.
  • using check as a prefix function reduces code clarity.
  • a new anonymous function syntax is introduced into the proposal, but only available to handle.
  • the proposal introduces two keywords, but one may suffice

It's useful to take a step back and clearly state what we are trying to do:

@gregwebs
gregwebs / StaticRewrite.hs
Last active Oct 13, 2015
WAI static page generator with Yesod example
View StaticRewrite.hs
-- something like this can be used to make friendlier urls for the static pages
-- import Network.Wai.Middleware.Rewrite (rewritePure)
-- rewritePure rewriteConvert
rewriteConvert :: [Text] -> H.RequestHeaders -> [Text]
rewriteConvert pieces _ = staticRewrite pieces
where
staticRewrite :: [Text] -> [Text]
staticRewrite [] = homePage
staticRewrite ("static":"html":_) = homePage -- prevent direct access, not really necessary
staticRewrite route@("pages":_) | ".html" `T.isSuffixOf` last route = staticHtml ++ route
View combine.html.hamlet
$with scripts <- ["Prelude", "Config", "KV"]
^{combineScripts "app" scripts}
@gregwebs
gregwebs / static-pages.hs
Created Feb 6, 2012
Yesod Static File generator.
View static-pages.hs
import Prelude
import Yesod.Routes.Parse (staticPageRoutes)
import Yesod hiding (Request)
import Text.Hamlet
import Network.Wai
import Network.Wai.Test
import Data.Conduit (runResourceT)
import Blaze.ByteString.Builder (toLazyByteString)
import qualified Data.ByteString.Char8 as BS8
import qualified Data.ByteString.Lazy as LBS
@gregwebs
gregwebs / External.hs
Created Nov 5, 2011
addDependentFile
View External.hs
module External where
import Language.Haskell.TH.Syntax
import Language.Haskell.TH.Lib
loadStringFromFile :: Q Exp
loadStringFromFile = do
let externalDependency = "external.txt"
qAddDependentFile externalDependency
s <- qRunIO $ readFile externalDependency
View mongoApp.hs
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
import Yesod
import Database.Persist
import Database.Persist.MongoDB
@gregwebs
gregwebs / pid-wrap
Created Mar 27, 2011
wrapper script that creates a pid file
View pid-wrap
#!/bin/bash
DIR="$( cd "$( dirname "$0" )" && pwd )"
cd $DIR/..
name=`basename "$2"`
case $1 in
'start')
echo $ > ./tmp/$name.pid
exec $2 2>&1 1> ./log/$name.log
View memoized methods
def self.cached_method(meth)
eval <<-CODE
def #{meth}_with_cache
return @#{meth} if @#{meth}
result = #{meth}_without_cache
@#{meth} ||= result
end
CODE
alias_method_chain meth, :cache
end
You can’t perform that action at this time.