Hacker News new | past | comments | ask | show | jobs | submit login

I think a good example of this concept is the foreach command in Tcl: It's quite flexible compared to list iteration constructs found in most languages.

This is normal usage:

  foreach x {1 2} {puts $x} => 1 2
You can iterate over two lists at once:

  foreach x {1 2} y {3 4} {puts $x $y} => 1 3 2 4
Instead of reading one element from the list, you can read multiple:

  foreach {x1 x2} {1 2 3 4} y {5 6} {puts $x1 $x2 $y} => 1 2 5 3 4 6
If any of the lists happen to run out before the others, instead of complaining it just returns empty elements.

Many commands in Tcl give the impression that they can be used in many different ways, so writing a program in Tcl feels like building a structure by creatively assembling a limited set of legos.




Aha, I’d known some of the superpowers of TCL’s foreach but not all of those.

A ‘deep command’.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: