Skip to content

chaos-lang/array

Repository files navigation

Array library of the Chaos language. You can install this spell with:

occultist install array

and import it with:

import array

Array Operations

list array.merge(list l1, list l2)

Merges list l1 and list l2 into a new list, in that order.

kaos> list a = [1, 2, 3]
kaos> list b = [4, 5, 6]
kaos> array.merge(a, b)
[1, 2, 3, 4, 5, 6]

list array.insert(list l, any x, num i = -1)

Inserts a new item x into list l before index i.

kaos> list a = [1, 2, 3]
kaos> array.insert(a, 5, 1)
[1, 5, 2, 3]
kaos> array.insert(a, 4)
[1, 2, 3, 4]

list array.reverse(list l)

Reverse the order of items in the list l.

kaos> import array
kaos> list a = [1, 2, 3]
kaos> array.reverse(a)
[3, 2, 1]

list array.chunk(list l, num x)

Chunk a list into x length sublists.

kaos> import array
kaos> list c = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
kaos> array.chunk(c, 3)
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h']]

Searching & Replacing

num list array.search(list haystack, any needle)

Searches the list haystack for a given value needle and returns the corresponding indexes if successful. Returns an empty list [] if unsuccessful.

kaos> list d = [1, 2, 3, 4, 5, 3, 6, 7, 3]
kaos> array.search(d, 3)
[2, 5, 8]
kaos> list e = ['foo', 'bar', 'foo', 'baz', 'bar', 'foo']
kaos> array.search(e, 'foo')
[0, 2, 5]

list array.replace(list haystack, any needle, any replacement)

Replaces all occurrences of the needle with the replacement in list haystack.

kaos> list d = [1, 2, 3, 4, 5, 3, 6, 7, 3]
kaos> list e = ['foo', 'bar', 'foo', 'baz', 'bar', 'foo']
kaos> array.replace(d, 3, 17)
[1, 2, 17, 4, 5, 17, 6, 7, 17]
kaos> array.replace(e, 'foo', 'goo')
['goo', 'bar', 'goo', 'baz', 'bar', 'goo']

Information Functions

num array.length(list l)

Returns the length of list l.

kaos> list a = [1, 2, 3]
kaos> array.length(a)
3