Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

array foreach implementation #10

Open
medranocalvo opened this issue Nov 21, 2016 · 1 comment
Open

array foreach implementation #10

medranocalvo opened this issue Nov 21, 2016 · 1 comment

Comments

@medranocalvo
Copy link

medranocalvo commented Nov 21, 2016

Hello,

here is an implementation of the array foreach improvement. I changed the interface to be more similar to that of dict for: array for {varName varName} arrayName { code }, are you O.K. with that? If so, I'll write required documentation changes and propose it for inclusion in Tcl core. As a bonus, you can use this today, no need to wait on a new Tcl release.

Thank you,
Adrián

proc ::tcl::array::for {variableNames arrayName code} {
    ::upvar 1 $arrayName arr;
    ::tailcall ::foreach $variableNames [::array get arr] $code
}
::namespace ensemble configure array -map \
    [dict replace [::namespace ensemble configure array -map] \
	 for ::tcl::array::for];

# Example usage:
array set myarr {
    x 1
    y 2
    z 3
}
array for {key val} myarr {
   puts "$key=$val";
}
@bovine
Copy link
Member

bovine commented Nov 22, 2016

A previously existing TIP was opened by karl years ago but never finished. The template might be reuable as the basis for this new TIP. http://www.tcl.tk/cgi-bin/tct/tip/421.html

Please note that the primary motivation of this enhancement is not just a syntactic improvement, but an actual performance improvement by eliminating the need to duplicate the array with [array get]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants