-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProcessRecords.oz
75 lines (66 loc) · 1.57 KB
/
ProcessRecords.oz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
%==============
% Code for processing records.
%
% Author: Satyadev Nandakumar
% Date : Fri Sep 28 18:34:06 2012
%==============
%==========
% Check if the entries in a *sorted* list
% are unique.
%==========
fun {HasUniqueEntries L}
case L
of H|T then
case T
of nil then true
[] !H|T1 then false
else {HasUniqueEntries T}
end
end
end
%===========================
% equals Value.'<' if A and B are of the same type.
% Otherwise, any number is treated less than any literal.
%===========================
fun {MixedCompare A B}
C D
in
case A
of literal(C)
then
case B
of literal(D)
then
if {IsNumber C}=={IsNumber D}
then C<D
else {IsNumber C} end
end
end
end
%=== Example Usage ===
% {Browse {HasUniqueEntries {Sort [a 1 2 d c 3] MixedCompare}}}
%=====================
%==================
% The list of fieldname#value pairs can be specified in any
% order. The function returns a list of pairs sorted in the "arity"
% order - numerical fieldnames first, sorted in ascending order,
% followed by lexicographic fieldnames in alphabetical order.
%==================
fun {Canonize Pairs}
Keys = {Map Pairs fun {$ X} X.1 end}
SortedKeys = {Sort Keys MixedCompare}
FindPairWithKey
Result
in
if {HasUniqueEntries SortedKeys}
then
%=======================
% return unique K#value pair
%=======================
fun {FindPairWithKey K}
{Filter Pairs fun {$ Y} Y.1 == K end}.1
end
{Map SortedKeys FindPairWithKey}
else illegalRecord(Pairs)
end
end