|
| 1 | +function result = find_and_count(root_folder_path, is_file_a_keeper_predicate, folder_filter_function) |
| 2 | + seed = 0 ; % for counting, start with a count of zero |
| 3 | + |
| 4 | + function result = filter(root_folder_path, base_folder_relative_path, file_name, depth, is_this_file_a_folder) %#ok<INUSD> |
| 5 | + result = feval(is_file_a_keeper_predicate, root_folder_path, base_folder_relative_path, file_name) ; |
| 6 | + end |
| 7 | + |
| 8 | + result = file_accumulate_map_filter(root_folder_path, seed, @accumulator, @mapper, @filter, folder_filter_function) ; |
| 9 | +end |
| 10 | + |
| 11 | +function result = accumulator(result_so_far, file_value) |
| 12 | + % For counting, just add the file_value to the ongoing count |
| 13 | + result = result_so_far + file_value ; |
| 14 | +end |
| 15 | + |
| 16 | +function result = mapper(root_folder_path, base_folder_relative_path, file_name, depth) %#ok<INUSD> |
| 17 | + % For counting, just want to return one for each file that gets past the |
| 18 | + % filter |
| 19 | + result = 1 ; |
| 20 | +end |
| 21 | + |
| 22 | + |
| 23 | +% function result = find_and_count(base_folder_path, is_file_a_keeper_predicate, varargin) |
| 24 | +% [folder_predicate_function] = ... |
| 25 | +% parse_keyword_args(... |
| 26 | +% varargin, ... |
| 27 | +% 'folder_predicate_function', @(folder_path, depth)(true)) ; |
| 28 | +% |
| 29 | +% result = ... |
| 30 | +% find_and_count_helper(base_folder_path, ... |
| 31 | +% is_file_a_keeper_predicate, ... |
| 32 | +% folder_predicate_function, ... |
| 33 | +% 0, ... |
| 34 | +% 0) ; |
| 35 | +% end |
| 36 | +% |
| 37 | +% |
| 38 | +% |
| 39 | +% function result = ... |
| 40 | +% find_and_count_helper(base_folder_path, ... |
| 41 | +% is_file_a_keeper_predicate, ... |
| 42 | +% folder_predicate_function, ... |
| 43 | +% depth, ... |
| 44 | +% incoming_count) |
| 45 | +% count_so_far = incoming_count ; |
| 46 | +% [file_names, is_file_a_folder] = simple_dir(base_folder_path) ; |
| 47 | +% file_count = length(file_names) ; |
| 48 | +% for i = 1 : file_count , |
| 49 | +% file_name = file_names{i} ; |
| 50 | +% is_this_file_a_folder = is_file_a_folder(i) ; |
| 51 | +% file_path = fullfile(base_folder_path, file_name) ; |
| 52 | +% if is_this_file_a_folder , |
| 53 | +% % if a folder satisfying predicate function, recurse |
| 54 | +% if feval(folder_predicate_function, file_path, depth) , |
| 55 | +% count_so_far = ... |
| 56 | +% find_and_count_helper(... |
| 57 | +% file_path, ... |
| 58 | +% is_file_a_keeper_predicate, ... |
| 59 | +% folder_predicate_function, ... |
| 60 | +% depth+1, ... |
| 61 | +% count_so_far) ; |
| 62 | +% end |
| 63 | +% else |
| 64 | +% if feval(is_file_a_keeper_predicate, file_path) , |
| 65 | +% count_so_far = count_so_far + 1 ; |
| 66 | +% end |
| 67 | +% end |
| 68 | +% end |
| 69 | +% result = count_so_far ; |
| 70 | +% end |
0 commit comments