TypedArray
t
type t<'a>get
let get: (t<'a>, int) => option<'a>get(typedArray, index) returns the element at index of typedArray.
Returns None if the index does not exist in the typed array. Equivalent to doing typedArray[index] in JavaScript.
Examples
RESCRIPTlet view = Int32Array.fromArray([1, 2, 3])
TypedArray.get(view, 0) == Some(1)
TypedArray.get(view, 10) == None
set
let set: (t<'a>, int, 'a) => unitset(typedArray, index, item) sets the provided item at index of typedArray.
Beware this will mutate the array.
Examples
RESCRIPTlet view = Int32Array.fromArray([1, 2])
TypedArray.set(view, 1, 5)
TypedArray.get(view, 1) == Some(5)
buffer
let buffer: t<'a> => ArrayBuffer.tbuffer(typedArray) returns the underlying ArrayBuffer backing this view.
See TypedArray.prototype.buffer on MDN.
byteLength
let byteLength: t<'a> => intbyteLength(typedArray) returns the length in bytes of the view.
See TypedArray.prototype.byteLength on MDN.
Examples
RESCRIPTlet view = Int32Array.fromArray([1, 2])
TypedArray.byteLength(view) == 8
byteOffset
let byteOffset: t<'a> => intbyteOffset(typedArray) returns the offset in bytes from the start of the buffer.
See TypedArray.prototype.byteOffset on MDN.
setArray
let setArray: (t<'a>, array<'a>) => unitsetArray(target, source) copies the values from source into target, mutating it.
See TypedArray.prototype.set on MDN.
Examples
RESCRIPTlet view = Int32Array.fromArray([0, 0])
TypedArray.setArray(view, [1, 2])
TypedArray.toString(view) == "1,2"
setArrayFrom
let setArrayFrom: (t<'a>, array<'a>, int) => unitsetArrayFrom(target, source, index) copies source into target starting at index.
See TypedArray.prototype.set on MDN.
Examples
RESCRIPTlet view = Int32Array.fromArray([0, 0, 0])
TypedArray.setArrayFrom(view, [5, 6], 1)
TypedArray.toString(view) == "0,5,6"
length
let length: t<'a> => intlength(typedArray) returns the number of elements.
See TypedArray.prototype.length on MDN.
Examples
RESCRIPTlet view = Int32Array.fromArray([1, 2, 3])
TypedArray.length(view) == 3
copyAllWithin
let copyAllWithin: (t<'a>, ~target: int) => array<'a>copyAllWithin(typedArray, ~target) copies values starting at index 0 over the positions beginning at target.
Beware this will mutate the typed array.
See TypedArray.prototype.copyWithin on MDN.
Examples
RESCRIPTlet view = Int32Array.fromArray([10, 20, 30])
let _ = TypedArray.copyAllWithin(view, ~target=1)
TypedArray.toString(view) == "10,10,20"
copyWithinToEnd
Deprecated
let copyWithinToEnd: (t<'a>, ~target: int, ~start: int) => array<'a>copyWithinToEnd(typedArray, ~target, ~start) copies values from start through the end of the view into the range beginning at target.
Beware this will mutate the typed array.
See TypedArray.prototype.copyWithin on MDN.
Examples
RESCRIPTlet view = Int32Array.fromArray([1, 2, 3, 4])
let _ = TypedArray.copyWithinToEnd(view, ~target=0, ~start=2)
TypedArray.toString(view) == "3,4,3,4"
copyWithin
let copyWithin: (t<'a>, ~target: int, ~start: int, ~end: int=?) => array<'a>copyWithin(typedArray, ~target, ~start, ~end) copies the section [start, end) onto the range beginning at target.
Beware this will mutate the typed array.
See TypedArray.prototype.copyWithin on MDN.
Examples
RESCRIPTlet view = Int32Array.fromArray([1, 2, 3, 4])
let _ = TypedArray.copyWithin(view, ~target=1, ~start=2, ~end=4)
TypedArray.toString(view) == "1,3,4,4"
fillAll
let fillAll: (t<'a>, 'a) => t<'a>fillAll(typedArray, value) fills every element with value.
Beware this will mutate the typed array.
See TypedArray.prototype.fill on MDN.
Examples
RESCRIPTlet view = Int32Array.fromArray([1, 2, 3])
let _ = TypedArray.fillAll(view, 9)
TypedArray.toString(view) == "9,9,9"
fillToEnd
Deprecated
let fillToEnd: (t<'a>, 'a, ~start: int) => t<'a>fillToEnd(typedArray, value, ~start) fills from start through the end with value.
Beware this will mutate the typed array.
fill
let fill: (t<'a>, 'a, ~start: int, ~end: int=?) => t<'a>fill(typedArray, value, ~start, ~end) fills the half-open interval [start, end) with value.
Beware this will mutate the typed array.
See TypedArray.prototype.fill on MDN.
Examples
RESCRIPTlet view = Int32Array.fromArray([1, 2, 3, 4])
let _ = TypedArray.fill(view, 0, ~start=1, ~end=3)
TypedArray.toString(view) == "1,0,0,4"
reverse
let reverse: t<'a> => unitreverse(typedArray) reverses the elements of the view in place.
Beware this will mutate the typed array.
See TypedArray.prototype.reverse on MDN.
toReversed
let toReversed: t<'a> => t<'a>toReversed(typedArray) returns a new typed array with the elements in reverse order, leaving the original untouched.
See TypedArray.prototype.toReversed on MDN.
Examples
RESCRIPTlet view = Int32Array.fromArray([1, 2, 3])
let reversed = TypedArray.toReversed(view)
TypedArray.toString(reversed) == "3,2,1"
TypedArray.toString(view) == "1,2,3"
sort
let sort: (t<'a>, ('a, 'a) => Ordering.t) => unitsort(typedArray, comparator) sorts the values in place using comparator.
Beware this will mutate the typed array.
See TypedArray.prototype.sort on MDN.
toSorted
let toSorted: (t<'a>, ('a, 'a) => Ordering.t) => t<'a>toSorted(typedArray, comparator) returns a new typed array containing the sorted values, leaving the original untouched.
See TypedArray.prototype.toSorted on MDN.
Examples
RESCRIPTlet view = Int32Array.fromArray([3, 1, 2])
let sorted = TypedArray.toSorted(view, Int.compare)
TypedArray.toString(sorted) == "1,2,3"
TypedArray.toString(view) == "3,1,2"
with
let with: (t<'a>, int, 'a) => t<'a>with(typedArray, index, value) returns a new typed array where the element at index is replaced with value.
See TypedArray.prototype.with on MDN.
Examples
RESCRIPTlet view = Int32Array.fromArray([1, 2, 3])
let updated = TypedArray.with(view, 1, 10)
TypedArray.toString(updated) == "1,10,3"
TypedArray.toString(view) == "1,2,3"
includes
let includes: (t<'a>, 'a) => boolincludes(typedArray, value) returns true if value occurs in the typed array.
See TypedArray.prototype.includes on MDN.
Examples
RESCRIPTlet view = Int32Array.fromArray([1, 2, 3])
TypedArray.includes(view, 2) == true
TypedArray.includes(view, 10) == false
indexOf
let indexOf: (t<'a>, 'a) => intindexOf(typedArray, value) returns the first index of value, or -1 when not found.
See TypedArray.prototype.indexOf on MDN.
indexOfFrom
let indexOfFrom: (t<'a>, 'a, int) => intindexOfFrom(typedArray, value, fromIndex) searches for value starting at fromIndex.
joinWith
let joinWith: (t<'a>, string) => stringjoinWith(typedArray, separator) returns a string formed by the elements joined with separator.
See TypedArray.prototype.join on MDN.
Examples
RESCRIPTlet view = Int32Array.fromArray([1, 2, 3])
TypedArray.joinWith(view, "-") == "1-2-3"
lastIndexOf
let lastIndexOf: (t<'a>, 'a) => intlastIndexOf(typedArray, value) returns the last index of value, or -1 if not found.
See TypedArray.prototype.lastIndexOf on MDN.
lastIndexOfFrom
let lastIndexOfFrom: (t<'a>, 'a, int) => intlastIndexOfFrom(typedArray, value, fromIndex) searches backwards starting at fromIndex.
slice
let slice: (t<'a>, ~start: int, ~end: int=?) => t<'a>slice(typedArray, ~start, ~end) returns a new typed array containing the elements in [start, end).
See TypedArray.prototype.slice on MDN.
sliceToEnd
Deprecated
let sliceToEnd: (t<'a>, ~start: int) => t<'a>sliceToEnd(typedArray, ~start) returns the elements from start through the end in a new typed array.
copy
let copy: t<'a> => t<'a>copy(typedArray) produces a shallow copy of the typed array.
subarray
let subarray: (t<'a>, ~start: int, ~end: int=?) => t<'a>subarray(typedArray, ~start, ~end) returns a new view referencing the same buffer over [start, end).
See TypedArray.prototype.subarray on MDN.
subarrayToEnd
Deprecated
let subarrayToEnd: (t<'a>, ~start: int) => t<'a>subarrayToEnd(typedArray, ~start) returns a new view from start to the end of the buffer.
toString
let toString: t<'a> => stringtoString(typedArray) returns a comma-separated string of the elements.
See TypedArray.prototype.toString on MDN.
Examples
RESCRIPTInt32Array.fromArray([1, 2])->TypedArray.toString == "1,2"
toLocaleString
let toLocaleString: t<'a> => stringtoLocaleString(typedArray) concatenates the elements using locale-aware formatting.
See TypedArray.prototype.toLocaleString on MDN.
every
let every: (t<'a>, 'a => bool) => boolevery(typedArray, predicate) returns true if predicate returns true for every element.
See TypedArray.prototype.every on MDN.
everyWithIndex
let everyWithIndex: (t<'a>, ('a, int) => bool) => booleveryWithIndex(typedArray, checker) is like every but provides the element index to checker.
filter
let filter: (t<'a>, 'a => bool) => t<'a>filter(typedArray, predicate) returns a new typed array containing only elements that satisfy predicate.
See TypedArray.prototype.filter on MDN.
filterWithIndex
let filterWithIndex: (t<'a>, ('a, int) => bool) => t<'a>filterWithIndex(typedArray, predicate) behaves like filter but also passes the index to predicate.
find
let find: (t<'a>, 'a => bool) => option<'a>find(typedArray, predicate) returns the first element that satisfies predicate, or None if nothing matches.
See TypedArray.prototype.find on MDN.
findWithIndex
let findWithIndex: (t<'a>, ('a, int) => bool) => option<'a>findWithIndex(typedArray, predicate) behaves like find, but predicate also receives the index.
findLast
let findLast: (t<'a>, 'a => bool) => option<'a>findLast(typedArray, predicate) returns the last element that satisfies predicate.
See TypedArray.prototype.findLast on MDN.
findLastWithIndex
let findLastWithIndex: (t<'a>, ('a, int) => bool) => option<'a>findLastWithIndex(typedArray, predicate) is the indexed variant of findLast.
findIndex
let findIndex: (t<'a>, 'a => bool) => intfindIndex(typedArray, predicate) returns the index of the first element that satisfies predicate, or -1 if none do.
See TypedArray.prototype.findIndex on MDN.
findIndexWithIndex
let findIndexWithIndex: (t<'a>, ('a, int) => bool) => intfindIndexWithIndex(typedArray, predicate) is the indexed variant of findIndex.
findLastIndex
let findLastIndex: (t<'a>, 'a => bool) => intfindLastIndex(typedArray, predicate) returns the index of the last matching element, or -1 if none do.
See TypedArray.prototype.findLastIndex on MDN.
findLastIndexWithIndex
let findLastIndexWithIndex: (t<'a>, ('a, int) => bool) => intfindLastIndexWithIndex(typedArray, predicate) is the indexed variant of findLastIndex.
forEach
let forEach: (t<'a>, 'a => unit) => unitforEach(typedArray, f) runs f for every element in order.
See TypedArray.prototype.forEach on MDN.
forEachWithIndex
let forEachWithIndex: (t<'a>, ('a, int) => unit) => unitforEachWithIndex(typedArray, f) runs f for every element, also providing the index.
map
let map: (t<'a>, 'a => 'b) => t<'b>map(typedArray, f) returns a new typed array whose elements are produced by applying f to each element.
See TypedArray.prototype.map on MDN.
mapWithIndex
let mapWithIndex: (t<'a>, ('a, int) => 'b) => t<'b>mapWithIndex(typedArray, f) behaves like map, but f also receives the index.
reduce
let reduce: (t<'a>, ('b, 'a) => 'b, 'b) => 'breduce(typedArray, reducer, initial) combines the elements from left to right using reducer.
See TypedArray.prototype.reduce on MDN.
reduceWithIndex
let reduceWithIndex: (t<'a>, ('b, 'a, int) => 'b, 'b) => 'breduceWithIndex(typedArray, reducer, initial) is the indexed variant of reduce.
reduceRight
let reduceRight: (t<'a>, ('b, 'a) => 'b, 'b) => 'breduceRight(typedArray, reducer, initial) is like reduce but processes the elements from right to left.
See TypedArray.prototype.reduceRight on MDN.
reduceRightWithIndex
let reduceRightWithIndex: (t<'a>, ('b, 'a, int) => 'b, 'b) => 'breduceRightWithIndex(typedArray, reducer, initial) is the indexed variant of reduceRight.
some
let some: (t<'a>, 'a => bool) => boolsome(typedArray, predicate) returns true if predicate returns true for at least one element.
See TypedArray.prototype.some on MDN.
someWithIndex
let someWithIndex: (t<'a>, ('a, int) => bool) => boolsomeWithIndex(typedArray, predicate) behaves like some, but predicate also receives the element index.
ignore
let ignore: t<'a> => unitignore(typedArray) ignores the provided typedArray and returns unit.
This helper is useful when you want to discard a value (for example, the result of an operation with side effects) without having to store or process it further.