Skip to content

Instantly share code, notes, and snippets.

@uehaj
uehaj / a.groovy
Created September 18, 2013 19:41
@GrabResolver(name="maven-repo", root="https://raw.github.com/uehaj/maven-repo/gh-pages/snapshot")
@Grab("groovyx.ast.bytecode:groovy-bytecode-ast:0.2.0-separate-asm")
import groovyx.ast.bytecode.Bytecode
import java.lang.invoke.*
import java.lang.invoke.MethodHandles.Lookup
import static groovyjarjarasm.asm.Opcodes.H_INVOKESTATIC
class Brainfuck {
// Bootstrapメソッド
@uehaj
uehaj / compile.groovy
Created September 18, 2013 19:17
Brainfuck compiler for JVM using indy(invokedynamic)
// ネストしたループにおけるラベル名を管理するためのデータとコード。
def list = [].withDefault{0}
def nestLevel = 0
def level = { it << list[0..nestLevel-1].join('_') }.asWritable()
def increaseLevel = { list[nestLevel++]++; level(it) }.asWritable()
def decreaseLevel = { level(it); nestLevel-- }.asWritable()
// Brainfuck命令をJVMバイトコードにコンバートする
def genCode = {
new File(args[0]).text.replaceAll(/[^\+\-\<\>\.\,\[\]]/, '').eachMatch(/[^\[\]]+|[\[\]]/) { g0 ->
@uehaj
uehaj / shortest.fr
Created September 16, 2013 02:24
第13回オフラインリアルタイムどう書くの参考問題をFregeで解く ref: http://qiita.com/uehaj/items/944cd9903c3827f1d1bc
shortestPath' :: Int -> Int -> Int
shortestPath' n maxn
| n==0 = 0
| n==1 = 1
| n > maxn = maxBound::Int
| even(n) = (shortestPath' (n `div` 2) (maxn-2)) + 1
| otherwise = minimum [(shortestPath' (n-1) (maxn-2)) + 1, (shortestPath' (n+1) (maxn-2)) + 1]
shortestPath :: Int -> Int
shortestPath n = shortestPath' n (n*2)
import org.codehaus.groovy.runtime.*
assert String.metaClass instanceof HandleMetaClass
String.metaClass.hello = {println "hello"}
"hoge".hello()
assert String.metaClass instanceof ExpandoMetaClass
@uehaj
uehaj / bench.groovy
Created May 8, 2013 04:29
CompileStatic benchmark
@Grab('org.gperfutils:gbench:0.4.2-groovy-2.1')
import groovy.transform.*
@CompileStatic
class BusStatic {
int ceil10(int n) { (Math.ceil(n/10) * 10) as Integer }
int half(int n) { ceil10(n/2) }
List parse(String input) {
@uehaj
uehaj / static typed markup builder
Created January 18, 2013 08:44
@DelegatesToを使った静的型チェックされるMarkupBuilderのようなものです。
import groovy.transform.*
class HtmlBuilder {
def html(@DelegatesTo(Html) Closure c) {
c.delegate = new Html()
println "<html>"; c(); println "</html>"
}
}
class Html {
def head(@DelegatesTo(Head) Closure c) {
c.delegate = new Head()
/*
* Copyright 2004-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@uehaj
uehaj / flymake-groovy
Created October 28, 2012 03:14
Flymake mode for groovy.
;; flymake for groovy
;; setup:
;;(when (require 'flymake-groovy nil t)
;; (add-hook 'groovy-mode-hook
;; (lambda () (define-key groovy-mode-map "\C-ce" 'next-flymake-error))))
(require 'flymake)
(defvar groovyc-command
(expand-file-name (concat (getenv "GROOVY_HOME") "/bin/groovyc")))
println "hello world"
@uehaj
uehaj / groovy--with-stc.groovy
Created March 28, 2012 10:12
groovy script invoker with statically type check without annotation.
#!/usr/bin/env groovy # -*-groovy-*-
import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer
import org.codehaus.groovy.control.CompilerConfiguration
import groovy.transform.TypeChecked
def configuration = new CompilerConfiguration()
configuration.addCompilationCustomizers(new ASTTransformationCustomizer(TypeChecked))
def binding = new Binding()
binding.setVariable("args", args.tail())