From d993464422e850689f73a6f7c35ae980ca3823d0 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Thu, 5 Oct 2017 12:46:42 -0300 Subject: luarocks.fun: a basic set of functional utilities Move `array_contains` away from the `luarocks.util` kitchen sink, and add a `luarocks.fun` module with some basic functional utilities. I considered using luafun instead, but at this point the basic functionality of LuaRocks has no mandatory external dependencies, so I thought I'd just add these functions internally instead. --- src/luarocks/build.lua | 3 ++- src/luarocks/fun.lua | 40 ++++++++++++++++++++++++++++++++++++++++ src/luarocks/tools/patch.lua | 10 +++++----- src/luarocks/util.lua | 9 --------- 4 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 src/luarocks/fun.lua (limited to 'src') diff --git a/src/luarocks/build.lua b/src/luarocks/build.lua index 860cbdc1..74135379 100644 --- a/src/luarocks/build.lua +++ b/src/luarocks/build.lua @@ -3,6 +3,7 @@ local build = {} local path = require("luarocks.path") local util = require("luarocks.util") +local fun = require("luarocks.fun") local fetch = require("luarocks.fetch") local fs = require("luarocks.fs") local dir = require("luarocks.dir") @@ -254,7 +255,7 @@ function build.build_rockspec(rockspec_file, need_to_fetch, minimal_mode, deps_m build_spec.type = "builtin" end - if cfg.accepted_build_types and util.array_contains(cfg.accepted_build_types, build_spec.type) then + if cfg.accepted_build_types and fun.contains(cfg.accepted_build_types, build_spec.type) then return nil, "This rockspec uses the '"..build_spec.type.."' build type, which is blocked by the 'accepted_build_types' setting in your LuaRocks configuration." end diff --git a/src/luarocks/fun.lua b/src/luarocks/fun.lua new file mode 100644 index 00000000..e8e0c6bb --- /dev/null +++ b/src/luarocks/fun.lua @@ -0,0 +1,40 @@ + +--- A set of basic functional utilities +local fun = {} + +function fun.concat(xs, ys) + local rs = {} + local n = #xs + for i = 1, n do + rs[i] = xs[i] + end + for i = 1, #ys do + rs[i + n] = ys[i] + end + return rs +end + +function fun.contains(xs, v) + for _, x in ipairs(xs) do + if v == x then + return true + end + end + return false +end + +function fun.map(f, xs) + local rs = {} + for i = 1, #xs do + rs[i] = f(xs[i]) + end + return rs +end + +function fun.traverse(f, t) + return fun.map(function(x) + return type(x) == "table" and fun.traverse(f, x) or f(x) + end, t) +end + +return fun diff --git a/src/luarocks/tools/patch.lua b/src/luarocks/tools/patch.lua index 30ed1419..2e95e879 100644 --- a/src/luarocks/tools/patch.lua +++ b/src/luarocks/tools/patch.lua @@ -11,7 +11,7 @@ local patch = {} local fs = require("luarocks.fs") -local util = require("luarocks.util") +local fun = require("luarocks.fun") local io = io local os = os @@ -284,7 +284,7 @@ function patch.read_patch(filename, data) local advance if state == 'filenames' then if startswith(line, "--- ") then - if util.array_contains(files.source, nextfileno) then + if fun.contains(files.source, nextfileno) then all_ok = false warning(format("skipping invalid patch for %s", files.source[nextfileno+1])) @@ -307,7 +307,7 @@ function patch.read_patch(filename, data) table.insert(files.source, match) end elseif not startswith(line, "+++ ") then - if util.array_contains(files.source, nextfileno) then + if fun.contains(files.source, nextfileno) then all_ok = false warning(format("skipping invalid patch with no target for %s", files.source[nextfileno+1])) @@ -318,7 +318,7 @@ function patch.read_patch(filename, data) end state = 'header' else - if util.array_contains(files.target, nextfileno) then + if fun.contains(files.target, nextfileno) then all_ok = false warning(format("skipping invalid patch - double target at line %d", lineno+1)) @@ -360,7 +360,7 @@ function patch.read_patch(filename, data) if not advance and state == 'hunkhead' then local m1, m2, m3, m4 = match_linerange(line) if not m1 then - if not util.array_contains(files.hunks, nextfileno-1) then + if not fun.contains(files.hunks, nextfileno-1) then all_ok = false warning(format("skipping invalid patch with no hunks for file %s", files.target[nextfileno])) diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index 2c4724f4..59173449 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua @@ -442,15 +442,6 @@ function util.get_default_rockspec() end end -function util.array_contains(tbl, value) - for _, v in ipairs(tbl) do - if v == value then - return true - end - end - return false -end - -- Quote Lua string, analogous to fs.Q. -- @param s A string, such as "hello" -- @return string: A quoted string, such as '"hello"' -- cgit v1.2.3-55-g6feb