Skip to content

Instantly share code, notes, and snippets.

@clairvy
Created February 17, 2010 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clairvy/306300 to your computer and use it in GitHub Desktop.
Save clairvy/306300 to your computer and use it in GitHub Desktop.
*.class
*.jar
.clojure

役に立たない情報

Java のリフレクション

  • java.lang.reflect パッケージ
  • 型 java.lang.Class
  • Object.getClass()
  • Class.forName(String)
  • Class.getName
  • getConstructors()
  • getFields()
  • getMethods()
  • getClasses()
  • 型 java.lang.reflect.Method
  • 型 java.lang.reflect.Field
  • Class プリミティブのラッパークラス.TYPE

Scala

  • getClass()

Clojure

  • doc [doc]

  • find-doc [re-string-or-pattern]

  • clojure.contrib.repl-utils/show [x] [x selector] r- class [x]

  • ancestors

  • instance?

test するときに contrib のものを使っているけど, カレントディレクトリの.clojure に指定している. この辺は,clj ファイルを読むこと.

参考

// proc :: (a -> a) -> a -> a
// (a -> a) is F (interface)
// proc F :: a -> a is F
// (a) is T (anonymous type)
abstract public class Church {
abstract public <T> F<T> proc(F<T> f);
public static Church ZERO = new Church() {
public <T> F<T> proc(F<T> f) {
return new F<T>() {
public T proc(T x) {
return x;
}
};
}
};
public Church inc() {
return Church.inc(this);
}
public Church plus(final Church n) {
return Church.plus(this, n);
}
public static Church inc(final Church n) {
return new Church() {
public <T> F<T> proc(final F<T> f) {
return new F<T>() {
public T proc(T x) {
return f.proc(n.proc(f).proc(x));
}
};
}
};
}
public static Church plus(final Church m, final Church n) {
return new Church() {
public <T> F<T> proc(final F<T> f) {
return new F<T>() {
public T proc(T x) {
return n.proc(f).proc(m.proc(f).proc(x));
}
};
}
};
}
}
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.runner.JUnitCore;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
public class ChurchTest {
private Church zero = null;
private Church one = null;
private Church two = null;
private Church three = null;
private F<Integer> to_i = null;
private Integer i_zero = null;
@Before
public void before() {
this.zero = Church.ZERO;
this.to_i = new F<Integer>() {
public Integer proc(Integer x) {
return Integer.valueOf(x.intValue() + 1);
}
};
this.one = zero.inc();
this.two = one.inc();
this.three = one.plus(two);
this.i_zero = new Integer(0);
}
@After
public void after() {
this.zero = null;
this.one = null;
this.two = null;
this.three = null;
this.to_i = null;
this.i_zero = null;
}
@Test
public void testToI() {
assertThat(this.to_i.proc(i_zero), is(new Integer(1)));
}
@Test
public void testZero() {
assertThat(to_i(this.zero), is(0));
}
@Test
public void testOne() {
assertThat(to_i(this.one), is(1));
}
@Test
public void testTwo() {
assertThat(to_i(this.two), is(2));
}
@Test
public void testThree() {
assertThat(to_i(this.three), is(3));
}
int to_i(Church n) {
return n.proc(this.to_i).proc(this.i_zero).intValue();
}
public static void main(String[] args) {
JUnitCore.main(ChurchTest.class.getName());
}
}
import org.scalatest.FunSuite
class ChurchTestSuite extends FunSuite {
val zero = Church.ZERO;
val one = zero.inc;
val two = one.inc;
val three = two.plus(one); // 2 + 1
val to_i = new F[java.lang.Integer] {
def proc(x : java.lang.Integer) : java.lang.Integer = {
java.lang.Integer.valueOf(x.intValue + 1)
}
}
test("inc is increment") {
assert(to_i.proc(0) === 1)
}
test("zero:Church convert to zero:Num (Church.ZERO)") {
assert(c_to_i(zero) === 0)
}
test("one:Church convert to one:Num (Church.inc)") {
assert(c_to_i(one) === 1)
}
test("two:Church convert to two:Num (Church.inc for incremented)") {
assert(c_to_i(two) === 2)
}
test("three:Church convert to three:Num (Church.plus)") {
assert(c_to_i(three) === 3)
}
// helper method
def c_to_i(n : Church) : Int = {
n.proc(to_i).proc(0).intValue
}
}
public interface F<T> {
public T proc(T x);
}
class Main {
public static void main(String[] args) {
Church zero = Church.ZERO;
Church one = zero.inc();
Church two = one.inc();
Church three = one.plus(two);
F<Integer> to_i = new F<Integer>() {
public Integer proc(Integer x) {
return Integer.valueOf(x.intValue() + 1);
}
};
Integer number_zero = new Integer(0);
System.out.println(zero.proc(to_i).proc(number_zero));
System.out.println(one.proc(to_i).proc(number_zero));
System.out.println(two.proc(to_i).proc(number_zero));
System.out.println(three.proc(to_i).proc(number_zero));
}
}
JAVAC = javac
JAVAC_OPTS = -encoding utf8 -Xlint:unchecked
JAVA = java
JAVA_OPTS = -Dfile.encoding=UTF8
JAVA_CP_OPTS = -cp junit-4.8.1.jar:.
PROVE = prove
PROVE_TARGET = $(wildcard *.pl)
JAVA_SRCS = $(wildcard *.java)
JAVA_OBJS = $(subst .java,.class,$(JAVA_SRCS))
JUNIT_TARGET = $(subst .class,,$(wildcard *Test.class))
Main = Main
SCALAC = scalac
SCALA = scala
SCALA_OPTS = org.scalatest.tools.Runner -p . -o -s
SCALA_CP_OPTS = -cp scalatest-1.0.jar
SCALA_SRCS = $(wildcard *.scala)
SCALA_OBJS = $(subst .scala,.class,$(SCALA_SRCS))
SCALATEST_TARGET = $(subst .scala,,$(SCALA_SRCS))
CLJ = clj
CLJ_OPTS =
CLJ_TARGET = $(wildcard *.clj)
do : build
$(JAVA) $(JAVA_OPTS) $(Main)
build : $(JAVA_OBJS) $(SCALA_OBJS)
test : build
env JAVA=$(JAVA) JAVA_OPTS=$(JAVA_OPTS) Main=$(Main) $(PROVE) $(PROVE_TARGET)
$(JAVA) $(JAVA_CP_OPTS) org.junit.runner.JUnitCore $(JUNIT_TARGET)
$(SCALA) $(SCALA_CP_OPTS) $(SCALA_OPTS) $(SCALATEST_TARGET)
$(CLJ) $(CLJ_OPTS) $(CLJ_TARGET)
$(JAVA_OBJS) : $(JAVA_SRCS)
$(JAVAC) $(JAVAC_OPTS) $(JAVA_CP_OPTS) $(JAVA_SRCS) 2>&1 | lv -Ou | cat
$(SCALA_OBJS) : $(SCALA_SRCS)
$(SCALAC) $(SCALA_CP_OPTS) $(SCALA_SRCS)
clean :
$(RM) $(RMF) *.class
(ns test
(:import F Church)
(:use clojure.contrib.test-is))
(def to_i (proxy [F] [] (proc [x] (+ x 1))))
(def zero (Church/ZERO))
(def one (. zero inc))
(def two (. one inc))
(def three (. one (plus two)))
(defn c_to_i [n]
(.. n (proc to_i) (proc 0)))
(deftest test-to_i
(is (= 1 (. to_i (proc 0)))))
(deftest test-zero-for-static_value
(is (= 0 (c_to_i zero))))
(deftest test-one-for-inc
(is (= 1 (c_to_i one))))
(deftest test-two-for-inc-with-incremented-value
(is (= 2 (c_to_i two))))
(deftest test-three-for-plus
(is (= 3 (c_to_i three))))
(run-tests)
#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 1;
my $expects = <<"EOL";
0
1
2
3
EOL
is(`$ENV{JAVA} $ENV{JAVA_OPTS} $ENV{Main}`, $expects);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment