5 ms·
I love the TCL foreach loops. TCL is the only language that lets you iterate through multiple data structures with the same loop. It is actually really useful.
by jostmey 14y ago
I love the TCL foreach loops. TCL is the only language that lets you iterate through multiple data structures with the same loop. It is actually really useful. Just try this statement with any other language:
foreach animal { "dog" "cat" "bird" } color { "brown" "black" "red" } {
puts "See the $color $animal?"
}
- eurleif 14y agofor animal, color in zip(['dog', 'cat', 'bird'], ['brown', 'black', 'red']): print 'See the %s %s?' % (color, animal)
- jostmey 14y agoI never knew about this zip command. I guess it brings the same utility offered by the TCL foreach loops to python. Still, I wish more languages directly imitated the TCL foreach loop. I've found that when I code, I wish I could just do some simple task 3-4 times. I am always tempted to create a little function, and to call that function repeatedly. But that seems like overkill. With TCL, I just write a simple loop statement instead of the function. I've found that my code is more compact, and the number of functions that I declare is reduced.
- thomc 14y agoRuby too: ['dog', 'cat', 'bird'].zip(['brown', 'black', 'red']).each {|animal, color| puts "See the #{color} #{animal}?"}
- thomc 14y agoI'm fairly certain you can do it in other languages, unless I didn't understand what you mean. In Python it would be something like: for animal, color in zip(['dog', 'cat', 'bird'], ['brown', 'black', 'red']): print 'See the %s %s?' % (color, animal) edit: eurleif beat me too it.
- silentbicycle 14y agoIn k (kona): a:`dog`cat`bird c:`brown`black`red {`0:,//("See the ";$x;" ";$y;"?\n")}'[c;a] The string formatting is a bit ugly because I'm not using a dedicated format function, but foreach with multiple data structures is just f'[list;of;arguments]. In other words, it's one character: ' ("each").
- gruseom 14y agoThis is fun. (loop :for animal :in '("dog" "cat" "bird") :for color :in '("brown" "black" "red") :do (format t "See the ~a ~a?~%" color animal)) See the brown dog? See the black cat? See the red bird? How far can we take the example? (loop :for animal :in '("dog" "cat" "bird") :for color :in '("brown" "black" "red") :for count :from 2 :do (format t "See the ~a ~a ~as?~%" count color animal)) See the 2 brown dogs? See the 3 black cats? See the 4 red birds? How about this? (loop :for animal :in '("dog" "cat" "bird") :for color :in '("brown" "black" "red") :for count :from 2 :sum count :into total :do (format t "See the ~a ~a ~as?~%" count color animal) :finally (format t "See all ~a?~%" total)) See the 2 brown dogs? See the 3 black cats? See the 4 red birds? See all 9? Anybody?
- jostmey 14y agoOkay, so I am impressed that you can also do this in lisp. I never really paid any attention to the language. Maybe it is worth checking out.
- gruseom 14y agoTo be fair, LOOP isn't really Lisp so much as an embedded DSL for loops that exists because of how easy Lisp makes embedded DSLs. People either love or hate Common Lisp's LOOP. Lispy types who prefer recursion often abhor it. I'm a lover.
- deleted 14y ago[deleted]
- arohner 14y ago(doseq [animal ["dog" "cat" "bird" color ["brown" "black" "red"]] (printf "see the %s %s?\n" color animal))
- pka 14y agoThe same thing in Clojure (untested): (doseq [[a c] (zip ["dog" "cat" "bird"] ["brown" "black" "red"])] (print "See the " c a "?")) Haskell: mapM_ (\(a, c) -> putStrLn $ "See the " ++ c ++ " " ++ a ++ "?") (zip ["dog", "cat", "bird"] ["brown", "black", "red"]) edit: one can do way more complicated stuff with the list monad in Haskell and the very comprehensive core lib in Clojure; I'm sure even these examples can still be made a little bit shorter and more readable.
- cygx 14y agoPerl6 version: for <dog cat bird> Z <brown black red> -> $animal, $color { say "See the $color $animal?" }