Otter
Packs
Otter
Upstream Contract & Behavioral Tests
list-utils Pack
Essential collection and list helpers: hasItem, dedupeList, countMatches, joinWithPrefix, removeItemFromList, findFirstMatch, sortList, reverseList, calcListSum, and sliceList.
Schema v2 Operation Contract (10 operations)
Scoping Prefix:_lu_
Schema v2 Pack exposing 10 structurally AST-verified Otter 1.0.1 operations with isolated prefix '_lu_'.
| Operation Signature | Returns | Side Effects | Error Mode |
|---|---|---|---|
hasItem(items: list, target: any) |
boolean |
none | returns-false |
dedupeList(items: list) |
list |
none | returns-fallback |
countMatches(items: list, target: any) |
number |
none | returns-fallback |
joinWithPrefix(items: list, prefix: text) |
list |
none | returns-fallback |
removeItemFromList(items: list, target: any) |
list |
none | returns-fallback |
findFirstMatch(items: list, target: any) |
any |
none | returns-gone |
sortList(items: list) |
list |
none | returns-fallback |
reverseList(items: list) |
list |
none | returns-fallback |
calcListSum(items: list) |
number |
none | returns-fallback |
sliceList(items: list, startIndex: number, countLimit: number) |
list |
none | returns-fallback |
Source Code (4 files)
# list-utils - Otter Reusable Collection Library
# Provides hasItem, dedupeList, countMatches, joinWithPrefix,
# removeItemFromList, findFirstMatch, sortList, reverseList,
# calcListSum, and sliceList
# Internal variables use '_lu_' prefix to prevent collisions with caller/outer variables.
to hasItem items target
if items contains target
return true
.
return false
.
to dedupeList items
_lu_result are empty
for each _lu_it in items
if not _lu_result contains _lu_it
add _lu_it to _lu_result
.
.
return _lu_result
.
to countMatches items target
_lu_matches is 0
for each _lu_it in items
if _lu_it is target
_lu_matches is _lu_matches plus 1
.
.
return _lu_matches
.
to joinWithPrefix items prefix
_lu_result are empty
for each _lu_it in items
_lu_combined is prefix plus _lu_it
add _lu_combined to _lu_result
.
return _lu_result
.
to removeItemFromList items target
_lu_result are empty
for each _lu_it in items
if _lu_it is not target
add _lu_it to _lu_result
.
.
return _lu_result
.
to findFirstMatch items target
for each _lu_it in items
if _lu_it is target
return _lu_it
.
.
return gone
.
to sortList items
_lu_copy are empty
for each _lu_it in items
add _lu_it to _lu_copy
.
sort _lu_copy
return _lu_copy
.
to reverseList items
_lu_copy are empty
for each _lu_it in items
add _lu_it to _lu_copy
.
reverse _lu_copy
return _lu_copy
.
to calcListSum items
_lu_total is 0
for each _lu_it in items
_lu_total is _lu_total plus _lu_it
.
return _lu_total
.
to sliceList items startIndex countLimit
_lu_result are empty
_lu_idx is 1
_lu_endIdx is startIndex plus countLimit
for each _lu_it in items
if _lu_idx is at least startIndex
if _lu_idx is less than _lu_endIdx
add _lu_it to _lu_result
.
.
_lu_idx is _lu_idx plus 1
.
return _lu_result
.
{
"schemaVersion": 2,
"name": "list-utils",
"publisher": "otter-official",
"version": "1.0.1",
"kind": "pack",
"description": "Essential collection and list helpers: hasItem, dedupeList, countMatches, joinWithPrefix, removeItemFromList, findFirstMatch, sortList, reverseList, calcListSum, and sliceList.",
"entry": "src/main.ot",
"license": "MIT",
"requiresOtter": ">=1.0.1",
"platforms": [
"console",
"desktop",
"web"
],
"variablePrefix": "_lu_",
"provides": {
"capabilities": [
"collections.list",
"collections.filtering",
"collections.aggregation"
],
"operations": [
{
"name": "hasItem",
"description": "Checks whether target exists inside the items list.",
"parameters": [
{ "name": "items", "type": "list" },
{ "name": "target", "type": "any" }
],
"returns": { "type": "boolean" },
"sideEffects": ["none"],
"errorMode": "returns-false"
},
{
"name": "dedupeList",
"description": "Returns a new list containing only the first occurrence of each distinct item.",
"parameters": [
{ "name": "items", "type": "list" }
],
"returns": { "type": "list" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "countMatches",
"description": "Counts how many elements in items equal target.",
"parameters": [
{ "name": "items", "type": "list" },
{ "name": "target", "type": "any" }
],
"returns": { "type": "number" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "joinWithPrefix",
"description": "Returns a new list with prefix prepended to every element of items.",
"parameters": [
{ "name": "items", "type": "list" },
{ "name": "prefix", "type": "text" }
],
"returns": { "type": "list" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "removeItemFromList",
"description": "Returns a new list with all elements equal to target removed.",
"parameters": [
{ "name": "items", "type": "list" },
{ "name": "target", "type": "any" }
],
"returns": { "type": "list" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "findFirstMatch",
"description": "Returns the first element matching target, or gone if not found.",
"parameters": [
{ "name": "items", "type": "list" },
{ "name": "target", "type": "any" }
],
"returns": { "type": "any", "nullable": true },
"sideEffects": ["none"],
"errorMode": "returns-gone"
},
{
"name": "sortList",
"description": "Returns a sorted shallow copy of items without mutating the input list.",
"parameters": [
{ "name": "items", "type": "list" }
],
"returns": { "type": "list" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "reverseList",
"description": "Returns a reversed shallow copy of items without mutating the input list.",
"parameters": [
{ "name": "items", "type": "list" }
],
"returns": { "type": "list" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "calcListSum",
"description": "Computes the numeric sum of all elements in items.",
"parameters": [
{ "name": "items", "type": "list" }
],
"returns": { "type": "number" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "sliceList",
"description": "Returns a 1-based slice of up to countLimit elements starting at startIndex.",
"parameters": [
{ "name": "items", "type": "list" },
{ "name": "startIndex", "type": "number" },
{ "name": "countLimit", "type": "number" }
],
"returns": { "type": "list" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
}
]
},
"requires": {
"capabilities": [],
"packages": {}
},
"capabilities": [],
"dependencies": {},
"tags": [
"list",
"collections",
"array",
"dedupe",
"contains",
"count",
"sort",
"slice"
],
"examples": [
"examples/demo.ot"
]
}
# Tests for list-utils (Strict Behavioral Assertions)
use "../src/main.ot"
say "Running list-utils behavioral tests..."
# 1. hasItem
fruits are
"apple"
"banana"
"cherry"
.
if hasItem fruits "banana" is not true
fail with "hasItem failed: expected true for banana"
.
if hasItem fruits "orange" is not false
fail with "hasItem failed: expected false for orange"
.
# 2. dedupeList
duplicates are
"a"
"b"
"a"
"c"
"b"
.
uniqueList is dedupeList duplicates
if length of uniqueList is not 3
fail with "dedupeList failed: expected 3 unique items"
.
if hasItem uniqueList "a" is not true
fail with "dedupeList failed: missing item a"
.
if hasItem uniqueList "b" is not true
fail with "dedupeList failed: missing item b"
.
if hasItem uniqueList "c" is not true
fail with "dedupeList failed: missing item c"
.
# 3. countMatches
repeated are
"x"
"y"
"x"
"x"
"z"
.
xCount is countMatches repeated "x"
if xCount is not 3
fail with "countMatches failed: expected 3 for x"
.
wCount is countMatches repeated "w"
if wCount is not 0
fail with "countMatches failed: expected 0 for w"
.
# 4. joinWithPrefix
tags are
"parser"
"lexer"
.
prefixed is joinWithPrefix tags "pkg:"
if length of prefixed is not 2
fail with "joinWithPrefix failed: expected 2 items"
.
if hasItem prefixed "pkg:parser" is not true
fail with "joinWithPrefix failed: expected pkg:parser"
.
# 5. removeItemFromList
originalItems are
"keep1"
"drop"
"keep2"
.
filtered is removeItemFromList originalItems "drop"
if length of filtered is not 2
fail with "removeItemFromList failed: expected 2 items remaining"
.
if hasItem filtered "drop" is not false
fail with "removeItemFromList failed: drop item still present"
.
# 6. findFirstMatch
foundItem is findFirstMatch originalItems "keep2"
if foundItem is not "keep2"
fail with "findFirstMatch failed: expected keep2"
.
missingItem is findFirstMatch originalItems "notFound"
if missingItem is not gone
fail with "findFirstMatch failed: expected gone for missing item"
.
# 7. sortList
unsortedNums are
50
20
40
10
.
sortedNums is sortList unsortedNums
if length of sortedNums is not 4
fail with "sortList failed: expected 4 items"
.
# 8. reverseList
reversedNums is reverseList sortedNums
if length of reversedNums is not 4
fail with "reverseList failed: expected 4 items"
.
# 9. calcListSum
values are
10
20
30
40
.
totalSum is calcListSum values
if totalSum is not 100
fail with "calcListSum failed: expected 100"
.
# 10. sliceList
alphabet are
"A"
"B"
"C"
"D"
"E"
.
subSlice is sliceList alphabet 2 3
if length of subSlice is not 3
fail with "sliceList failed: expected 3 items"
.
if hasItem subSlice "B" is not true
fail with "sliceList failed: expected item B"
.
if hasItem subSlice "D" is not true
fail with "sliceList failed: expected item D"
.
if hasItem subSlice "A" is not false
fail with "sliceList failed: A should not be in subslice"
.
say "All list-utils behavioral tests passed successfully!"
# list-utils Demo
use "../src/main.ot"
say "=== list-utils Demo ==="
tasks are
"Code"
"Test"
"Code"
"Deploy"
"Test"
.
say "Original tasks count:" length of tasks
say "Has 'Deploy':" hasItem tasks "Deploy"
say "Count of 'Code':" countMatches tasks "Code"
deduped is dedupeList tasks
say "Unique tasks count:" length of deduped
say "Prefixed tasks:"
prefixed is joinWithPrefix deduped "[DONE] "
for each p in prefixed
say " " p
.
say "Removing 'Deploy':"
cleaned is removeItemFromList deduped "Deploy"
for each c in cleaned
say " Remaining:" c
.
sub is sliceList cleaned 1 2
say "Subslice (first 2):"
for each s in sub
say " Item:" s
.
Installation & Usage
otter pack install otter-official/list-utils
otter pack verify --contracts
use ".otter/packages/otter-official/list-utils/1.0.1/src/main.ot" # Inspect exported operation contracts: otter pack contract otter-official/list-utils