Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions src/Data/Traversable.lua
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
local array1 = function(a) return {a} end
local array2 = function(a) return function(b) return {a, b} end end
local array3 = function(a) return function(b) return function(c) return {a, b, c} end end end
local concat2 = function(xs) return function(ys) return table.concat(xs, ys) end end
return {
traverseArrayImpl = (function(apply)
return function(map)
return function(pure)
return function(f)
return function(array)
local function go(bot, top)
if top - bot == 0 then
return pure({})
elseif top - bot == 1 then
return map(array1)(f(array[bot]))
elseif top - bot == 2 then
return apply(map(array2)(f(array[bot])))(f(array[bot + 1]))
elseif top - bot == 3 then
return apply(apply(map(array3)(f(array[bot])))(f(array[bot + 1])))(f(array[bot + 2]))
else
-- This slightly tricky pivot selection aims to produce two
-- even-length partitions where possible.
local pivot = bot + math.floor((top - bot) / 4) * 2
return apply(map(concat2)(go(bot, pivot)))(go(pivot, top))
return function (appendArrays)
return function(f)
return function(array)
local function go(bot, top)
if top - bot == 0 then
return pure({})
elseif top - bot == 1 then
return map(array1)(f(array[bot + 1]))
elseif top - bot == 2 then
return apply(map(array2)(f(array[bot + 1])))(f(array[bot + 2]))
elseif top - bot == 3 then
return apply(apply(map(array3)(f(array[bot + 1])))(f(array[bot + 2])))(f(array[bot + 3]))
else
-- This slightly tricky pivot selection aims to produce two
-- even-length partitions where possible.
local pivot = bot + math.floor((top - bot) / 4) * 2
return apply(map(appendArrays)(go(bot, pivot)))(go(pivot, top))
end
end
end

return go(0, #array)
return go(0, #array)
end
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion src/Data/Traversable.purs
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,15 @@ sequenceDefault
sequenceDefault = traverse identity

instance traversableArray :: Traversable Array where
traverse = traverseArrayImpl apply map pure
traverse = traverseArrayImpl apply map pure (<>)
sequence = sequenceDefault

foreign import traverseArrayImpl
:: forall m a b
. (forall x y. m (x -> y) -> m x -> m y)
-> (forall x y. (x -> y) -> m x -> m y)
-> (forall x. x -> m x)
-> (forall x. Array x -> Array x -> Array x)
-> (a -> m b)
-> Array a
-> m (Array b)
Expand Down