-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoc_base-1.0.tm
104 lines (78 loc) · 2.24 KB
/
oc_base-1.0.tm
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#===============================================================================
# oc_base-1.0.tm
#
# Shorthand conveniance functions: get, exists, for and assign.
#
# Copyright Sam O'Connor 2014
# Licenced for use under the same terms as Tcl 8.6. See:
# http://core.tcl.tk/tcl/artifact/537ba3f664b958496ab51849e23d7f564342014b
# http://github.com/tcltk/tcl/raw/core_8_6_1/license.terms
#===============================================================================
package provide oc_base 1.0
package require oclib::oc_assert
if [info exists ::oc::options::overload_set] {
rename set tcl_set
proc set {args} {
# usage: set var_name value
# or: set dict_name key... value
if {[llength $args] <= 2} {
uplevel [list tcl_set {*}$args]
} else {
uplevel [list dict set {*}$args]
}
}
}
proc get {args} {
# usage: get dict key...
# get: get value
if {[llength $args] == 1} {
lfirst $args
} else {
try {
dict get {*}$args
} trap {TCL LOOKUP DICT} {} {}
}
}
proc exists {args} {
# usage: exists var_name
# or: exists dict key...
if {[llength $args] == 1} {
uplevel [list info exists {*}$args]
} else {
uplevel [list dict exists {*}$args]
}
}
interp alias {} ∃ {} exists
rename for tcl_for
proc for {args} {
# usage: for {set i 0} {$i < $limit} {incr i} { ... }
# or: for {n v} in $dict { ... }
# or: for x in $list { ... }
try {
if {[lindex $args 1] eq "in"} {
uplevel [list foreach {*}[lreplace $args 1 1]]
} else {
uplevel [list tcl_for {*}$args]
}
} on return {result options} {
dict incr options -level
return {*}$options $result
}
}
proc assign {dict args} {
# usage: assign $dict key...
for v in $args {
uplevel [list set $v [get $dict $v]]
}
}
proc capture {args} {
# usage: capture var...
for var in $args {
upvar $var v
dset result $var $v
}
return $result
}
#===============================================================================
# End of file.
#===============================================================================