Created
March 13, 2011 07:45
-
-
Save arsenm/867959 to your computer and use it in GitHub Desktop.
I made some kind of mess
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Lua implementation of the curry function | |
-- Developed by tinylittlelife.org | |
-- released under the WTFPL (http://sam.zoy.org/wtfpl/) | |
-- curry(func, num_args) : take a function requiring a tuple for num_args arguments | |
-- and turn it into a series of 1-argument functions | |
-- e.g.: you have a function dosomething(a, b, c) | |
-- curried_dosomething = curry(dosomething, 3) -- we want to curry 3 arguments | |
-- curried_dosomething (a1) (b1) (c1) -- returns the result of dosomething(a1, b1, c1) | |
-- partial_dosomething1 = curried_dosomething (a_value) -- returns a function | |
-- partial_dosomething2 = partial_dosomething1 (b_value) -- returns a function | |
-- partial_dosomething2 (c_value) -- returns the result of dosomething(a_value, b_value, c_value) | |
function curry(func, num_args) | |
-- currying 2-argument functions seems to be the most popular application | |
num_args = num_args or 2 | |
-- helper | |
local function curry_h(argtrace, n) | |
if 0 == n then | |
-- reverse argument list and call function | |
return func(reverse(argtrace())) | |
else | |
-- "push" argument (by building a wrapper function) and decrement n | |
return function (onearg) | |
return curry_h(function () return onearg, argtrace() end, n - 1) | |
end | |
end | |
end | |
-- no sense currying for 1 arg or less | |
if num_args > 1 then | |
return curry_h(function () return end, num_args) | |
else | |
return func | |
end | |
end | |
-- reverse(...) : take some tuple and return a tuple of elements in reverse order | |
-- | |
-- e.g. "reverse(1,2,3)" returns 3,2,1 | |
function reverse(...) | |
--reverse args by building a function to do it, similar to the unpack() example | |
local function reverse_h(acc, v, ...) | |
if 0 == select('#', ...) then | |
return v, acc() | |
else | |
return reverse_h(function () return v, acc() end, ...) | |
end | |
end | |
-- initial acc is the end of the list | |
return reverse_h(function () return end, ...) | |
end | |
------------------------------------------------------------------------------------------- | |
function mapApply(fs, tbl) | |
local i = 1 | |
local ret = { } | |
for _, f in ipairs(fs) do | |
for _, v in ipairs(tbl) do | |
ret[i] = f(v) | |
i = i + 1 | |
end | |
end | |
return ret | |
end | |
function buildAllCombinations(f, ...) | |
local n = #{...} | |
local g = curry(f, n) | |
local function applyList(x, ...) | |
-- Need to put original curried function into list | |
local ret = mapApply({ g }, x) | |
for _, v in ipairs({...}) do | |
ret = mapApply(ret, v) | |
end | |
return ret | |
end | |
return applyList(unpack({...})) | |
end | |
allCriterion = { "sw93", "NewCriterion", "BH86", "Exact" } | |
useQuads = { true, false } | |
thetas = { 1.0, 0.9, 0.7, 0.5, 0.3 } | |
allowIncests = useQuads | |
treeRsizes = { 4.0, 8.0, 2.0, 1.0 } | |
function createNBodyCtxRaw(theta, rsize, crit, useQuad, allowIncest) | |
local rawCtx = { } | |
rawCtx.theta = theta | |
rawCtx.treeRSize = rsize | |
rawCtx.criterion = crit | |
rawCtx.useQuad = useQuad | |
rawCtx.allowIncest = allowIncest | |
return rawCtx | |
end | |
everything = buildAllCombinations(createNBodyCtxRaw, | |
thetas, | |
treeRsizes, | |
allCriterion, | |
useQuads, | |
allowIncests) | |
for _, v1 in ipairs(everything) do | |
print("-------") | |
table.foreach(v1, print) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment