Firetron's FractionGrab

stable
By Firetron in Miscellaneous Published April 2021 👁 1,315 views 💬 0 comments

Description

Grabs a fractional range from an array, list, or table. Custom Command Dependencies: None Test code:
if not Load('done', false) then

  local list = Grab(ClosePrices(), 0, 12)

  Log(' ')
  Log(list)
  Log('Unsorted List:')
  Log(' ')

  local unsorted_3_4 = CC_FractionGrab(list, 3, 4, false)

  Log(unsorted_3_4)
  Log('3rd part of 4 unsorted:')
  Log(' ')

  Log(ArraySort(list))
  Log('Sorted List:')
  Log(' ')

  local sorted_3_3 = CC_FractionGrab(list, 3, 3, true)

  Log(sorted_3_3)
  Log('3rd part of 3 sorted:')
  Log(' ')

  local sorted_1_3 = CC_FractionGrab(list, 1, 3, true)

  Log(sorted_1_3)
  Log('1st part of 3 sorted:')
  Log(' ')

  Save('done', true)

end
HaasScript
--  ============================================================================
--    Firetron's FractionGrab
--
--    Grabs a fractional range from an array, list, or table.
--
--    Custom Command Dependencies:
--    None
--
--    Discord: @FiretronP75
--  ============================================================================

--  ========================================================
--    Variables
--  ========================================================

--  ------------------------------------
--    Definition
--  ------------------------------------

local dDefault
local dDescription
local dName
local dOutput
local dRequired
local dSuggestions
local dType

--  ------------------------------------
--    Function
--  ------------------------------------

local FractionGrab
local ValidateParameters

--  ------------------------------------
--    Parameter
--  ------------------------------------

local pIndex
local pIsSort
local pList
local pTotal

--  ========================================================
--    Command Definition
--  ========================================================

dName        = 'FractionGrab'
dDescription = 'Grabs a fractional range from an array, list, or table.'
DefineCommand(dName, dDescription)

--  ========================================================
--    Parameter Definition
--  ========================================================

dType        = ListDynamicType
dName        = 'list'
dDescription = 'Array, List, or Table to grab a fractional range from.'
dRequired    = false
dDefault     = {3, 1, 2}
dSuggestions = 'Prices'
pList = DefineParameter(dType, dName, dDescription, dRequired, dDefault, dSuggestions)

dType        = NumberType
dName        = 'index'
dDescription = 'Which part to grab. '
             ..'Unlike a fraction numerator, previous parts are not included. '
             ..'In a fraction, 2 would mean 1st and 2nd parts combined. '
             ..'But here, 2 would mean the 2nd part without the 1st.'
dRequired    = false
dDefault     = 1
dSuggestions = 'Input'
pIndex = DefineParameter(dType, dName, dDescription, dRequired, dDefault, dSuggestions)

dType        = NumberType
dName        = 'total'
dDescription = 'How many parts to divide into. '
             ..'This is just like a fraction denominator.'
dRequired    = false
dDefault     = 1
dSuggestions = 'Input'
pTotal = DefineParameter(dType, dName, dDescription, dRequired, dDefault, dSuggestions)

dType        = BooleanType
dName        = 'sort'
dDescription = 'Set to true to sort before dividing into parts.'
dRequired    = false
dDefault     = false
dSuggestions = 'Input'
pIsSort = DefineParameter(dType, dName, dDescription, dRequired, dDefault, dSuggestions)

--  ========================================================
--    Parameter Validation
--  ========================================================

ValidateParameters = function ()

  local isValid = true

  if pTotal < 1 then

    LogError('CC_FractionGrab:  Total "'..pTotal..'" is not a valid total parameter. Must be greater than 0.')
    isValid = false

  end

  if pIndex < 1 then

    LogError('CC_FractionGrab:  Index "'..pIndex..'" is not a valid index parameter. Must be greater than 0.')
    isValid = false

  end

  if pIndex > pTotal then

    LogError('CC_FractionGrab:  Index "'..pIndex..'" is not a valid index parameter. Must not be greater than total parameter.')
    isValid = false

  end

  if pTotal > #pList then

    LogError('CC_FractionGrab:  Total "'..pTotal..'" is not a valid total parameter. Must not be greater than length of list parameter.')
    isValid = false

  end

  return isValid

end

--  ========================================================
--    Functions
--  ========================================================

FractionGrab = function ()

  local isValid = ValidateParameters()

  if not isValid then return end

  local list = pIsSort
           and ArraySort(pList)
            or pList

  if pTotal == 1 then

    return list

  end

  local listLength = #list

  local portionLength = Round(listLength / pTotal, 0)

  local offset

  if pIndex == 1 then

    offset = 0

  elseif pIndex == pTotal then

    offset = listLength - portionLength

  else

    offset = Floor(listLength * ((pIndex - 1) / pTotal))

  end

  return Grab(list, offset, portionLength)

end

--  ========================================================
--    Output Definitions
--  ========================================================

dType        = ListDynamicType
dOutput      = FractionGrab()
dDescription = 'The fractional range from the array, list, or table.'
dSuggestions = ''
DefineOutput(dType, dOutput, dDescription, dSuggestions)

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!