私は以前に似たようなことをしたことがありますが、方法が異なります。私も式の結果をチェックする必要がありました。このトリックの利点は、自分でチェックする必要がないことです。パターン マッチングがチェックしてくれます。このトリックの鍵は、lists:foldl を使用することです。以下に例を示します。
check_group(Community_code,Category_code,Group_code) ->
Group = dis_scan:get_group_item(Community_code,Category_code,Group_code),
StartItems = [ start_item(Community_code,Category_code,Item_seq_no)
|| {_, _, Item_seq_no } <- Group ],
ok = lists:foldl(fun check_start_item/2, ok, StartItems),
exit(normal).
check_start_item(StartItem, ok) ->
## Return ok if item is ok, {error, Reason} otherwise.
ok.
一方、チェックから返された結果によって項目をグループ化する必要がある場合は、lists:partition を使用します。
check_group(Community_code,Category_code,Group_code) ->
Group = dis_scan:get_group_item(Community_code,Category_code,Group_code),
StartItems = [ start_item(Community_code,Category_code,Item_seq_no)
|| {_, _, Item_seq_no } <- Group ],
{GoodItems, BadItems} = lists:partition(fun check_start_item/1, StartItems),
case BadItems of
[] -> exit(normal);
_ -> error({bad_start_items, BadItems})
end.
check_start_item(StartItem) ->
## Return true if item is ok, false otherwise.
true.