hping wiki

Differences for page Functional programming in Tcl

Current version compared with version Tue Nov 16 09:51:38 GMT 2004

...
  as a downloadable file at the end of the page.
  
   # Functional.tcl - Functional Programming Library for TCL
-  # Version 14Nov2004
+  # Version 30Nov2004
   #
   # Copyright (C) 2004 Salvatore Sanfilippo <antirez at invece dot org>
   #
...
   # permission to use and distribute the software in accordance with the
   # terms specified in this license.
   
+  ################################################################################
+  # FUNCTIONAL PROGRAMMING COMMANDS
+  ################################################################################
+  
   # [map] works exactly like Tcl's [foreach] command, but for every
   # iteration, the result of the body script is appended to a list
   # that is finally returned.
...
       return $result
   }
   
+  # A non garbage collecting [lamda] implementation.
+  # The best we can get for now. Note that's not very useful
+  # with the implementation of other functional command in this
+  # library because [map], [filter], ... all take Tcl scripts directly
+  # as "inline functions", so lambda is not required.
+  #
+  # However there are times where a command is passed as argument, like
+  # in the case of [fold] command. In such a case [lambda] is useful.
+  proc lambda {argl body} {
+      set name [info level 0]
+      proc $name $argl $body
+      set name
+  }
+  
+  # Given a command 'cmd' and a list 'l' with elements l1, l2, l3, ..., lN
+  # [fold] returns [$cmd ... [$cmd [$cmd l1 l2] l3] ... lN].
+  # For example if 'cmd' is the procedure:
+  #
+  # proc add {x y} {expr {$x+$y}}
+  #
+  # Then [fold {1 2 3 4} add] returns (((1+2) + 3) + 4)
+  proc fold {l cmd} {
+      set res [lindex $l 0]
+      for {set i 1} {$i < [llength $l]} {incr i} {
+  	set res [$cmd $res [lindex $l $i]]
+      }
+      return $res
+  }
+  
+  ################################################################################
+  # ALISTS OPERATIONS
+  ################################################################################
+  
   # Returns the value relative to the key 'k' in the alist 'l'.
   proc aget {l k} {
       if {[llength $l] % 2} {
...
       return -1
   }
   
-  # A non garbage collecting [lamda] implementation.
-  # The best we can get for now. Note that's not very useful
-  # with the implementation of other functional command in this
-  # library because [map], [filter], ... all take Tcl scripts directly
-  # as "inline functions", so lambda is not required.
-  #
-  # However there are times where a command is passed as argument, like
-  # in the case of [fold] command. In such a case [lambda] is useful.
-  proc lambda {argl body} {
-      set name [info level 0]
-      proc $name $argl $body
-      set name
-  }
+  ################################################################################
+  # MATH OPERATORS AS COMMANDS
+  ################################################################################
   
-  # Given a command 'cmd' and a list 'l' with elements l1, l2, l3, ..., lN
-  # [fold] returns [$cmd ... [$cmd [$cmd l1 l2] l3] ... lN].
-  # For example if 'cmd' is the procedure:
-  #
-  # proc add {x y} {expr {$x+$y}}
-  #
-  # Then [fold {1 2 3 4} add] returns (((1+2) + 3) + 4)
-  proc fold {l cmd} {
-      set res [lindex $l 0]
-      for {set i 1} {$i < [llength $l]} {incr i} {
-  	set res [$cmd $res [lindex $l $i]]
-      }
-      return $res
-  }
-  
   # Math operators as commands
   foreach {op neutral} {+ 0 * 1} {
       proc $op args [format {
...
       } elseif {[llength $args] == 1} {
   	expr {1.0/[lindex $args 0]}
       } else {
-  	error "- expects at least 1 argument."
+  	error "/ expects at least 1 argument."
       }
   }
   
...
+  ################################################################################
+  # SETS OPERATIONS
+  ################################################################################
+  
+  proc lintersect {a b} {
+      foreach e $a {
+  	set x($e) {}
+      }
+      set result {}
+      foreach e $b {
+  	if {[info exists x($e)]} {
+  	    lappend result $e
+  	}
+      }
+      return $result
+  }
+  
+  proc lunion {a b} {
+      foreach e $a {
+  	set x($e) {}
+      }
+      foreach e $b {
+  	if {![info exists x($e)]} {
+  	    lappend a $e
+  	}
+      }
+      return $a
+  }
+  
+  proc ldifference {a b} {
+      foreach e $b {
+  	set x($e) {}
+      }
+      set result {}
+      foreach e $a {
+  	if {![info exists x($e)]} {
+  	    lappend result $e
+  	}
+      }
+      return $result
+  }
+  
+  proc in {list element} {
+      expr {[lsearch -exact $list $element] != -1}
+  }
+  
+  ################################################################################
+  # ADDITIONAL LIST FUNCTIONS
+  ################################################################################
+  
   # The [range] command as for TIP 225.
   proc rangeLen {start end step} {
       if {$step == 0} {return -1}
...
-  }+  }
+ 

The following is the old page content