CARVIEW |
Select Language
HTTP/2 200
date: Sun, 20 Jul 2025 15:15:34 GMT
content-type: text/html; charset=utf-8
vary: X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame, X-Requested-With,Accept-Encoding, Accept, X-Requested-With
x-robots-tag: none
etag: W/"04d5fcf5dff2a051f055a66029c96a49"
cache-control: max-age=0, private, must-revalidate
strict-transport-security: max-age=31536000; includeSubdomains; preload
x-frame-options: deny
x-content-type-options: nosniff
x-xss-protection: 0
referrer-policy: no-referrer-when-downgrade
content-security-policy: default-src 'none'; base-uri 'self'; child-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/; connect-src 'self' uploads.github.com www.githubstatus.com collector.github.com raw.githubusercontent.com api.github.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com *.rel.tunnels.api.visualstudio.com wss://*.rel.tunnels.api.visualstudio.com objects-origin.githubusercontent.com copilot-proxy.githubusercontent.com proxy.individual.githubcopilot.com proxy.business.githubcopilot.com proxy.enterprise.githubcopilot.com *.actions.githubusercontent.com wss://*.actions.githubusercontent.com productionresultssa0.blob.core.windows.net/ productionresultssa1.blob.core.windows.net/ productionresultssa2.blob.core.windows.net/ productionresultssa3.blob.core.windows.net/ productionresultssa4.blob.core.windows.net/ productionresultssa5.blob.core.windows.net/ productionresultssa6.blob.core.windows.net/ productionresultssa7.blob.core.windows.net/ productionresultssa8.blob.core.windows.net/ productionresultssa9.blob.core.windows.net/ productionresultssa10.blob.core.windows.net/ productionresultssa11.blob.core.windows.net/ productionresultssa12.blob.core.windows.net/ productionresultssa13.blob.core.windows.net/ productionresultssa14.blob.core.windows.net/ productionresultssa15.blob.core.windows.net/ productionresultssa16.blob.core.windows.net/ productionresultssa17.blob.core.windows.net/ productionresultssa18.blob.core.windows.net/ productionresultssa19.blob.core.windows.net/ github-production-repository-image-32fea6.s3.amazonaws.com github-production-release-asset-2e65be.s3.amazonaws.com insights.github.com wss://alive.github.com api.githubcopilot.com api.individual.githubcopilot.com api.business.githubcopilot.com api.enterprise.githubcopilot.com; font-src github.githubassets.com; form-action 'self' github.com gist.github.com copilot-workspace.githubnext.com objects-origin.githubusercontent.com; frame-ancestors 'none'; frame-src viewscreen.githubusercontent.com notebooks.githubusercontent.com; img-src 'self' data: blob: github.githubassets.com media.githubusercontent.com camo.githubusercontent.com identicons.github.com avatars.githubusercontent.com private-avatars.githubusercontent.com github-cloud.s3.amazonaws.com objects.githubusercontent.com release-assets.githubusercontent.com secured-user-images.githubusercontent.com/ user-images.githubusercontent.com/ private-user-images.githubusercontent.com opengraph.githubassets.com copilotprodattachments.blob.core.windows.net/github-production-copilot-attachments/ github-production-user-asset-6210df.s3.amazonaws.com customer-stories-feed.github.com spotlights-feed.github.com objects-origin.githubusercontent.com *.githubusercontent.com; manifest-src 'self'; media-src github.com user-images.githubusercontent.com/ secured-user-images.githubusercontent.com/ private-user-images.githubusercontent.com github-production-user-asset-6210df.s3.amazonaws.com gist.github.com; script-src github.githubassets.com; style-src 'unsafe-inline' github.githubassets.com; upgrade-insecure-requests; worker-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/
server: github.com
content-encoding: gzip
accept-ranges: bytes
set-cookie: _gh_sess=QWbRIAqkNxI%2Bi2O%2FXnJ5ihjG0%2FzvySaJqhFwFJMw2w2kmejXStqfTpxz%2BzY3c5%2B0WNwbjqySlzMqSOibPNEnTSok42Fbqmp1LD8VpSK4juCcI2l9sNAQxqtGpqjMK29krEnCwQWOVLrrfw6XLuPXfJeINW2nRoqvNZSQDWboKPtxs4swYUwiB7gvv09DmaVkh%2FCewAEwUQTGM9L%2FnfQOMAtJLKCWvL5ssBEccdTm5%2B10SJN6D8kdeTv0HRdfRi49qtUoRTZq4L39xDDD%2Fiwd6Q%3D%3D--WC2UAzkYvhhyR93e--8ZpPLGF6pRYasZ2UkSaK%2FQ%3D%3D; Path=/; HttpOnly; Secure; SameSite=Lax
set-cookie: _octo=GH1.1.23655179.1753024534; Path=/; Domain=github.com; Expires=Mon, 20 Jul 2026 15:15:34 GMT; Secure; SameSite=Lax
set-cookie: logged_in=no; Path=/; Domain=github.com; Expires=Mon, 20 Jul 2026 15:15:34 GMT; HttpOnly; Secure; SameSite=Lax
x-github-request-id: 8136:228B1:11E64BF:1753AE4:687D0816
Example word count · rohitjoshi/lua-mapreduce Wiki · GitHub
Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 8
Example word count
rohitjoshi edited this page Sep 10, 2012
·
7 revisions
Here is an example of how word count is performed using lua-mapreduce.
example/task-file.lua is a sample task-file for work count which implements following functions. Each of these functions are invoked using coroutines to avoid non-blocking calls so as well as returning results one entry at a time to save the memory.
All these functions must be defines as part of table and return this table in function mapreduce()
- taskfn: It reads the source and creates the map of the tasks. E.g For word count, it reads the all file with .lua from current directory and creates a map with key as a file name and content as a value.
mr.taskfn = function() --logger:debug("Getting map task") local tasks = read_source() -- read source is utility function defined to read data source for key, value in pairs(tasks) do coroutine.yield(key, value) end end
Here read_source() is a local function defined as below. NOTE: it uses luafilesystem (lfs) module to read the files
local function read_source() --local file_path = system.pathForFile( "*.lua", lfs.currentdir() ) local file_path = lfs.currentdir() --logger:debug("Current directory path:" .. file_path) local source_table = {} for file in lfs.dir(file_path) do if(string.find(file, ".lua") ~= nil) then -- logger:debug("File name:" .. file_path .. "/" .. file) local c = read_file(file_path .. "/" .. file) -- logger:debug("file:" .. file .. ", length:" .. #c) if( c ~= nil) then source_table[file]=c end end end return source_table end
- finalfn : How to output final result. Here it prints on the console.
mr.finalfn = function (results) print("Final results of the task:") for key, value in pairs(results) do print( key .. ":" .. value) coroutine.yield() end end
- mapfn: Map function which processes the task (content of the file here) and splits into lines and each lines into words
-- Map function : Here it splits the content of the files into lines and each line into words mr.mapfn = function(key, value) --logger:debug("mapfn with key:" .. key .. ", value :" .. value .. "\r\n\r\n") local file_words = {} local lines = value:split("[^\r\n%s]+") -- logger:debug("Number of lines in " .. #lines .. " in the file " .. key) for k, w in ipairs(lines) do if(w ~= nil) then local words = {} string.gsub(w, "(%a+)", function (word) table.insert(words, string.lower(word)) end) --local words = w:split("[^ %s]+") if(words ~= nil) then --logger:debug("Number of words in line " .. k .. " are " .. #words) for j=1, #words do --logger:debug("mapfn:yielding " .. words[j]) coroutine.yield(words[j], 1) end end end end end
- reducefn : Reduce function which processes the number of occurrences for a word. Here it returns the size of an array.
---Reduce function: It returns the numbe of entries for the values mr.reducefn = function (key, value) --logger:debug("reducefn: for key:" .. key .. ", number of words :" .. #value) coroutine.yield(key, #value) end
You can’t perform that action at this time.