Skip to content

Commit

Permalink
Added "got" command to listutils(n).
Browse files Browse the repository at this point in the history
Checks to see whether a list is non-empty.
  • Loading branch information
wduquette committed Aug 29, 2014
1 parent 2d1abba commit 47c965b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
23 changes: 23 additions & 0 deletions docs/mann/listutils.manpage
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,29 @@ listutils(n) defines the following commands:<p>

<deflist commands>

<defitem got {got <i list>}>

Returns 1 if the list contains any elements, and 0 if the list is empty.
This allows the programmer to write

<pre>
if {[got $someListThatMightBeEmpty]} {
...
}
</pre>

instead of<p>

<pre>
if {[llength $someListThatMightBeEmpty] > 0} {
...
}
</pre>

Which seems somewhat trivial, but it reads better, and I'm less likely
to mistype it than the "llength" version, which I seem to mistype about
half the time.<p>

<defitem interleave {interleave <i alist blist>}>

Given two lists, returns the result of interleaving the elements of the
Expand Down
10 changes: 10 additions & 0 deletions lib/quill/listutils.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace eval ::quill:: {
namespace export \
got \
interleave \
ladd \
ldelete \
Expand All @@ -28,6 +29,15 @@ namespace eval ::quill:: {
#-------------------------------------------------------------------------
# Command Definitions

# got list
#
# list - A list
#
# Returns 1 if the list has any elements, and 0 otherwise.

proc ::quill::got {list} {
expr {[llength $list] > 0}
}

# ladd listvar value...
#
Expand Down
15 changes: 15 additions & 0 deletions test/quill/listutils.test
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ namespace import ::quill::*
#-------------------------------------------------------------------------
# Setup

#-------------------------------------------------------------------------
# got

test got-1.1 {empty} -body {
got {}
} -result {0}

test got-1.2 {one item} -body {
got {a}
} -result {1}

test got-1.3 {two item} -body {
got {a b}
} -result {1}

#-------------------------------------------------------------------------
# interleave

Expand Down

0 comments on commit 47c965b

Please sign in to comment.