Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
xbxalex
Reel Rookie

My advanced functionality

Hello, i'm bad speak english, sorry. In my thread is instended on expand roku functionality.

___

Logical

 

function eq(a, b) as Boolean
    return a = b
end function
function ne(a, b) as Boolean return a <> b end function function ge(a, b) as Boolean return a >= b end function function gt(a, b) as Boolean return a > b end function function le(a, b) as Boolean return a <= b end function function lt(a, b) as Boolean return a < b end function

 

Typecast

 

function instanceof(a, b as String)
    return type(a) = b
end function

function is_invalid(a) as Boolean
    return eq(a, invalid) or instanceof(a, "roInvalid")
end function

function is_valid(a)
    return NOT is_invalid(a)
end function

function is_string(a) as Boolean
    return instanceof(a, "String") or instanceof(a, "roString")
end function
function is_int(a) as Boolean return instanceof(a, "Integer") or instanceof(a, "roInt") end function function is_long(a) as Boolean return instanceof(a, "roLongInteger") end function rem }}} function is_float(a) as Boolean return instanceof(a, "Float") or instanceof(a, "roDouble") end function rem }}} function is_bool(a) as Boolean return instanceof(a, "Boolean") or instanceof(a, "roBoolean") end function rem }}} function is_function(a) as Boolean return instanceof(a, "Function") or instanceof(a, "roFunction") end function function is_array(a) as Boolean return instanceof(a, "roArray") end function rem }}} function is_aa(a) as Boolean return instanceof(a, "roAssociativeArray") end function

 

Runtime

 

function set_timeout(ms, fn) as Integer
    messagePort = createObject("roMessagePort")
    event = wait(ms, messagePort)

    fn()

    return 0
end function

 

Window

 

function window_get_screen_resolution()
    deviceInfo = createObject("roDeviceInfo")
    return deviceInfo.getUIResolution().name
end function

function window_get_screen_width()
    deviceInfo = createObject("roDeviceInfo")
    return deviceInfo.getUIResolution().width
end function

function window_get_screen_height()
    deviceInfo = createObject("roDeviceInfo")
    return deviceInfo.getUIResolution().height
end function

 

 

String functions:

 

 

function ucfirst(str as String) : return mid(ucase(str), 1, 1) + mid(lcase(str), 2) : end function

function substr(str as String, start = 0 as Integer, length = invalid)
if is_invalid(length) then return mid(str, start + 1)
return mid(str, start + 1, length)
end function

function strpos(str, needle)
cnt = len(str)
i = 0
while lt(i, cnt)
if eq(needle, substr(str, i, 1)) then return i
i = i + 1
endwhile

return -1
end function

 

Time functions:

function time()
    dt = createObject("roDateTime")
    return dt.asSeconds()
endfunction

function usleep(ms)
    set_timeout(ms, function() : return 0 : endfunction)
end function

 

Array utils:

function implode(array as Object, delimiter = "," as String)
    result = ""
    for i = 0 to ( array.count() - 1 )
        value = array[i]
        if i = array.count() - 1 then
            result = result + value.toStr()
        else
            result = result + value.toStr() + delimiter
        end if
    end for
    return result
end function

function array_map(array, transform)
    result = []

    for each el in array
        result.push( transform(el) )
    end for

    return result
end function

function array_reduce(array, initValue, transform)
    value = initValue

    for each el in array
        value = transform(value, el)
    end for

    return value
end function

function array_filter(array, filter)
    result = []

    for each el in array
        if filter( el ) then value.push(el)
    end for

    return result
end function

function array_slice(array as Object, startIndex = 0 as Integer, endIndex = 0 as Integer)
    result = []

    if startIndex < 0 then return slicedArray
    if endIndex = 0 then endIndex = array.count()
    if endIndex > array.count() then endIndex = array.count()

    for i = startIndex to ( endIndex - 1 )
        result.push(array[i])
    end for

    return result
end function

Sourcemaking 😉

function queue()
    return {

        '*
        '*
        '*
        tasks: [],

        '*
        '*
        '*
        idx: 0,

        '*
        '*
        '*
        completeFunc: invalid,

        '*
        '*
        '*
        addTask: function(fn) : m.tasks.push(fn)  : endfunction,

        '*
        '*
        '*
        done: function()

            m.idx = m.idx + 1
            cnt = m.tasks.count()
            if eq(m.idx, cnt) then

                if is_valid(m.completeFunc) then
                    completeFunc = m.completeFunc
                    completeFunc()
                endif

                return false
            endif

                m.tasks[m.idx]()

        endfunction

        '*
        '*
        '*
        onComplete: function(func) : m.completeFunc = func : endfunction,

        start: function() : m.tasks[0]() : endfunction
    }
end function

 

 

 

0 Kudos
1 REPLY 1
xbxalex
Reel Rookie

Re: My advanced functionality

String splitting:

function str_split(str, del)

    strlen = len(str)
    dellen = len(del)
    i = 0
    offset = 0
    chain = []
    while lt(offset, strlen)
        if ne(0, i) and eq(del, substr(str, offset + i, dellen)) or eq(offset + i, strlen) then
            chain.push( substr(str, offset, offset + i - offset) )
            offset = offset + i + dellen
            i = 0
        else
            i = i + 1
        endif
    endwhile

    return chain
end function
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.