Skip to content

Instantly share code, notes, and snippets.

View sighmin's full-sized avatar
💛
grateful

Simon van Dyk sighmin

💛
grateful
View GitHub Profile
@sighmin
sighmin / Maps Go Tour Exercise
Created February 23, 2014 17:27
Go tour "WordCount" method: http://tour.golang.org
package main
import (
"code.google.com/p/go-tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
tokens := strings.Fields(s)
hashmap := make(map[string] int)
@sighmin
sighmin / Closure Go Tour Exercise
Last active August 29, 2015 13:56
Go tour "Fibonacci closure factory" method: http://tour.golang.org
package main
import "fmt"
// Fibonacci closure factory
func fibonacci() func() int {
previous := 0
current := 1
return func() int {
package main
import (
"fmt"
"math/cmplx"
)
func Cbrt(x complex128) complex128 {
result := complex128(x)
for i := 0; i < 100; i++ { // To ensure high precision
@sighmin
sighmin / Go hello world http server
Created April 14, 2015 05:53
Go hello world http server
package main
import (
"fmt"
"log"
"net/http"
)
type Hello struct{}
@sighmin
sighmin / checklist.rb
Created June 4, 2015 14:17
Fiddling with the shockingly documented AREL library to produce a join using an aggregate SQL function (max) in the hopes I'd be able to chain it. ALAS! Only after 2 hours did I realize this ain't gonna works yo.
class Checklist < ActiveRecord::Base
scope :current, -> {
c = Arel::Table.new(:checklists, as: 'c')
r = Arel::Table.new(:checklists, as: 'r')
sql = c.project(Arel.star)
.join(r.project(r[:version_id], r[:version_no].maximum.as('vno'))
.group(r[:version_id]).as('r'))
.on(r[:version_id].eq(c[:version_id])
.and(r[:vno].eq(c[:version_no])))
find_by_sql(sql)
class User < PersistentDataStore::Base
attr_accessor :first_name, :last_name
end
class UserView < SimpleDelegator
def fullname
"#{first_name} #{last_name}"
end
end
class UserView < SimpleDelegator
def initialize(delegate, salutation: 'Padawan')
super(delegate)
@salutation = salutation
end
def fullname
"#{salutation} #{first_name} #{last_name}"
end
module ViewHelpers
def urls
Rails.application.routes.url_helpers
end
def helpers
ActionController::Base.helpers
end
end
module ViewHelpers
extend ActiveSupport::Concern
included do
def default_url_options
ActionMailer::Base.default_url_options
end
end
...
end
class DashboardView
def initialize(current_user, users, featured_posts)
@current_user = current_user
@users = users.map { |u| UserView.new(u) }
@featured_posts = featured_posts
end
attr_reader :current_user, :users, :featured_posts
end
# Instantiate this in your controller layer and pass it to the view as the only "source of truth" for the data on the page