1 # Jim stdlib - a pure-Jim extension library for Jim
3 # Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # A copy of the license is also included in the source distribution
12 # of Jim, as a TXT file name called LICENSE.
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
20 # To use this library just do [package require stdlib]
21 # Make sure this file is in one directory specified in $jim_libpath
23 package provide stdlib
1.0
25 ### Functional programming ###
27 proc curry
{cmd args
} {
28 lambda args
[list cmd
[list pref
$args]] {
29 uplevel 1 [list $cmd {expand
}$pref {expand
}$args]
33 proc memoize
{} {{Memo
{}}} {
34 set cmd
[info level
-1]
35 if {[info level
] > 2 && [lindex [info level
-2] 0] eq
"memoize"} return
36 if {![info exists Memo
($cmd)]} {set Memo
($cmd) [eval $cmd]}
37 return -code return $Memo($cmd)
40 ### Control structures ###
42 proc repeat
{n body
} {
43 for {set i
0} {$i < $n} {incr i
} {
48 ### List procedures ###
50 proc first
{list} {lindex $list 0}
51 proc rest
{list} {lrange $list 1 end
}
52 proc last
{list} {lindex $list end
}