<commit_before>-- Alias for document.querySelectorAll
local q = function(query)
    local result  = document:querySelectorAll(query)

    -- Store result in lua array
    local elements = {}
    for i = 0, result.length - 1 do
        table.insert(elements, result[i])
    end

    if #elements > 0 then
        return #elements > 1 and elements or elements[1]
    end

    return nil
end

-- Fetch n url in parallel, must be run in a coroutine co
local fetch = function(co, ...)
    local requests = {...}
    for i = 1, #requests do
        local promise = js.global:fetch(requests[i])
        promise["then"](promise, function(_, res)
            local textPromise = res:text()
            textPromise["then"](textPromise, function(_, text)
                co(i, text)
            end)
        end)
    end

    local results = {}
    for i = 1, #requests do
        local requestId, response = coroutine.yield()
        results[requestId] = response
    end

    return table.unpack(results)
end

-- Take full paths for now, should be in coroutine co
local require = function(co, ...)
    local paths = table.pack(...)

    local modules = table.pack(fetch(co, table.unpack(paths)))

    for i = 1, #modules do
        local module = modules[i]

        modules[i] = load(module)
    end

    return table.unpack(modules)
end

return {
    q = q,
    fetch = fetch
}<commit_msg>No need to pass coroutine as parameter, use coroutine.running()<commit_after>-- Alias for document.querySelectorAll
local q = function(query)
    local result  = document:querySelectorAll(query)

    -- Store result in lua array
    local elements = {}
    for i = 0, result.length - 1 do
        table.insert(elements, result[i])
    end

    if #elements > 0 then
        return #elements > 1 and elements or elements[1]
    end

    return nil
end

-- Fetch n url in parallel, must be run in a coroutine
local fetch = function(...)
    local co = coroutine.running()

    local requests = {...}
    for i = 1, #requests do
        local promise = js.global:fetch(requests[i])
        promise["then"](promise, function(_, res)
            local textPromise = res:text()
            textPromise["then"](textPromise, function(_, text)
                co(i, text)
            end)
        end)
    end

    local results = {}
    for i = 1, #requests do
        local requestId, response = coroutine.yield()
        results[requestId] = response
    end

    return table.unpack(results)
end

-- Take full paths for now, should be in coroutine
local require = function(...)
    local co = coroutine.running()

    local paths = table.pack(...)

    local modules = table.pack(fetch(co, table.unpack(paths)))

    for i = 1, #modules do
        local module = modules[i]

        modules[i] = load(module)
    end

    return table.unpack(modules)
end

return {
    q = q,
    fetch = fetch
}
{'fraction_non_alphanumeric': 0.10793871866295264, 'fraction_numerical': 0.005571030640668524, 'mean_word_length': 2.279680365296804, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1172488', 'n_tokens_mistral': 869, 'n_tokens_neox': 841, 'n_words': 284}