Skip to content

Instantly share code, notes, and snippets.

@kgadek
Created January 22, 2014 01:24
Show Gist options
  • Save kgadek/8551919 to your computer and use it in GitHub Desktop.
Save kgadek/8551919 to your computer and use it in GitHub Desktop.
Projekt Sauron: kod zadania map-reduce: merge-sort.
-- =====================================================================================================================
-- MOCK INTERFEJSU DOSTĘPNEGO NA APPCE
-- Dzięki temu można "ot tak" wrzucić kod w interpreter i będzie działać
-- ---------------------------------------------------------------------------------------------------------------------
local android = {}
function android.send(self, x)
print("ANDROID:SEND:", x)
end
function android.sleep(self, t)
print("ANDROID:SLEEP:", t)
local os = require "os"
os.execute("sleep "..tonumber(t))
end
function android.getLatitude(self)
local res = 20.019
print("ANDROID:GETLATITUDE >>", res)
return res
end
function android.getLongitude(self)
local res = 50.0655
print("ANDROID:GETLLONGITUDE >>", res)
return res
end
function android.getTime(self)
local res = os.time()
print("ANDROID:GETLLONGITUDE >>", res)
return res
end
-- ===== DOKLEJANA ZAWARTOŚĆ ===========================================================================================
-- zadanie wysyłane do urządzenia
-- --- JAK TESTOWAĆ? ---------------------------------------------------------------------------------------------------
-- Odkomentować jedną z sekcji MAP, REDUCE albo CHUNKS tuż poniżej
-- ----- MAP ------------------
-- local val1 = {2, 0, 4, 6, 2}
-- ----- REDUCE ---------------
-- local val1 = {0, 2, 2, 4, 6}
-- local val2 = {0, 1, 2, 3, 4}
-- ----- CHUNKS ---------------
local val2 = {3,1,4,1,5,9,2,6,5}
-- tak naprawdę to jedno z tych powyżej powinno być doklejane do appki
-- ===== CHUNKIFY-MAP-REDUCE ===========================================================================================
function domap(xs)
table.sort(xs)
return xs
end
function reduce(xs, ys)
local res = {}
while next(xs) ~= nil and next(ys) ~= nil do
if xs[1] <= ys[1]
then table.insert(res, table.remove(xs, 1))
else table.insert(res, table.remove(ys, 1)) end
end
if next(xs) == nil then
for k,v in pairs(ys) do table.insert(res,v) end
elseif next(ys) == nil then
for k,v in pairs(xs) do table.insert(res,v) end
end
return res
end
function chunks(xs)
local chunklen = 3
local res = {}
while next(xs) ~= nil do
local tmp = {}
while #tmp < chunklen and next(xs) ~= nil do
table.insert(tmp, table.remove(xs, 1))
end
table.insert(res, tmp)
end
return res
end
-- ===== helpers =======================================================================================================
function table2json(xs)
return "[" .. table.concat(xs, ',') .. "]"
end
function map(func, array)
local new_array = {}
for i,v in ipairs(array) do
new_array[i] = func(v)
end
return new_array
end
-- ===== dispatcher ====================================================================================================
if val1 ~= nil and val2 ~= nil then -- REDUCE
android:send(table2json(reduce(val1, val2)))
elseif val1 ~= nil then -- DOMAP
android:send(table2json(domap(val1)))
elseif val2 ~= nil then -- CHUNKIFY
val2 = "[" .. table.concat((map(table2json, chunks(val2))), ", ") .. "]"
android:send(val2)
else -- dunno... ;_;
end
@kgadek
Copy link
Author

kgadek commented Jan 22, 2014

Jeśli:

-- ----- CHUNKS ---------------
local val2 = {3,1,4,1,5,9,2,6,5}

Wynik:

❯ luajit mergesort.lua                                                                                                                                                                            [3:44:17]
ANDROID:SEND:   [[3,1,4], [1,5,9], [2,6,5]]

@kgadek
Copy link
Author

kgadek commented Jan 22, 2014

Jeśli:

-- ----- REDUCE ---------------
local val1 = {0, 2, 2, 4, 6}
local val2 = {0, 1, 2, 3, 4}

Wynik:

❯ luajit mergesort.lua
ANDROID:SEND:   [0,0,1,2,2,2,3,4,4,6]

@kgadek
Copy link
Author

kgadek commented Jan 22, 2014

Jeśli:

-- ----- MAP ------------------
local val1 = {2, 0, 4, 6, 2}

Wynik:

❯ luajit mergesort.lua
ANDROID:SEND:   [0,2,2,4,6]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment