From 42ec7286b2d36a9ba22925f816a17cb1cc2aa5ce Mon Sep 17 00:00:00 2001 From: chai Date: Sat, 30 Oct 2021 11:32:16 +0800 Subject: + Penlight --- .../Penlight/docs/examples/seesubst.lua.html | 174 ++++++++++ .../Penlight/docs/examples/sipscan.lua.html | 161 +++++++++ .../Penlight/docs/examples/symbols.lua.html | 347 +++++++++++++++++++ .../Penlight/docs/examples/test-cmp.lua.html | 130 +++++++ .../Penlight/docs/examples/test-data.lua.html | 372 +++++++++++++++++++++ .../docs/examples/test-listcallbacks.lua.html | 138 ++++++++ .../Penlight/docs/examples/test-pretty.lua.html | 140 ++++++++ .../Penlight/docs/examples/test-symbols.lua.html | 209 ++++++++++++ .../Penlight/docs/examples/testapp.lua.html | 133 ++++++++ .../Penlight/docs/examples/testclone.lua.html | 165 +++++++++ .../Penlight/docs/examples/testconfig.lua.html | 176 ++++++++++ .../Penlight/docs/examples/testglobal.lua.html | 153 +++++++++ .../docs/examples/testinputfields.lua.html | 140 ++++++++ .../docs/examples/testinputfields2.lua.html | 136 ++++++++ .../Penlight/docs/examples/testxml.lua.html | 209 ++++++++++++ .../Penlight/docs/examples/which.lua.html | 155 +++++++++ 16 files changed, 2938 insertions(+) create mode 100644 Data/Libraries/Penlight/docs/examples/seesubst.lua.html create mode 100644 Data/Libraries/Penlight/docs/examples/sipscan.lua.html create mode 100644 Data/Libraries/Penlight/docs/examples/symbols.lua.html create mode 100644 Data/Libraries/Penlight/docs/examples/test-cmp.lua.html create mode 100644 Data/Libraries/Penlight/docs/examples/test-data.lua.html create mode 100644 Data/Libraries/Penlight/docs/examples/test-listcallbacks.lua.html create mode 100644 Data/Libraries/Penlight/docs/examples/test-pretty.lua.html create mode 100644 Data/Libraries/Penlight/docs/examples/test-symbols.lua.html create mode 100644 Data/Libraries/Penlight/docs/examples/testapp.lua.html create mode 100644 Data/Libraries/Penlight/docs/examples/testclone.lua.html create mode 100644 Data/Libraries/Penlight/docs/examples/testconfig.lua.html create mode 100644 Data/Libraries/Penlight/docs/examples/testglobal.lua.html create mode 100644 Data/Libraries/Penlight/docs/examples/testinputfields.lua.html create mode 100644 Data/Libraries/Penlight/docs/examples/testinputfields2.lua.html create mode 100644 Data/Libraries/Penlight/docs/examples/testxml.lua.html create mode 100644 Data/Libraries/Penlight/docs/examples/which.lua.html (limited to 'Data/Libraries/Penlight/docs/examples') diff --git a/Data/Libraries/Penlight/docs/examples/seesubst.lua.html b/Data/Libraries/Penlight/docs/examples/seesubst.lua.html new file mode 100644 index 0000000..e850122 --- /dev/null +++ b/Data/Libraries/Penlight/docs/examples/seesubst.lua.html @@ -0,0 +1,174 @@ + + + + + Penlight Documentation + + + + +
+ +
+ +
+
+
+ + +
+ + + + + + +
+ +

seesubst.lua

+
+-- shows how replacing '@see module' in the Markdown documentation
+-- can be done more elegantly using PL.
+-- We either have something like 'pl.config' (a module reference)
+-- or 'pl.seq.map' (a function reference); these cases must be distinguished
+-- and a Markdown link generated pointing to the LuaDoc file.
+
+local sip = require 'pl.sip'
+local stringx = require 'pl.stringx'
+
+local res = {}
+local s = [[
+(@see pl.bonzo.dog)
+remember about @see pl.bonzo
+
+]]
+
+local _gsub_patterns = {}
+
+local function gsub (s,pat,subst,start)
+    local fpat = _gsub_patterns[pat]
+    if not fpat then
+        -- use SIP to generate a proper string pattern.
+        -- the _whole thing_ is a capture, to get the whole match
+        -- and the unnamed capture.
+        fpat = '('..sip.create_pattern(pat)..')'
+        _gsub_patterns[pat] = fpat
+    end
+    return s:gsub(fpat,subst,start)
+end
+
+
+local mod = sip.compile '$v.$v'
+local fun = sip.compile '$v.$v.$v'
+
+for line in stringx.lines(s) do
+    line = gsub(line,'@see $p',function(see,path)
+        if fun(path,res) or mod(path,res) then
+            local ret = ('[see %s](%s.%s.html'):format(path,res[1],res[2])
+            if res[3] then
+                return ret..'#'..res[3]..')'
+            else
+                return ret..')'
+            end
+        end
+    end)
+    print(line)
+end
+ + +
+
+
+generated by LDoc 1.4.6 +
+
+ + diff --git a/Data/Libraries/Penlight/docs/examples/sipscan.lua.html b/Data/Libraries/Penlight/docs/examples/sipscan.lua.html new file mode 100644 index 0000000..8848423 --- /dev/null +++ b/Data/Libraries/Penlight/docs/examples/sipscan.lua.html @@ -0,0 +1,161 @@ + + + + + Penlight Documentation + + + + +
+ +
+ +
+
+
+ + +
+ + + + + + +
+ +

sipscan.lua

+
+-- another SIP example, shows how an awkward log file format
+-- can be parsed. It also prints out the actual Lua string
+-- pattern generated:
+-- SYNC%s*%[([+%-%d]%d*)%]%s*([+%-%d]%d*)%s*([+%-%d]%d*)
+
+local sip = require 'pl.sip'
+local stringx = require 'pl.stringx'
+
+local s = [[
+SYNC [1] 0 547 (14679 sec)
+SYNC [2] 0 555 (14679 sec)
+SYNC [3] 0 563 (14679 sec)
+SYNC [4] 0 571 (14679 sec)
+SYNC [5] -1 580 (14679 sec)
+SYNC [6] 0 587 (14679 sec)
+]]
+
+
+local first = true
+local expected
+local res = {}
+local pat = 'SYNC [$i{seq}] $i{diff} $i{val}'
+print(sip.create_pattern(pat))
+local match = sip.compile(pat)
+for line in stringx.lines(s) do
+  if match(line,res) then
+    if first then
+      expected = res.val
+      first = false
+    end
+    print(res.val,expected - res.val)
+    expected = expected + 8
+  end
+end
+ + +
+
+
+generated by LDoc 1.4.6 +
+
+ + diff --git a/Data/Libraries/Penlight/docs/examples/symbols.lua.html b/Data/Libraries/Penlight/docs/examples/symbols.lua.html new file mode 100644 index 0000000..0dafb21 --- /dev/null +++ b/Data/Libraries/Penlight/docs/examples/symbols.lua.html @@ -0,0 +1,347 @@ + + + + + Penlight Documentation + + + + +
+ +
+ +
+
+
+ + +
+ + + + + + +
+ +

symbols.lua

+
+require 'pl'
+utils.import 'pl.func'
+local ops = require 'pl.operator'
+local List = require 'pl.List'
+local append,concat = table.insert,table.concat
+local compare,find_if,compare_no_order,imap,reduce,count_map = tablex.compare,tablex.find_if,tablex.compare_no_order,tablex.imap,tablex.reduce,tablex.count_map
+local unpack = table.unpack
+
+function bindval (self,val)
+    rawset(self,'value',val)
+end
+
+local optable = ops.optable
+
+function sexpr (e)
+	if isPE(e) then
+		if e.op ~= 'X' then
+			local args = tablex.imap(sexpr,e)
+			return '('..e.op..' '..table.concat(args,' ')..')'
+		else
+			return e.repr
+		end
+	else
+		return tostring(e)
+	end
+end
+
+
+psexpr = compose(print,sexpr)
+
+
+
+function equals (e1,e2)
+    local p1,p2 = isPE(e1),isPE(e2)
+    if p1 ~= p2 then return false end  -- different kinds of animals!
+    if p1 and p2 then -- both PEs
+        -- operators must be the same
+        if e1.op ~= e2.op then return false end
+        -- PHs are equal if their representations are equal
+        if e1.op == 'X' then return e1.repr == e2.repr
+        -- commutative operators
+        elseif e1.op == '+' or e1.op == '*' then
+            return compare_no_order(e1,e2,equals)
+        else
+            -- arguments must be the same
+            return compare(e1,e2,equals)
+        end
+    else -- fall back on simple equality for non PEs
+        return e1 == e2
+    end
+end
+
+-- run down an unbalanced operator chain (like a+b+c) and return the arguments {a,b,c}
+function tcollect (op,e,ls)
+    if isPE(e) and e.op == op then
+        for i = 1,#e do
+            tcollect(op,e[i],ls)
+        end
+    else
+        ls:append(e)
+        return
+    end
+end
+
+function rcollect (e)
+    local res = List()
+    tcollect(e.op,e,res)
+    return res
+end
+
+
+-- balance ensures that +/* chains are collected together, operates in-place.
+-- thus (+(+ a b) c) or (+ a (+ b c)) becomes (+ a b c), order immaterial
+function balance (e)
+    if isPE(e) and e.op ~= 'X' then
+        local op,args = e.op
+        if op == '+' or op == '*' then
+            args = rcollect(e)
+        else
+            args = imap(balance,e)
+        end
+        for i = 1,#args do
+            e[i] = args[i]
+        end
+    end
+    return e
+end
+
+-- fold constants in an expression
+function fold (e)
+    if isPE(e) then
+        if e.op == 'X' then
+            -- there could be _bound values_!
+            local val = rawget(e,'value')
+            return val and val or e
+        else
+            local op = e.op
+            local addmul = op == '*' or op == '+'
+            -- first fold all arguments
+            local args = imap(fold,e)
+            if not addmul and not find_if(args,isPE) then
+                -- no placeholders in these args, we can fold the expression.
+                local opfn = optable[op]
+                if opfn then
+                    return opfn(unpack(args))
+                else
+                    return '?'
+                end
+            elseif addmul then
+                -- enforce a few rules for + and *
+                -- split the args into two classes, PE args and non-PE args.
+                local classes = List.partition(args,isPE)
+                local pe,npe = classes[true],classes[false]
+                if npe then -- there's at least one non PE argument
+                    -- so fold them
+                    if #npe == 1 then npe = npe[1]
+                    else npe = npe:reduce(optable[op])
+                    end
+                    -- if the result is a constant, return it
+                    if not pe then return npe end
+
+                    -- either (* 1 x) => x or (* 1 x y ...) => (* x y ...)
+                    if op == '*' then
+                        if npe == 0 then return 0
+                        elseif npe == 1 then -- identity
+                            if #pe == 1 then return pe[1] else npe = nil end
+                        end
+                    else -- special cases for +
+                        if npe == 0 then -- identity
+                            if #pe == 1 then return pe[1] else npe = nil end
+                        end
+                    end
+                end
+                -- build up the final arguments
+                local res = {}
+                if npe then append(res,npe) end
+                for val,count in pairs(count_map(pe,equals)) do
+                    if count > 1 then
+                        if op == '*' then val = val ^ count
+                        else val = val * count
+                        end
+                    end
+                    append(res,val)
+                end
+                if #res == 1 then return res[1] end
+                return PE{op=op,unpack(res)}
+            elseif op == '^' then
+                if args[2] == 1 then return args[1] end -- identity
+                if args[2] == 0 then return 1 end
+            end
+            return PE{op=op,unpack(args)}
+        end
+    else
+        return e
+    end
+end
+
+function expand (e)
+    if isPE(e) and e.op == '*' and isPE(e[2]) and e[2].op == '+' then
+        local a,b = e[1],e[2]
+        return expand(b[1]*a) + expand(b[2]*a)
+    else
+        return e
+    end
+end
+
+function isnumber (x)
+    return type(x) == 'number'
+end
+
+-- does this PE contain a reference to x?
+function references (e,x)
+    if isPE(e) then
+        if e.op == 'X' then return x.repr == e.repr
+        else
+            return find_if(e,references,x)
+        end
+    else
+        return false
+    end
+end
+
+local function muli (args)
+    return PE{op='*',unpack(args)}
+end
+
+local function addi (args)
+    return PE{op='+',unpack(args)}
+end
+
+function diff (e,x)
+    if isPE(e) and references(e,x) then
+        local op = e.op
+        if op == 'X' then
+            return 1
+        else
+            local a,b = e[1],e[2]
+            if op == '+' then -- differentiation is linear
+                local args = imap(diff,e,x)
+                return balance(addi(args))
+            elseif op == '*' then -- product rule
+                local res,d,ee = {}
+                for i = 1,#e do
+                    d = fold(diff(e[i],x))
+                    if d ~= 0 then
+                        ee = {unpack(e)}
+                        ee[i] = d
+                        append(res,balance(muli(ee)))
+                    end
+                end
+                if #res > 1 then return addi(res)
+                else return res[1] end
+            elseif op == '^' and isnumber(b) then -- power rule
+                return b*x^(b-1)
+            end
+        end
+    else
+        return 0
+    end
+end
+ + +
+
+
+generated by LDoc 1.4.6 +
+
+ + diff --git a/Data/Libraries/Penlight/docs/examples/test-cmp.lua.html b/Data/Libraries/Penlight/docs/examples/test-cmp.lua.html new file mode 100644 index 0000000..ac6e44a --- /dev/null +++ b/Data/Libraries/Penlight/docs/examples/test-cmp.lua.html @@ -0,0 +1,130 @@ + + + + + Penlight Documentation + + + + +
+ +
+ +
+
+
+ + + +
+generated by LDoc 1.4.6 +
+
+ + diff --git a/Data/Libraries/Penlight/docs/examples/test-data.lua.html b/Data/Libraries/Penlight/docs/examples/test-data.lua.html new file mode 100644 index 0000000..8128411 --- /dev/null +++ b/Data/Libraries/Penlight/docs/examples/test-data.lua.html @@ -0,0 +1,372 @@ + + + + + Penlight Documentation + + + + +
+ +
+ +
+
+
+ + +
+ + + + + + +
+ +

test-data.lua

+
+local data = require 'pl.data'
+local List = require 'pl.List'
+local array = require 'pl.array2d'
+local func = require 'pl.func'
+local seq = require 'pl.seq'
+local stringio = require 'pl.stringio'
+local open = stringio. open
+local asserteq = require 'pl.test' . asserteq
+local T = require 'pl.test'. tuple
+
+--[=[
+dat,err = data.read(open [[
+1.0 0.1
+0.2 1.3
+]])
+
+if err then print(err) end
+
+require 'pl.pretty'.dump(dat)
+os.exit(0)
+--]=]
+
+-- tab-separated data, explicit column names
+local t1f = open [[
+EventID	Magnitude	LocationX	LocationY	LocationZ	LocationError	EventDate	DataFile
+981124001	2.0	18988.4	10047.1	4149.7	33.8	24/11/1998 11:18:05	981124DF.AAB
+981125001	0.8	19104.0	9970.4	5088.7	3.0	25/11/1998 05:44:54	981125DF.AAB
+981127003	0.5	19012.5	9946.9	3831.2	46.0	27/11/1998 17:15:17	981127DF.AAD
+981127005	0.6	18676.4	10606.2	3761.9	4.4	27/11/1998 17:46:36	981127DF.AAF
+981127006	0.2	19109.9	9716.5	3612.0	11.8	27/11/1998 19:29:51	981127DF.AAG
+]]
+
+local t1 = data.read (t1f)
+-- column_by_name returns a List
+asserteq(t1:column_by_name 'Magnitude',List{2,0.8,0.5,0.6,0.2})
+-- can use array.column as well
+asserteq(array.column(t1,2),{2,0.8,0.5,0.6,0.2})
+
+-- only numerical columns (deduced from first data row) are converted by default
+-- can look up indices in the list fieldnames.
+local EDI = t1.fieldnames:index 'EventDate'
+assert(type(t1[1][EDI]) == 'string')
+
+-- select method returns a sequence, in this case single-valued.
+-- (Note that seq.copy returns a List)
+asserteq(seq(t1:select 'LocationX where Magnitude > 0.5'):copy(),List{18988.4,19104,18676.4})
+
+--[[
+--a common select usage pattern:
+for event,mag in t1:select 'EventID,Magnitude sort by Magnitude desc' do
+    print(event,mag)
+end
+--]]
+
+-- space-separated, but with last field containing spaces.
+local t2f = open [[
+USER PID %MEM %CPU COMMAND
+sdonovan 2333  0.3 0.1 background --n=2
+root 2332  0.4  0.2 fred --start=yes
+root 2338  0.2  0.1 backyard-process
+]]
+
+local t2,err = data.read(t2f,{last_field_collect=true})
+if not t2 then return print (err) end
+
+-- the last_field_collect option is useful with space-delimited data where the last
+-- field may contain spaces. Otherwise, a record count mismatch should be an error!
+local lt2 = List(t2[2])
+asserteq(lt2:join ',','root,2332,0.4,0.2,fred --start=yes')
+
+-- fieldnames are converted into valid identifiers by substituting _
+-- (we do this to make select queries parseable by Lua)
+asserteq(t2.fieldnames,List{'USER','PID','_MEM','_CPU','COMMAND'})
+
+-- select queries are NOT SQL so remember to use == ! (and no 'between' operator, sorry)
+--s,err = t2:select('_MEM where USER="root"')
+--assert(err == [[[string "tmp"]:9: unexpected symbol near '=']])
+
+local s = t2:select('_MEM where USER=="root"')
+assert(s() == 0.4)
+assert(s() == 0.2)
+assert(s() == nil)
+
+-- CSV, Excel style. Double-quoted fields are allowed, and they may contain commas!
+local t3f = open [[
+"Department Name","Employee ID",Project,"Hours Booked"
+sales,1231,overhead,4
+sales,1255,overhead,3
+engineering,1501,development,5
+engineering,1501,maintenance,3
+engineering,1433,maintenance,10
+]]
+
+local t3 = data.read(t3f,{csv=true})
+
+-- although fieldnames are turned in valid Lua identifiers, there is always original_fieldnames
+asserteq(t3.fieldnames,List{'Department_Name','Employee_ID','Project','Hours_Booked'})
+asserteq(t3.original_fieldnames,List{'Department Name','Employee ID','Project','Hours Booked'})
+
+-- a common operation is to select using a given list of columns, and each row
+-- on some explicit condition. The select() method can take a table with these
+-- parameters
+local keepcols = {'Employee_ID','Hours_Booked'}
+
+local q = t3:select { fields = keepcols,
+    where = function(row) return row[1]=='engineering' end
+    }
+
+asserteq(seq.copy2(q),{{1501,5},{1501,3},{1433,10}})
+
+-- another pattern is doing a select to restrict rows & columns, process some
+-- fields and write out the modified rows.
+
+local outf = stringio.create()
+
+local names = {[1501]='don',[1433]='dilbert'}
+
+t3:write_row (outf,{'Employee','Hours_Booked'})
+q = t3:select_row {fields=keepcols,where=func.Eq(func._1[1],'engineering')}
+for row in q do
+    row[1] = names[row[1]]
+    t3:write_row(outf,row)
+end
+
+asserteq(outf:value(),
+[[
+Employee,Hours_Booked
+don,5
+don,3
+dilbert,10
+]])
+
+-- data may not always have column headers. When creating a data object
+-- from a two-dimensional array, may specify the fieldnames, as a list or a string.
+-- The delimiter is deduced from the fieldname string, so a string just containing
+-- the delimiter will set it,  and the fieldnames will be empty.
+local dat = List()
+local row = List.range(1,10)
+for i = 1,10 do
+    dat:append(row:map('*',i))
+end
+dat = data.new(dat,',')
+local out = stringio.create()
+dat:write(out,',')
+asserteq(out:value(), [[
+1,2,3,4,5,6,7,8,9,10
+2,4,6,8,10,12,14,16,18,20
+3,6,9,12,15,18,21,24,27,30
+4,8,12,16,20,24,28,32,36,40
+5,10,15,20,25,30,35,40,45,50
+6,12,18,24,30,36,42,48,54,60
+7,14,21,28,35,42,49,56,63,70
+8,16,24,32,40,48,56,64,72,80
+9,18,27,36,45,54,63,72,81,90
+10,20,30,40,50,60,70,80,90,100
+]])
+
+-- you can always use numerical field indices, AWK-style;
+-- note how the copy_select method gives you a data object instead of an
+-- iterator over the fields
+local res = dat:copy_select '$1,$3 where $1 > 5'
+local L = List
+asserteq(L(res),L{
+    L{6, 18},
+    L{7,21},
+    L{8,24},
+    L{9,27},
+    L{10,30},
+})
+
+-- the column_by_name method may take a fieldname or an index
+asserteq(dat:column_by_name(2), L{2,4,6,8,10,12,14,16,18,20})
+
+-- the field list may contain expressions or even constants
+local q = dat:select '$3,2*$4 where $1 == 8'
+asserteq(T(q()),T(24,64))
+
+dat,err = data.read(open [[
+1.0 0.1
+0.2 1.3
+]])
+
+if err then print(err) end
+
+-- if a method cannot be found, then we look up in array2d
+-- array2d.flatten(t) makes a 1D list out of a 2D array,
+-- and then List.minmax() gets the extrema.
+
+asserteq(T(dat:flatten():minmax()),T(0.1,1.3))
+
+local f = open [[
+Time Message
+1266840760 +# EE7C0600006F0D00C00F06010302054000000308010A00002B00407B00
+1266840760 closure data 0.000000 1972 1972 0
+1266840760 ++ 1266840760 EE 1
+1266840760 +# EE7C0600006F0D00C00F06010302054000000408020A00002B00407B00
+1266840764 closure data 0.000000 1972 1972 0
+1266840764 ++ 1266840764 EE 1
+1266840764 +# EE7C0600006F0D00C00F06010302054000000508030A00002B00407B00
+1266840768 duplicate?
+1266840768 +# EE7C0600006F0D00C00F06010302054000000508030A00002B00407B00
+1266840768 closure data 0.000000 1972 1972 0
+]]
+
+-- the convert option provides custom converters for each specified column.
+-- Here we convert the timestamps into Date objects and collect everything
+-- else into one field
+local Date = require 'pl.Date'
+
+local function date_convert (ds)
+    return Date(tonumber(ds))
+end
+
+local d = data.read(f,{convert={[1]=date_convert},last_field_collect=true})
+
+asserteq(#d[1],2)
+asserteq(d[2][1]:year(),2010)
+
+d = {{1,2,3},{10,20,30}}
+out = stringio.create()
+data.write(d,out,{'A','B','C'},',')
+asserteq(out:value(),
+[[
+A,B,C
+1,2,3
+10,20,30
+]])
+
+out = stringio.create()
+d.fieldnames = {'A','B','C'}
+data.write(d,out)
+
+asserteq(out:value(),
+[[
+A	B	C
+1	2	3
+10	20	30
+]])
+
+
+d = data.read(stringio.open 'One,Two\n1,\n,20\n',{csv=true})
+asserteq(d,{
+    {1,0},{0,20},
+    original_fieldnames={"One","Two"},fieldnames={"One","Two"},delim=","
+})
+ + +
+
+
+generated by LDoc 1.4.6 +
+
+ + diff --git a/Data/Libraries/Penlight/docs/examples/test-listcallbacks.lua.html b/Data/Libraries/Penlight/docs/examples/test-listcallbacks.lua.html new file mode 100644 index 0000000..4416532 --- /dev/null +++ b/Data/Libraries/Penlight/docs/examples/test-listcallbacks.lua.html @@ -0,0 +1,138 @@ + + + + + Penlight Documentation + + + + +
+ +
+ +
+
+
+ + +
+ + + + + + +
+ +

test-listcallbacks.lua

+
+-- demonstrates how to use a list of callbacks
+local List = require 'pl.List'
+local utils = require 'pl.utils'
+local actions = List()
+local L = utils.string_lambda
+
+actions:append(function() print 'hello' end)
+actions:append(L '|| print "yay"')
+
+-- '()' is a shortcut for operator.call or function(x) return x() end
+actions:foreach '()'
+ + +
+
+
+generated by LDoc 1.4.6 +
+
+ + diff --git a/Data/Libraries/Penlight/docs/examples/test-pretty.lua.html b/Data/Libraries/Penlight/docs/examples/test-pretty.lua.html new file mode 100644 index 0000000..a7493f3 --- /dev/null +++ b/Data/Libraries/Penlight/docs/examples/test-pretty.lua.html @@ -0,0 +1,140 @@ + + + + + Penlight Documentation + + + + +
+ +
+ +
+
+
+ + +
+ + + + + + +
+ +

test-pretty.lua

+
+local pretty = require 'pl.pretty'
+
+local tb = {
+    'one','two','three',{1,2,3},
+    alpha=1,beta=2,gamma=3,['&']=true,[0]=false,
+    _fred = {true,true},
+    s = [[
+hello dolly
+you're so fine
+]]
+}
+
+print(pretty.write(tb))
+ + +
+
+
+generated by LDoc 1.4.6 +
+
+ + diff --git a/Data/Libraries/Penlight/docs/examples/test-symbols.lua.html b/Data/Libraries/Penlight/docs/examples/test-symbols.lua.html new file mode 100644 index 0000000..a847ab9 --- /dev/null +++ b/Data/Libraries/Penlight/docs/examples/test-symbols.lua.html @@ -0,0 +1,209 @@ + + + + + Penlight Documentation + + + + +
+ +
+ +
+
+
+ + +
+ + + + + + +
+ +

test-symbols.lua

+
+require 'pl'
+-- force us to look in the script's directory when requiring...
+app.require_here()
+require 'symbols'
+
+local MT = getmetatable(_1)
+
+add = MT.__add
+mul = MT.__mul
+pow = MT.__pow
+
+
+function testeq (e1,e2)
+    if not equals(e1,e2) then
+        print ('Not equal',repr(e1),repr(e2))
+    end
+end
+
+sin = register(math.sin,'sin')
+
+f = register(function(x,y,z) end)
+
+--[[
+testeq (_1,_1)
+testeq (_1+_2,_1+_2)
+testeq (_1 + 3*_2,_1 + 3*_2)
+testeq (_2+_1,_1+_2)
+testeq (sin(_1),sin(_1))
+testeq (1+f(10,20,'ok'),f(10,20,'ok')+1)
+--]]
+
+
+function testexpand (e)
+    print(repr(fold(expand(e)))) --fold
+end
+
+--[[
+testexpand (a*(a+1))
+
+testexpand ((x+2)*(b+1))
+]]--
+
+function testfold (e)
+    print(repr(fold(e)))
+end
+
+a,b,c,x,y = Var 'a,b,c,x,y'
+
+--~ testfold(_1 + _2)
+--~ testfold(add(10,20))
+--~ testfold(add(mul(2,_1),mul(3,_2)))
+--[[
+testfold(sin(a))
+e = a^(b+2)
+testfold(e)
+bindval(b,1)
+testfold(e)
+bindval(a,2)
+testfold(e)
+
+bindval(a)
+bindval(b)
+]]
+
+
+
+function testdiff (e)
+    balance(e)
+    e = diff(e,x)
+    balance(e)
+    print('+ ',e)
+    e = fold(e)
+    print('- ',e)
+end
+
+
+testdiff(x^2+1)
+testdiff(3*x^2)
+testdiff(x^2 + 2*x^3)
+testdiff(x^2 + 2*a*x^3 + x^4)
+testdiff(2*a*x^3)
+testdiff(x*x*x)
+ + +
+
+
+generated by LDoc 1.4.6 +
+
+ + diff --git a/Data/Libraries/Penlight/docs/examples/testapp.lua.html b/Data/Libraries/Penlight/docs/examples/testapp.lua.html new file mode 100644 index 0000000..56ae5a3 --- /dev/null +++ b/Data/Libraries/Penlight/docs/examples/testapp.lua.html @@ -0,0 +1,133 @@ + + + + + Penlight Documentation + + + + +
+ +
+ +
+
+
+ + +
+ + + + + + +
+ +

testapp.lua

+
+-- shows how a script can get a private file path
+-- the output on my Windows machine is:
+-- C:\Documents and Settings\steve\.testapp\test.txt
+local app = require 'pl.app'
+print(app.appfile 'test.txt')
+ + +
+
+
+generated by LDoc 1.4.6 +Last updated 2018-11-23 21:07:42 +
+
+ + diff --git a/Data/Libraries/Penlight/docs/examples/testclone.lua.html b/Data/Libraries/Penlight/docs/examples/testclone.lua.html new file mode 100644 index 0000000..1e48839 --- /dev/null +++ b/Data/Libraries/Penlight/docs/examples/testclone.lua.html @@ -0,0 +1,165 @@ + + + + + Penlight Documentation + + + + +
+ +
+ +
+
+
+ + +
+ + + + + + +
+ +

testclone.lua

+
+--cloning a directory tree.
+local lfs = require 'lfs'
+local path = require 'pl.path'
+local dir = require 'pl.dir'
+
+local p1 = [[examples]]
+local p2 = [[copy/of/examples]]
+
+if not path.isfile 'examples/testclone.lua' then
+	return print 'please run this in the penlight folder (below examples)'
+end
+
+-- make a copy of the examples folder
+dir.clonetree(p1,p2,dir.copyfile)
+
+assert(path.isdir 'copy')
+
+print '---'
+local t = os.time()
+print(lfs.touch('examples/testclone.lua',t,t+10))
+
+-- this should only update this file
+dir.clonetree(p1,p2,
+function(f1,f2)
+  local t1 = path.getmtime(f1)
+  local t2 = path.getmtime(f2)
+  --print(f1,t1,f2,t2)
+  if t1 > t2 then
+	dir.copyfile(f1,f2)
+	print(f1,f2,t1,t2)
+  end
+  return true
+end)
+
+-- and get rid of the whole copy directory, with subdirs
+dir.rmtree 'copy'
+
+assert(not path.exists 'copy')
+ + +
+
+
+generated by LDoc 1.4.6 +
+
+ + diff --git a/Data/Libraries/Penlight/docs/examples/testconfig.lua.html b/Data/Libraries/Penlight/docs/examples/testconfig.lua.html new file mode 100644 index 0000000..4ba947f --- /dev/null +++ b/Data/Libraries/Penlight/docs/examples/testconfig.lua.html @@ -0,0 +1,176 @@ + + + + + Penlight Documentation + + + + +
+ +
+ +
+
+
+ + +
+ + + + + + +
+ +

testconfig.lua

+
+local stringio = require 'pl.stringio'
+local config = require 'pl.config'
+
+local function dump(t,indent)
+    if type(t) == 'table' then
+        io.write(indent,'{\n')
+        local newindent = indent..'  '
+        for k,v in pairs(t) do
+            io.write(newindent,k,'=')
+            dump(v,indent)
+            io.write('\n')
+        end
+        io.write(newindent,'},\n')
+    else
+        io.write(indent,t,'(',type(t),')')
+    end
+end
+
+
+local function testconfig(test)
+    local f = stringio.open(test)
+    local c = config.read(f)
+    f:close()
+    dump(c,'  ')
+    print '-----'
+end
+
+testconfig [[
+ ; comment 2 (an ini file)
+[section!]
+bonzo.dog=20,30
+config_parm=here we go again
+depth = 2
+[another]
+felix="cat"
+]]
+
+testconfig [[
+# this is a more Unix-y config file
+fred = 1
+alice = 2
+home = /bonzo/dog/etc
+]]
+
+testconfig [[
+# this is just a set of comma-separated values
+1000,444,222
+44,555,224
+]]
+ + +
+
+
+generated by LDoc 1.4.6 +
+
+ + diff --git a/Data/Libraries/Penlight/docs/examples/testglobal.lua.html b/Data/Libraries/Penlight/docs/examples/testglobal.lua.html new file mode 100644 index 0000000..e05a1f8 --- /dev/null +++ b/Data/Libraries/Penlight/docs/examples/testglobal.lua.html @@ -0,0 +1,153 @@ + + + + + Penlight Documentation + + + + +
+ +
+ +
+
+
+ + +
+ + + + + + +
+ +

testglobal.lua

+
+-- very simple lexer program which looks at all identifiers in a Lua
+-- file and checks whether they're in the global namespace.
+-- At the end, we dump out the result of count_map, which will give us
+-- unique identifiers with their usage count.
+-- (an example of a program which itself needs to be careful about what
+-- goes into the global namespace)
+
+local utils = require 'pl.utils'
+local file = require 'pl.file'
+local lexer = require 'pl.lexer'
+local List = require 'pl.List'
+local pretty = require 'pl.pretty'
+local seq = require 'pl.seq'
+local path = require 'pl.path'
+
+utils.on_error 'quit'
+
+local txt = file.read(arg[1] or path.normpath('examples/testglobal.lua'))
+local globals = List()
+for t,v in lexer.lua(txt) do
+	if t == 'iden' and rawget(_G,v) then
+		globals:append(v)
+	end
+end
+
+pretty.dump(seq.count_map(globals))
+ + +
+
+
+generated by LDoc 1.4.6 +
+
+ + diff --git a/Data/Libraries/Penlight/docs/examples/testinputfields.lua.html b/Data/Libraries/Penlight/docs/examples/testinputfields.lua.html new file mode 100644 index 0000000..0fa63e4 --- /dev/null +++ b/Data/Libraries/Penlight/docs/examples/testinputfields.lua.html @@ -0,0 +1,140 @@ + + + + + Penlight Documentation + + + + +
+ +
+ +
+
+
+ + +
+ + + + + + +
+ +

testinputfields.lua

+
+local input = require 'pl.input'
+local sum = 0.0
+local count = 0
+local text = [[
+    981124001	2.0	18988.4	10047.1	4149.7
+    981125001	0.8	19104.0	9970.4	5088.7
+    981127003	0.5	19012.5	9946.9	3831.2
+]]
+for id,magn,x in input.fields(3,' ',text) do
+  sum = sum + x
+  count = count + 1
+end
+print('average x coord is ',sum/count)
+ + +
+
+
+generated by LDoc 1.4.6 +
+
+ + diff --git a/Data/Libraries/Penlight/docs/examples/testinputfields2.lua.html b/Data/Libraries/Penlight/docs/examples/testinputfields2.lua.html new file mode 100644 index 0000000..51a5acf --- /dev/null +++ b/Data/Libraries/Penlight/docs/examples/testinputfields2.lua.html @@ -0,0 +1,136 @@ + + + + + Penlight Documentation + + + + +
+ +
+ +
+
+
+ + +
+ + + + + + +
+ +

testinputfields2.lua

+
+local input = require 'pl.input'
+local seq = require 'pl.seq'
+local text = [[
+    981124001	2.0	18988.4	10047.1	4149.7
+    981125001	0.8	19104.0	9970.4	5088.7
+    981127003	0.5	19012.5	9946.9	3831.2
+]]
+local sum,count = seq.sum(input.fields ({3},' ',text))
+print(sum/count)
+ + +
+
+
+generated by LDoc 1.4.6 +
+
+ + diff --git a/Data/Libraries/Penlight/docs/examples/testxml.lua.html b/Data/Libraries/Penlight/docs/examples/testxml.lua.html new file mode 100644 index 0000000..c01fe3b --- /dev/null +++ b/Data/Libraries/Penlight/docs/examples/testxml.lua.html @@ -0,0 +1,209 @@ + + + + + Penlight Documentation + + + + +
+ +
+ +
+
+
+ + +
+ + + + + + +
+ +

testxml.lua

+
+-- an example showing 'pl.lexer' doing some serious work.
+-- The resulting Lua table is in the same LOM format used by luaexpat.
+-- This is (clearly) not a professional XML parser, so don't use it
+-- on your homework!
+
+local lexer = require 'pl.lexer'
+local pretty = require 'pl.pretty'
+
+local append = table.insert
+local skipws,expecting = lexer.skipws,lexer.expecting
+
+local function parse_element (tok,tag)
+	local tbl,t,v,attrib
+	tbl = {}
+	tbl.tag = tag  -- LOM 'tag' is the element tag
+	t,v = skipws(tok)
+	while v ~= '/' and v ~= '>' do
+		if t ~= 'iden' then error('expecting attribute identifier') end
+		attrib = v
+		expecting(tok,'=')
+		v = expecting(tok,'string')
+		-- LOM: 'attr' subtable contains attrib/value pairs and an ordered list of attribs
+		if not tbl.attr then tbl.attr = {} end
+		tbl.attr[attrib] = v
+		append(tbl.attr,attrib)
+		t,v = skipws(tok)
+	end
+	if v == '/' then
+		expecting(tok,'>')
+		return tbl
+	end
+	-- pick up element data
+	t,v = tok()
+	while true do
+		if t == '<' then
+			t,v = skipws(tok)
+			if t == '/' then -- element end tag
+				t,v = tok()
+				if t == '>' then return tbl end
+				if t == 'iden' and v == tag then
+					if tok() == '>' then return tbl end
+				end
+				error('expecting end tag '..tag)
+			else
+				append(tbl,parse_element(tok,v)) -- LOM: child elements added to table
+				t,v = skipws(tok)
+			end
+		else
+			append(tbl,v) -- LOM: text added to table
+			t,v = skipws(tok)
+		end
+	end
+end
+
+local function parse_xml (tok)
+	local t = skipws(tok)
+	local v
+	while t == '<' do
+		t,v = tok()
+		if t == '?' or t == '!' then
+			-- skip meta stuff and commentary
+			repeat t = tok() until t == '>'
+			t = expecting(tok,'<')
+		else
+			return parse_element(tok,v)
+		end
+	end
+end
+
+local s = [[
+<?xml version="1.0" encoding="UTF-8"?>
+<sensor name="closure-meter-2" id="7D7D0600006F0D00" loc="100,100,0" device="closure-meter" init="true">
+<detector name="closure-meter" phenomenon="closure" units="mm" id="1"
+    vmin="0" vmax="5000" device="closure-meter" calib="0,0;5000,5000"
+    sampling_interval="25000" measurement_interval="600000"
+/>
+</sensor>
+]]
+
+local tok = lexer.scan(s,nil,{space=false},{string=true})
+local res = parse_xml(tok)
+print(pretty.write(res))
+ + +
+
+
+generated by LDoc 1.4.6 +
+
+ + diff --git a/Data/Libraries/Penlight/docs/examples/which.lua.html b/Data/Libraries/Penlight/docs/examples/which.lua.html new file mode 100644 index 0000000..d5e8961 --- /dev/null +++ b/Data/Libraries/Penlight/docs/examples/which.lua.html @@ -0,0 +1,155 @@ + + + + + Penlight Documentation + + + + +
+ +
+ +
+
+
+ + +
+ + + + + + +
+ +

which.lua

+
+-- a simple implementation of the which command. This looks for
+-- the given file on the path. On windows, it will assume an extension
+-- of .exe if no extension is given.
+local List = require 'pl.List'
+local path = require 'pl.path'
+local app = require 'pl.app'
+
+local pathl = List.split(os.getenv 'PATH',path.dirsep)
+
+local function which (file)
+    local res = pathl:map(path.join,file)
+    res = res:filter(path.exists)
+    if res then return res[1] end
+end
+
+local _,lua = app.lua()
+local file = arg[1] or lua -- i.e. location of lua executable
+local try
+
+if not file then return print 'must provide a filename' end
+
+if path.extension(file) == '' and path.is_windows then
+    try = which(file..'.exe')
+else
+    try = which(file)
+end
+
+if try then print(try) else print 'cannot find on path' end
+ + +
+
+
+generated by LDoc 1.4.6 +
+
+ + -- cgit v1.1-26-g67d0