èªååæãª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ã§ã¯ä¾¿å©ãªã©ã¤ãã©ãªãããã©ã«ãã§ã¤ãããããã«ãªãã
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)))
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)))
ã³ã¼ã
gistã«æ¸ãã°è¯ãã£ãããã