REBOL [
    Title: "Function to get all names of certain file types"
    Purpose: {Generalized version of a function from a program by
    Carl.  Given a block of file names and a block of file types, 
    return a block of all the file names in the given block 
    that are of that type.}
]

;; [---------------------------------------------------------------------------]
;; [ This is a function borrowed from Carl's picture viewer program.           ]
;; [ Pass to it a block of file names and a block of file types.               ]
;; [ The program will return a block of names of the specified types.          ]
;; [ For example:                                                              ]
;; [     FILE-LIST: [block-of-names] FILES-OF-TYPE [%.jpg %.JPG %.png %.PNG]   ]
;; [ Note how you can make a function within a function.                       ]
;; [ General procedure:                                                        ]
;; [ Go through the list.  If a file has the desired suffix, go on to the      ]
;; [ next file.  Otherwise remove it from the list.  At the end, reposition    ]
;; [ to the head of the list and return it to the caller.                      ]
;; [---------------------------------------------------------------------------]

FILES-OF-TYPE-IN-LIST: func [
    FILE-ID-LIST
    TYPE-LIST
] [
    OF-TYPE?: func [
        FILE-ID 
    ] [
        find TYPE-LIST find/last FILE-ID "."
    ]
    while [not tail? FILE-ID-LIST] [
        either OF-TYPE? first FILE-ID-LIST [
            FILE-ID-LIST: next FILE-ID-LIST
        ] [
            remove FILE-ID-LIST
        ]
    ]
    FILE-ID-LIST: head FILE-ID-LIST
    return FILE-ID-LIST
]

;;Uncomment to test
;TEST-LIST: read %.
;probe FILES-OF-TYPE-IN-LIST TEST-LIST [%.r]
;halt