Skip to content

Instantly share code, notes, and snippets.

@itang
itang / crystal_macro.cr
Created July 21, 2015 06:11
crystal macro: reflect
class A
def name
"itang"
end
end
macro field(obj, name)
if {{obj}}.responds_to? {{name}}
{{obj}}.{{name.id}}
end
@itang
itang / nil.cr
Last active August 29, 2015 14:23
crystal Nil?
a = "hello"
def get_it(a)
if a
"a"
else
nil
end
end
@itang
itang / migration.sh
Created May 27, 2015 13:42
db migrations
#!/bin/bash
DIR=$1
TARGET=$DIR/package.sql
contains() {
string="$1"
substring="$2"
if test "${string#*$substring}" != "$string"
then
@itang
itang / play-json.scala
Created April 30, 2015 07:53
test with play-json
scala> import play.api.libs.json._
scala> val json = Json.parse(s"""{"a":1,"b":[1,2,3]}""")
json: play.api.libs.json.JsValue = {"a":1,"b":[1,2,3]}
scala> val a = json \ "a"
a: play.api.libs.json.JsLookupResult = JsDefined(1)
scala> a == JsDefined(JsNumber(1))
res27: Boolean = true
a = nil
if true
a = "hello"
end
# puts a.length //undefined method 'length' for Nil
puts a.length if a
crul get http://localhost:9000/echo_json -d '{"hello":"world"}' -H Content-Type:application/json -j
@itang
itang / as_threading.clj
Created March 13, 2015 09:28
Clojure as-> threading
(as-> "hello" name (count name) (str "hello-" name ))
=>
"hello-5"
import play.api.Logger
import play.api.mvc._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
object XRuntimeFilter extends Filter {
def apply(next: (RequestHeader) => Future[Result])(request: RequestHeader): Future[Result] = {
val s = System.nanoTime()
val result = next(request).map { response =>
val e = System.nanoTime()
@itang
itang / play2.3_eclipse_classpath.xml
Created March 13, 2015 03:41
play2.3 + eclipse 视图代码解析
<classpathentry kind="src" path="target/scala-2.11/twirl/main"/>
@itang
itang / dirty_work.cr
Created March 12, 2015 09:11
苦逼之代码生成
weeks = "monday","tuesday","wednesday","thursday", "friday", "saturday", "sunday"
weeks.each do |w|
puts "schedule_#{w}_start schedule_#{w}_end"
end
puts "*" * 100
weeks.each do |w|