Permalink
Browse files

Add _::detect and _::reject.

  • Loading branch information...
1 parent 63e1c98 commit 7402daa31f7814761fc4d62f172539a2dcf46b2a @arthurschreiber committed Nov 18, 2012
Showing with 45 additions and 2 deletions.
  1. +45 −2 underscore.tcl
View
@@ -254,9 +254,52 @@ namespace eval _ {
set result [dict create]
foreach item $list {
- dict lappend result [yield $iterator $item] $item
+ dict lappend result [_::yield $iterator $item] $item
}
return $result
}
-}
+
+ # Calls the given block for each element in the list,
+ # returning a new list without the elements for which the block returned
+ # a truthy value.
+ #
+ # @example
+ # set large [_::reject {1 2 3 4 5} {{n} {
+ # expr { $n < 3 }
+ # }}]
+ # set large; # => {3 4 5}
+ #
+ # @param list [list]
+ # @param block [lambda]
+ # @return [list]
+ proc reject { list block } {
+ set result [list]
+ foreach item $list {
+ if { ![_::yield $block $item] } {
+ lappend result $item
+ }
+ }
+ return $result
+ }
+
+ # Looks through each value in the given list, returning the first one for
+ # which the block returned a truthy value.
+ #
+ # @example
+ # set even [_::detect {1 2 3 4 5} {{n} {
+ # expr { $n < 3 }
+ # }}]
+ # set even; # => 2
+ #
+ # @param list [list]
+ # @param block [lambda]
+ # @return [list]
+ proc detect { list block } {
+ foreach item $list {
+ if { [_::yield $block $item] } {
+ return $item
+ }
+ }
+ }
+}

0 comments on commit 7402daa

Please sign in to comment.