Skip to content

Instantly share code, notes, and snippets.

@yamasushi
Last active December 14, 2015 22:39
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 yamasushi/5160023 to your computer and use it in GitHub Desktop.
Save yamasushi/5160023 to your computer and use it in GitHub Desktop.
; リソースID(uriなど)が指すデータを読んで、アイテムリストに変換する手続きを返す。
; アイテム、ヘッダを多値で返す。(ヘッダはないときもあるのでアイテム優先の順序)
(define (read-and-map$
:key
get-raw ; id ---> res 元データを読む生データを返す
raw->items ; res ---> items 生データからアイテムリストに変換
(raw->header (^x #f) ) ; res ---> header 生データからヘッダ抽出 ... 省略可
(item-map #f ) ; 各アイテムを変換
(item-filter #f ) ; 変換後のフィルタ
(peek-id (^x ) ) ; idを覗く
)
($ (pack$ values
($ (andfn-filter$ item-filter) $ (andfn-map$ item-map) $ (andfn$ raw->items) $)
(andfn$ raw->header) )
$ (andfn$ get-raw) $ (peek$ peek-id ) $) )
; ($ .. $ ..)のなかで使う。途中で#fが返ると#fをかえすように
;andfn$ ... f .... 元の関数、#fを渡すと identityを返す
(define (andfn$ f)
(if f
(^x (and x (f x) ) )
identity ) )
;andfn-map$ ... f .... 元の関数、#fを渡すと identityを返す
(define (andfn-map$ f)
(if f
($ andfn$ $ map$ f)
identity ) )
;andfn-filter$ ... f .... 元の関数、#fを渡すと identityを返す
(define (andfn-filter$ f)
(if f
($ andfn$ $ filter$ f)
identity ) )
; .$ の途中にはさんで中身をのぞくため
(define (peek$ peeker)
(^ arg
(apply peeker arg) ;; xを覗く
(apply values arg) ;; xをそのまま返す
))
; 手続き列に値を通して、リストにするような手続きを返す
; ( (list-pack$ f g h) x ) ---> ( (f x) (g x) (h x) )
; ( (list-pack$ car cdr) '(1 2 3) ) ----> ( 1 (2 3))
(define (list-pack$ . fs)
(^ xs (map (cut apply <> xs) fs) ) )
; 手続き列に値を通して、指定手続きpackerで固める手続きを返す
; ((pack$ f g h) x) -----> (f (g x) (h x) )
(define (pack$ packer . fs)
($ packer $* (apply list-pack$ fs) $*) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment