Factor 言語ですこし遊んでみている。
http://factorcode.org/
>> Factor is a general purpose, dynamically typed, stack-based programming language.
Factor に含まれている テトリス を動かすことは出来た。
"tetris" run
スペースインベーダもあるようだが、まだ動作させられずにいる。
"space-invaders" run
すばらくすると次のエラーがでる。
Set 'rom-root' to the path containing the root of the 8080 ROM files.
google で検索すると次の情報を得た。
- http://www.bluishcoder.co.nz/2007/02/emulating-other-intel-8080-based.html
> > Emulating other Intel 8080 Based Arcade Games
- http://www.mail-archive.com/factor-talk@lists.sourceforge.net/msg00972.html
> > [Factor-talk] space-invaders error
- http://www.mameworld.net/maws/romset/invaders
rom データなどがある?
dll をセットしろとか、ROM データを置いて、そこを差すように変数をセットしろとか
いろいろ面倒そう。
そもそも Mac (G4) で動作するのか?
ネット上で探した、Factor スクリプトの例を試している。それを以下に紹介する。
最初は helloworld プログラムと フィボナッチ数計算の例だ。
: foo は foo というメソッド(word) の定義。
! で始まる行はコメント。定義したメソッド(word) の実行の仕方を書いている。
$ cat zzz.factor
! See http://ancient.s6.xrea.com/factor/cookbook.html#sec9
! "/Users/kato/work/src/factor/zzz.factor" run-file
! ¥ hello edit
! ¥ fib edit
USING: kernel io math memory system ;
: hello "Hello World! Factor" print ;
! hello
: fib
dup 3 <
[ drop 1 ]
[ dup 1 - fib swap 2 - fib + ]
if ;
! 10 fib .
! [ 32 fib . ] time
次は、ファイルに乱数を書き出す例だ。
times で実行時間の計測もできるようだ。
$ cat zzz01.factor
! See http://www.wagerlabs.com/blog/2008/01/introducing-fac.html
USING: prettyprint kernel math io io.mmap io.files io.binary splitting sequences alien.c-types ;
: from-source "/dev/random" <file-reader> ;
: to-target "/tmp/floats.bin" <file-writer> ;
: dump-floats
4000000 from-source dup >r stream-read
to-target dup >r stream-write
r> stream-close r> stream-close ;
: clip 1000.0 min -1000.0 max ;
: make-float le> bits>float ;
: sum-floats
"/tmp/floats.bin" 4000000 [
4 <sliced-groups> 0 [ make-float clip + 2 / ] reduce
] with-mapped-file ;
! [ sum-floats ] time
!! 7737 ms run / 76 ms GC time
: sum-floats1
"/tmp/floats.bin" 4000000 [
0 swap dup length 4 / swap mapped-file-address
[ float-nth clip + 2 / ] curry each
] with-mapped-file ;
! [ sum-floats1 ] time
!! 1358 ms run / 27 ms GC time
最後は、 階乗計算の複数の定義だ。3通りの方法を試している。
$ cat zzz02.factor
! See http://useless-factor.blogspot.com/2007/07/factorial-101.html
! http://useless-factor.blogspot.com/2008/02/factorial-102.html
! http://useless-factor.blogspot.com/search/label/factor
USING: prettyprint kernel math math.ranges
io io.mmap io.files io.binary splitting sequences alien.c-types ;
: factorial ( n -- n! )
dup zero? [ drop 1 ] [ dup 1- factorial * ] if ;
! 10 factorial .
: factorialX ( n -- n! )
1 [ 1+ * ] reduce ;
! 10 factorialX .
: factorialY ( n -- n! )
[1,b] product ;
! 10 factorialY .
いずれも、java などより簡潔に記載できている。
Factor は多くの ライブラリーが用意されているし、それらのナビゲート機能もある。
(テトリスゲームがかけるぐらいには)
どう書く?.org でもいくつか Factor での解答が投稿されている。
http://ja.doukaku.org/tag/Factor/
もうすこし、Factor で遊んでみようと思う。
最近のコメント