-
Notifications
You must be signed in to change notification settings - Fork 5
/
indexable.edh
86 lines (62 loc) · 1.72 KB
/
indexable.edh
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
76
77
78
79
80
81
82
83
84
85
86
class Data {
d = {}
# magic method responding to read with an index
method ([]) ( ix ) {
# console.info <| 'Indexing with ' ++ type(ix) ++ ': ' ++ ix
case ix of {
{ ( start:stop:step ) } -> {
console.info <| 'Indexing interleaved 1d range: ' ++ ix
return NA # no way to be success on a dict
}
{ ( start:stop ) } -> {
console.info <| 'Indexing contiguous 1d range: ' ++ ix
return NA # no way to be success on a dict
}
{ ( dim'1, dim'2 ) } -> {
console.info <| 'Indexing 2d space with: ' ++ ix
case dim'1 of {
{ ( start:stop:step ) } -> {
console.info <| 'Indexing interleaved 1st dimension range: ' ++ dim'1
return NA # no way to be success on a dict
}
{ ( start:stop ) } -> {
console.info <| 'Indexing contiguous 1st dimension range: ' ++ dim'1
return NA # no way to be success on a dict
}
}
# similar can be done for 2nd dimension - dim'2
return NA # no way to be success on a dict
}
}
case type( ix ) of {
"Decimal" -> {
console.info <| 'Indexing 1d element: ' ++ ix
}
"String" -> {
console.info <| 'Indexing column by name: ' ++ ix
}
_ -> {
console.info <| 'Suspicious index ' ++ type( ix ) ++ ': ' ++ ix
return NA # avoid actually doing indexing with this ix
}
}
return this.d[ ix ]
}
# magic method responding to write with an index
method ([=]) ( ix, val ) this.d[ ix ] = val
}
# %%
d = Data()
# %%
d[ 3 ] = 5
d[ 3 ]
# %%
d[ 'price' ] = [ 1.2, 1.3, 1.1 ]
d[ 'price' ]
# %%
d[ 3:5 ] = 7
d[ 3:5 ]
# %%
d[ 3:5:2, 0:7:3 ]
# %%
d[ 3, 5, 7 ]