Skip to content

Instantly share code, notes, and snippets.

View zhangxu's full-sized avatar
🏠
Working from home

ZhangXu zhangxu

🏠
Working from home
View GitHub Profile
@zhangxu
zhangxu / scalaz-validation
Last active August 29, 2015 14:00
Option to Scalaz Validation
#!/usr/bin/env scalas
/***
scalaVersion := "2.11.0"
scalacOptions += "-language:_"
libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.0.6"
*/
@zhangxu
zhangxu / tmux everyday usage
Last active October 27, 2017 10:09
Tmux usage
default prefix: Ctrl+b
start new session: tmux
list sessions: tmux ls
attach to session: tmux attach [-t <session>]
attach to session forcely: tmux attach -d
detach: prefix then d
Windows:
@zhangxu
zhangxu / reader-config.scala
Last active August 29, 2015 14:01
Scalaz Reader of Configuration
import scalaz.Reader
case class Configuration(hostname: String, port: Int, file: String)
case class ServerConnection(host: String, port: Int, file: String)
val config = Configuration("localhost", 8080, "index.html")
def configReader[A](f: Configuration => A): Reader[Configuration, A] = Reader { f }
val conn: scalaz.Kleisli[scalaz.Id.Id, Configuration, ServerConnection] = for {
@zhangxu
zhangxu / ruby-monkey-patch
Last active August 29, 2015 14:02
Ruby Monkey Patch
class String
def say_hello
"hello #{self}!"
end
end
puts "world".say_hello
@zhangxu
zhangxu / validation-and-for-comp
Last active August 29, 2015 14:02
Scalaz Validation and For-comprehension
#!/usr/bin/env scalas
/***
scalaVersion := "2.11.0"
scalacOptions += "-language:_"
libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.0.6"
*/
@zhangxu
zhangxu / activerecord-raw-sql
Created August 12, 2014 03:13
ActiveRecord Raw SQL
@results = []
ActiveRecord::Base.connection.select_all(
ActiveRecord::Base.send(:sanitize_sql_array,
["select * from events where area_type = ? and area_id = ?", 'parcel', '1'])
).each do |record|
# instead of an array of hashes, you could put in a custom object with attributes
@results << {id: record["id"], category: record["category"]}
end
@zhangxu
zhangxu / postgres-with-clause
Last active August 29, 2015 14:05
PostgreSQL `With` Clause
with parcel_ids as (select id from parcels where ranch_id = 57),
zone_ids as (select id from zones where parcel_id in (select id from parcel_ids))
select extract(month from date) as month, category, sub_category, count(*) from events where (area_type = 'parcel' and area_id in (select id from parcel_ids)) or (area_type = 'zone' and area_id in (select id from zone_ids))
group by extract(month from date), category, sub_category;
@zhangxu
zhangxu / postgis
Last active August 29, 2015 14:06
PostGIS functions
\set p1 '\'' 'POLYGON((0 0, 0 3, 3 3, 3 0, 0 0))' '\''
\set p2 '\'' 'POLYGON((1 1, 1 2, 2 2, 2 1, 1 1))' '\''
\set p3 '\'' 'POLYGON((-1 -1, -1 1, 1 1, 1 -1, -1 -1))' '\''
\set p4 '\'' 'POLYGON((-2 0, -1 0, -1 -1, -2 -1, -2 0))' '\''
\set p5 '\'' 'POLYGON((0 0, 0 3, 1.5 3.5, 3 3, 3 0, 0 0))' '\''
\set p6 '\'' 'POLYGON((9473678.20883554 1181106.57122879,9473678.20915479 1181106.57199719,9473678.21065819 1181106.57712958,9473678.21207348 1181106.58209349,9473678.2081223 1181106.58868023,9473678.20795089 1181106.58826838,9473678.20606889 1181106.57622375,9473678.20883554 1181106.57122879))' '\''
\set p7 '\'' 'SRID=3857;POLYGON ((-121.7917486242956 36.87756646920572, -121.7896991290737 36.87551268040243, -121.7864477199712 36.87768243547166, -121.7884120600693 36.87976431594564, -121.7917486242956 36.87756646920572))' '\''
@zhangxu
zhangxu / gist:427fa6fa18a69eec7043
Created September 24, 2014 05:03
RoR native sql
def monthly_count_of_ranch_fields(ranch_id, start_date, end_date)
sql ="with parcel_ids as (select id from parcels where ranch_id = ?),
zone_ids as (select id, parcel_id from zones where parcel_id in (select id from parcel_ids)),
parcel_events as (select date, category, sub_category, area_id as parcel_id from events where area_type = 'parcel' and area_id in (select id from parcel_ids)),
zone_events as (select e.date, e.category, e.sub_category, z.parcel_id from events e, (select id, parcel_id from zone_ids) z where e.area_id = z.id and e.area_type = 'zone'),
all_events as (select * from parcel_events union select * from zone_events)
select extract(month from date) as month, category, sub_category, parcel_id, count(*) from all_events where date >= ? and date <= ?
group by extract(month from date), category, sub_category, parcel_id"
field_event_summaries = Hash.new
@zhangxu
zhangxu / forward-message.scala
Last active August 29, 2015 14:21
Forward message
import akka.actor._
import akka.pattern.ask
import akka.util.Timeout
import scala.concurrent.ExecutionContext._
import scala.concurrent.duration._
import Implicits.global
implicit val timeout = Timeout(30.seconds)
val system = ActorSystem("system")