Created
August 12, 2024 13:49
-
-
Save numberlesstim/49ee151859597d8f62c8127a5e408b6c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//gist 49ee151859597d8f62c8127a5e408b6c | |
function sort { | |
parameter inputItems. | |
parameter comparator is { | |
//true --> a further down the list (sorting small to big for numbers) | |
parameter a, b. | |
return a > b. | |
}. | |
local items is inputItems. | |
local tempIndex is round(items:length/2). | |
local temp is items[tempIndex]. | |
local current is lex("index", tempIndex, "value", temp). | |
local smallest is lex("index", tempIndex, "value", temp). | |
local biggest is lex("index", tempIndex, "value", temp). | |
local direction is -1. | |
local swap is { | |
parameter indexA, indexB. | |
set temp to items[indexA]. | |
set items[indexA] to items[indexB]. | |
set items[indexB] to temp. | |
}. | |
local move is { | |
parameter indexFrom, indexTo. | |
local direction is choose 1 if indexTo > indexFrom else -1. | |
until indexFrom = indexTo { | |
swap(indexFrom, indexFrom + direction). | |
set indexFrom to indexFrom + direction. | |
} | |
}. | |
until smallest:index = 0 { | |
set current:index to smallest:index - 1. | |
set current:value to items[current:index]. | |
if comparator(smallest:value, current:value) { | |
set smallest:index to current:index. | |
set smallest:value to current:value. | |
} | |
else { | |
if comparator(biggest:value, current:value) { | |
set tempIndex to smallest:index. | |
set smallest:index to current:index. | |
until comparator(items[tempIndex], current:value) { | |
swap(tempIndex, tempIndex - 1). | |
set tempIndex to tempIndex + 1. | |
} | |
} | |
else { | |
if biggest:index < items:length - 1 { | |
swap(current:index, biggest:index + 1). | |
set biggest:index to biggest:index + 1. | |
set biggest:value to current:value. | |
} | |
else { | |
move(current:index, biggets:index). | |
set smallest:index to current:index. | |
} | |
} | |
} | |
} | |
until biggest:index = items:length - 1 { | |
set current:index to biggest:index + 1. | |
set current:value to items[current:index]. | |
if comparator(current:value, biggest:value) { | |
set biggest:index to current:index. | |
set biggest:value to current:value. | |
} | |
else { | |
if comparator(current:value, smallest:value) { | |
set tempIndex to biggest:index. | |
set biggest:index to current:index. | |
until comparator(current:value, items[tempIndex]) { | |
swap(tempIndex, tempIndex + 1). | |
set tempIndex to tempIndex - 1. | |
} | |
} | |
else { | |
move(current:index, smallest:index). | |
set biggest:index to current:index. | |
} | |
} | |
} | |
return items. | |
} | |
function splitList { | |
parameter items. | |
parameter splitLength is terminal:height - 1. | |
local i is 0. | |
local sublists is list(). | |
until i >= items:length { | |
sublists:add(items:sublist(i, min(splitLength, items:length - i))). | |
set i to i + splitLength. | |
} | |
return sublists. | |
} | |
function pagedList { | |
parameter items. | |
parameter title is "Page". | |
parameter firstPage is 1. | |
parameter splitLength is terminal:height - 2. | |
local i is 0. | |
local pages is list(). | |
local p is firstPage. | |
until i >= items:length { | |
local page is list(title + " [" + p + " of " + (firstPage + ceiling(items:length/splitLength) - 1) + "]:"). | |
for s in items:sublist(i, min(splitLength, items:length - 1)) page:add(s). | |
pages:add(page). | |
set i to i + splitLength. | |
set p to p + 1. | |
} | |
return pages. | |
} | |
function filteredList { | |
parameter items. | |
parameter filter is { | |
parameter item. | |
return true. | |
}. | |
local ret is list(). | |
for i in items if filter(i) ret:add(i). | |
return ret. | |
} | |
function invertedList { | |
parameter items. | |
local ret is list(). | |
for i in range(items:length) ret:add(items[items:length - i - 1]). | |
return ret. | |
} | |
set alphabetical to { | |
parameter a, b. | |
for i in range(min(a:length, b:length)) { | |
if unchar(a[i]) < unchar(b[i]) return false. | |
if unchar(a[i]) > unchar(b[i]) return true. | |
} | |
if a:length < b:length return false. | |
return true. | |
}. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment