コード
gistã«æ›¸ã‘ã°è‰¯ã‹ã£ãŸã‹ã‚‚。
listã‚’ä¸€é€£ã®æµã‚Œã®é€£éŽ–ã§ã¯ãªã候補ã‹ã‚‰ã®é¸æŠžã¨æ‰ãˆã¦ã¿ã‚‹ã¨â€¦
listã‚’ä¸€é€£ã®æµã‚Œã®é€£éŽ–ã§ã¯ãªã候補ã‹ã‚‰ã®é¸æŠžã¨æ‰ãˆã¦ã¿ã‚‹ã¨ã€åˆ¥ãªå½¢ã®ãƒžã‚¯ãƒãŒæ€ã„æµ®ã‹ã¶ã€‚
(やりéŽãŽè‰¯ããªã„…ãŸã ã€å®Ÿé¨“ã¨ã„ã†ã“ã¨ã§)
例ãˆã°ã€ã“ã‚“ãªæ„Ÿã˜ã€‚clojureã®->,->>ã«ãªã‚‰ã„ã€åˆæœŸå€¤ã‚’å–りãれをもã¨ã«é¸æŠžã‚’行ã†ã‚ˆã†ã«ãªã£ã¦ã„る。
(define (f x p val) (and (p x) val)) (define (QandA Q) (@maybeL Q (f symbol? 'symbol) (f integer? 'int) (f list? 'list) (f identity 'unknown))) (map QandA '(10 aa (aa) #(aa))) ; => (int symbol list unknown)
ä»–ã®æ§‹æ–‡ã¨ã¯ç•°è³ªãªã‚‚ã®ãªã®ã§ã€ãれをã¯ã£ãりã•ã›ã‚‹ãŸã‚ã«@ã‚’ã¤ã‘ã¦ã„る。
code
(use util.match) (define-macro (@maybeL init . candidates) (let ((tmp (gensym))) `(let1 ,tmp ,init (or ,@(map (match-lambda [(fn . args) `(,fn ,tmp ,@args)] [fn `(,fn ,tmp)]) candidates))))) (define-macro (@maybeR init . candidates) (let ((tmp (gensym))) `(let1 ,tmp ,init (or ,@(map (match-lambda [(fn . args) `(,fn ,@(append args (list tmp)))] [fn `(,fn ,tmp)]) candidates))))) (define-macro (@allL init . candidates) (let ((tmp (gensym))) `(let1 ,tmp ,init (and ,@(map (match-lambda [(fn . args) `(,fn ,tmp ,@args)] [fn `(,fn ,tmp)]) candidates))))) (define-macro (@allR init . candidates) (let ((tmp (gensym))) `(let1 ,tmp ,init (and ,@(map (match-lambda [(fn . args) `(,fn ,@(append args (list tmp)))] [fn `(,fn ,tmp)]) candidates)))))
@maybeLãŒã‚ã‚‹ã¨fizzbuzzãŒä»¥ä¸‹ã®ã‚ˆã†ã«æ›¸ã‘る。(æ„味ãŒã‚ã‚‹ã‹ã¯çŸ¥ã‚‰ãªã„)
fizzbuzz
(define (f n x val) (and (zero? (modulo n x)) val)) (dotimes (i 100) (print (@maybeL k (f 15 'fizzbuzz) (f 3 'fizz) (f 5 'buzz) identity)))
clojureã®ã‚«ã‚¹ã‚±ãƒ¼ãƒ‰ãƒžã‚¯ãƒã‚’å°Žå…¥
昨日書ã„ã¦ã„ãŸã‚³ãƒ¼ãƒ‰ã®ä¸ã§assoce-default相当ã®é–¢æ•°ã‚’書ã“ã†ã¨ã—ãŸéš›ã«ã€
clojureã®-?>ãŒä½¿ã„ãŸããªã£ãŸã€‚-?>ã¯çµæžœã‚’ã¿ã¦å½ãªã‚‰å®Ÿè¡Œã‚’途ä¸ã§æ‰“ã¡åˆ‡ã‚‹ã¨ã„ã†ç‚¹ã§ã¯and-let*ã¨åŒæ§˜ã€
ã§ã‚‚and-let*ã¨ã¯ç•°ãªã‚Šçµæžœã«ç‰¹ã«åå‰ã‚’ã¤ã‘ãŸããªã„よã†ãªå ´åˆã«ä½¿ã„ãŸããªã‚‹ã€‚
ã“れãŒã‚ã‚‹ã¨assoc-defaultã¯ä»¥ä¸‹ã®ã‚ˆã†ã«æ›¸ã‘る。
(define (assoc-default e alist) ;;elispãªã©ã«ã‚ã‚‹ (-?> (assoc e alist) cadr)) (assoc-default 'x '((x 10) (y 20) (z 30))) ; => 10 (assoc-default 'i '((x 10) (y 20) (z 30))) ; => #f
code
(use util.match) (define-macro (-> init . rest) (fold (lambda (xs acc) (match xs [(fn . args) `(,fn ,acc ,@args)] [fn `(,fn ,acc)])) init rest)) (define-macro (->> init . rest) (fold (lambda (xs acc) (match xs [(fn . args) `(,fn ,@(append args (list acc)))] [fn `(,fn ,acc)])) init rest)) (define-macro (-?> init . rest) (let1 tmp (gensym) (fold (lambda (xs acc) (match xs [(fn . args) `(let1 ,tmp ,acc (and ,tmp (,fn ,tmp ,@args)))] [ fn `(let1 ,tmp ,acc (and ,tmp (,fn ,tmp)))])) init rest))) ;; (macroexpand '(-> 10)) ;; (macroexpand '(->> 10)) ;; (macroexpand '(-?> 10)) ;; (macroexpand '(-> 10 (+ 2) (- 3))) ;; (display (unwrap-syntax (macroexpand '(-?> 10 (+ 2) (- 3))))) ;; (macroexpand '(->> 10 (+ 2) (- 3)))
è‡ªåˆ†å‹æ‰‹ãªgoshコマンドã®ä½œæˆ
個人用ã®ã‚¹ã‚¯ãƒªãƒ—トを書ãéš›ã«ã¯ã€ä¾¿åˆ©ãªãƒ©ã‚¤ãƒ–ラリãŒå…¨ã¦useã•れã¦ã„ã‚‹ã¨å¬‰ã—ã„。
(gauche.experimental.*ã¯ã€ã“れをuseã™ã‚‹ã®ãŒå„„劫ã«ãªã‚Šä½¿ã‚ãªã„ã“ã¨ãŒåº¦ã€…)
今ã¾ã§ã¯é€ä¸€åˆ©ç”¨ã—ãŸã„ライブラリをuseã—ã¦ããŸã‘れã©â€¦é¢å€’ãã•ã„。
特ã«ä»–人ã¨å…±æœ‰ã—よã†ã¨ã™ã‚‹ã‚¹ã‚¯ãƒªãƒ—トã§ãªã‘れã°ã€ã‚‚ã£ã¨ã‚ãŒã¾ã¾ã«æŒ¯ã‚‹èˆžã£ã¦ã‚‚良ã„ã®ã§ã¯ãªã„ã‹ã¨æ€ã£ãŸã€‚
幸ã„goshã¯-lオプションã§äº‹å‰ã«èªã¿è¾¼ã‚€ãƒ•ァイルをè¨å®šã§ãる。
個人的ãªå‡¦ç†ã‚’行ã†éš›ã«èµ·å‹•ã™ã‚‹goshコマンドをgosh-exã¨ã—ã¦ä½œã£ã¦ã¿ãŸã€‚
è¡Œã†æ‰‹é †ã¯ä»¥ä¸‹ã®ã¨ãŠã‚Š
- ~/.gosh-ex作æˆ
- gosh-ex コマンドを作æˆ
.gosh-ex
useã—ã¦ãŠããŸã„ライブラリãªã©ã‚’useã™ã‚‹ãªã©ã™ã‚‹ã€‚
(use gauche.experimental.lamb) (use gauche.experimental.ref) (use gauche.experimental.app) (use srfi-1) (use util.match) (use util.list)
gosh-exコマンド
以下ã®ã‚ˆã†ãªã‚·ã‚§ãƒ«ã‚¹ã‚¯ãƒªãƒ—トを作æˆ
#!/bin/sh gosh -I ~ -l ~/.gosh-ex $@
ã“れã§gosh-exã§ç«‹ã¡ä¸Šã’ãŸreplã§ã¯ä¾¿åˆ©ãªãƒ©ã‚¤ãƒ–ラリãŒãƒ‡ãƒ•ォルトã§ã¤ã‹ãˆã‚‹ã‚ˆã†ã«ãªã‚‹ã€‚
re:schemeã§å…¨dataã‚’+ã™ã‚‹ã®ã‚’知りãŸã„ã§ã™
ã“ã‚“ãªæ„Ÿã˜ã‹ãªï¼Ÿ
http://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q1347295398
http://d.hatena.ne.jp/yad-EL/20100921/p1
(define (mysum xs) (fold (lambda (x acc) (+ (if (list? x) (mysum x) x) acc)) 0 xs)) ;; (mysum 20) ;; (mysum '()) ; => 0 ;; (mysum '(1 2 3)) ; => 6 ;; (mysum '((1 2) ((3)) (4 (5)))) ; => 15 (define (tree-map fn tree) (map (lambda (x) (if (list? x) (tree-map fn x) (fn x))) tree)) (define (mytrans xs) (let1 source '(zero one two three four five six seven eight nine) (cond ((null? xs) 0) (else (tree-map (pa$ list-ref source) xs))))) ;; (mytrans 1) ;; (mytrans '()) ;; (mytrans '(1 2 3)) ; => (one two three) ;; (mytrans '((1 2) ((3)) (4 (5 6)))) ; => ((one two) ((three)) (four (five six))) (define (my-find xs e) (and-let* ((pair (assoc e xs))) (cadr pair))) ;; (my-find 10) ;; (my-find 'a) ;; (my-find '((x 10) (y 20) (x 30)) 'x) ; => 10 ;; (my-find '((x 10) (y 20) (x 30)) 'i) ; => #f
mirahを使ã£ã¦ã¿ã‚‹
mirah=javaã®speed+rubyã®æ–‡æ³• ã®ã‚ˆã†ãªè¨€èªžã§ã™ã€‚
rubyã£ã½ã„記述を翻訳ã—ã¦.classã«è½ã¨ã—込むよã†ãªãƒˆãƒ©ãƒ³ã‚¹ãƒ¬ãƒ¼ã‚¿ã®ã‚ˆã†ãªã‚‚ã®ã¿ãŸã„ã§ã™ã€‚
C#ã£ã½ã„見ãŸç›®ã®ãƒ•ァイルをC+glibã®å½¢ã«ç¿»è¨³ã™ã‚‹valaã«è¿‘ã„æ„Ÿã˜ã§ã™ã。
install
BuildingMirahãŒã¨ã¦ã‚‚å‚考ã«ãªã‚‹ã€‚
javaã¨gitã®ç’°å¢ƒã¯å¿…è¦
pwd=`pwd` mkdir workarea && cd workarea ## jruby wget http://jruby.org.s3.amazonaws.com/downloads/1.5.2/jruby-bin-1.5.2.tar.gz tar xvzf jruby-bin-1.5.2.tar.gz mv jruby-1.5.2 jruby ## mirah git clone http://github.com/headius/bitescript.git #core library for mirah git clone http://github.com/headius/mirah.git ## build cd ./mirah ../jruby/bin/jruby -S rake jar ## add $PATH cd $pwd cd workarea/jruby/bin jruby=`pwd` cd $pwd cd workarea/mirah/bin mirah=`pwd` export PATH=$mirah:$jruby:$PATH cd $pwd
使ã£ã¦ã¿ã‚‹
例ãˆã°ã€ä»¥ä¸‹ã®ã‚ˆã†ãªãƒ•ァイルを作æˆ(hello.mirah)
puts "hello"
ã“ã®ãƒ•ァイルをmirahcã§ã‚³ãƒ³ãƒ‘イル
$ mirahc hello.mirah && java Hello # mirahc ã«-jオプションをã¤ã‘ã‚‹ã¨.javaã§å‡ºåŠ›ã—ã¦ãれる。 $ mirahc -j hello.mirah
ã¡ãªã¿ã«ä»¥ä¸‹ã®ã‚ˆã†ãªjavaã®ã‚³ãƒ¼ãƒ‰ã«ç¿»è¨³ã•れã¾ã™ã€‚
// Generated from hello.mirah public class Hello extends java.lang.Object { public static void main(java.lang.String[] argv) { java.io.PrintStream temp$1 = java.lang.System.out; temp$1.println("hello"); } }
anything-occurãªã©ã§ç¾åœ¨ã®selectionã®ä½ç½®ã‚’表示ã™ã‚‹ã€‚
上下ã«ç§»å‹•ã™ã‚‹éš›ã«ã€persistent-actionを実行ã—ã¦ä¸Šã’れã°ã§ããã†ã§ã™ã€‚(type-attributeãŒlineãªã‚‰)
今回ã¯ã€
- anything-bm-list*
- anything-occur*
を作ã£ã¦ã¿ã¾ã—ãŸã€‚
code
(defadvice anything-next-line (after execute-persistent-action disable) | |
(unless (or (anything-get-previous-header-pos) | |
(anything-get-next-header-pos)) | |
(call-interactively 'anything-execute-persistent-action))) | |
(defadvice anything-previous-line (after execute-persistent-action disable) | |
(unless (or (anything-get-previous-header-pos) | |
(anything-get-next-header-pos)) | |
(call-interactively 'anything-execute-persistent-action))) | |
;; (defadvice anything-next-line (after line-goto disable) | |
;; (and-let* ((selection (anything-get-selection))) | |
;; (let1 old-amount anything-scroll-amount | |
;; (setq anything-scroll-amount (1- (car selection))) | |
;; (unwind-protect | |
;; (anything-scroll-other-window-base 'goto-line) | |
;; (setq anything-scroll-amount old-amount))))) | |
;; (defadvice anything-previous-line (after line-goto disable) | |
;; (and-let* ((selection (anything-get-selection))) | |
;; (let1 old-amount anything-scroll-amount | |
;; (setq anything-scroll-amount (1- (car selection))) | |
;; (unwind-protect | |
;; (anything-scroll-other-window-base 'goto-line) | |
;; (setq anything-scroll-amount old-amount))))) | |
(defmacro with-anything-line-move-advice (advice-name action) | |
`(progn | |
(ad-enable-advice 'anything-next-line 'after ',advice-name) | |
(ad-activate 'anything-next-line) | |
(ad-enable-advice 'anything-previous-line 'after ',advice-name) | |
(ad-activate 'anything-previous-line) | |
(unwind-protect | |
,action | |
(progn (ad-deactivate 'anything-previous-line) | |
(ad-deactivate 'anything-next-line))))) | |
(defun anything-occur* () | |
"Preconfigured Anything for Occur source." | |
(interactive) | |
(with-anything-line-move-advice | |
execute-persistent-action | |
(anything-occur))) | |
(defun anything-bm-list* () | |
"Preconfigured `anything' for visible bookmarks." | |
(interactive) | |
(with-anything-line-move-advice | |
execute-persistent-action | |
(anything-bm-list))) |