iter.lua - gsl-shell.git - gsl-shell

index : gsl-shell.git
gsl-shell
summary refs log tree commit diff
path: root/iter.lua
blob: 06d4618a339ce3b47f207406d7a37d5598d384cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165

 -- base.lua
 -- 
 -- Copyright (C) 2009-2011 Francesco Abbate
 -- 
 -- This program is free software; you can redistribute it and/or modify
 -- it under the terms of the GNU General Public License as published by
 -- the Free Software Foundation; either version 3 of the License, or (at
 -- your option) any later version.
 -- 
 -- This program is distributed in the hope that it will be useful, but
 -- WITHOUT ANY WARRANTY; without even the implied warranty of
 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 -- General Public License for more details.
 -- 
 -- You should have received a copy of the GNU General Public License
 -- along with this program; if not, write to the Free Software
 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 --
local cat = table.concat
local fmt = string.format
do
 local ffi = require('ffi')
 local reg = debug.getregistry()
 reg.__gsl_ffi_types = {}
 function reg.__gsl_reg_ffi_type(ctype, name)
 local t = reg.__gsl_ffi_types
 t[#t + 1] = {ctype, name}
 end
 gsl_type = function(obj)
 local s = reg.__gsl_type(obj)
 if s == "cdata" then
 for _, item in ipairs(reg.__gsl_ffi_types) do
 local ctype, name = unpack(item)
 if ffi.istype(ctype, obj) then return name end
 end
 end
 return s
 end
end
function math.divmod(n, p)
 local r = n % p
 return (n-r)/p, r
end
local tos
local function key_tos(k)
 if type(k) == 'string' then
 return string.match(k, "[%a_][%a%d_]*") and k or fmt('[%q]', k)
 else
 return '[' .. tos(k) .. ']'
 end
end
local max_depth = 3
tos = function (t, depth)
 local tp = type(t)
 if tp == 'table' then
 if depth >= max_depth then return fmt('<table: %p>', t) end
 local ls, n = {}, #t
 local skip = {}
 for i, v in ipairs(t) do 
 skip[i] = true
 ls[i] = tos(v, depth + 1)
 end
 for k, v in pairs(t) do
 if not skip[k] then
 ls[#ls+1] = key_tos(k, 1) .. '= ' .. tos(v, depth + 1)
 end
 end
 return '{' .. cat(ls, ', ') .. '}'
 elseif tp == 'function' then
 return '<function>'
 elseif tp == 'string' then
 return (depth == 0 and t or fmt('%q', t))
 elseif tp == 'userdata' then
 local mt = getmetatable(t)
 local ftostr = mt and mt.__tostring
 if ftostr then return ftostr(t) else
 if gsl_type then
 return fmt('<%s: %p>', gsl_type(t), t)
 else
 return fmt('<userdata: %p>', t)
 end
 end
 elseif tp == 'cdata' then
 local tpext = gsl_type and gsl_type(t) or tp
 if tpext ~= 'cdata' and t.show then
 return (depth == 0 and t:show() or fmt("<%s: %p>", tpext, t))
 end
 end
 return tostring(t)
end
local function myprint(...)
 local n = select('#', ...)
 for i=1, n do
 if i > 1 then io.write(', ') end
 io.write(tos(select(i, ...), 0))
 end
 io.write('\n')
end
print = myprint
local function sequence(f, a, b)
 a, b = (b and a or 1), (b and b or a)
 local k = a
 return function()
 if k <= b then
 k = k+1
 return f(k-1)
 end
 end
end
-- take the function f and return an iterator that gives the couple (x, f(x))
-- for x going from 'xi' to 'xs' with n sampling points
local function sample(f, xi, xs, n)
 local c = (xs-xi)/n
 local k = 0
 return function()
 if k <= n then
 local x = xi+k*c
 k = k+1
 return x, f(x)
 end
 end
end
local function ilist(f, a, b)
 a, b = (b and a or 1), (b and b or a)
 local ls = {}
 for i = a, b do ls[i] = f(i) end
 return ls
end
local function isample(f, a, b)
 return sequence(function(i) return i, f(i) end, a, b)
end
local function isum(f, a, b)
 a, b = (b and a or 1), (b and b or a)
 local s = 0
 for k = a, b do s = s + f(k) end
 return s
end
iter = {
 sequence = sequence,
 sample = sample,
 ilist = ilist,
 isample = isample,
 isum = isum,
}
return iter
generated by cgit v1.2.3 (git 2.39.1) at 2025年09月26日 10:09:25 +0000

AltStyle によって変換されたページ (->オリジナル) /