A Buffer in Motoko is a growable data structure that houses elements of generic type X. The Buffer Base Module contains a class Buffer (same name as module) with class methods. The module also offers many public functions.
The convention is to name the module alias after the file name it is defined in:
import Buffer "mo:base/Buffer";
Type Buffer.Buffer<X>
Class Buffer.Buffer<X>(initCapacity : Nat)
Class methods
Method size
Method add
Method get
Method getOpt
Method put
Method removeLast
Method remove
Method clear
Method filterEntries
Method capacity
Method reserve
Method append
Method insert
Method insertBuffer
Method sort
Method vals
Module public functions
Function isEmpty
Function contains
Function clone
Function max
Function min
Function equal
Function compare
Function toText
Function hash
Function indexOf
Function lastIndexOf
Function indexOfBuffer
Function binarySearch
Function subBuffer
Function isSubBufferOf
Function isStrictSubBufferOf
Function prefix
Function isPrefixOf
Function isStrictPrefixOf
Function suffix
Function isSuffixOf
Function isStrictSuffixOf
Function forAll
Function forSome
Function forNone
Function toArray
Function toVarArray
Function fromArray
Function fromVarArray
Function fromIter
Function trimToSize
Function map
Function iterate
Function mapEntries
Function mapFilter
Function mapResult
Function chain
Function foldLeft
Function foldRight
Function first
Function last
Function make
Function reverse
Function merge
Function removeDuplicates
Function partition
Function split
Function chunk
Function groupBy
Function flatten
Function zip
Function zipWith
Function takeWhile
Function dropWhile
The Buffer module contains a public type Buffer<X> with the same name. It's convenient to rename the type locally:
import Buffer "mo:base/Buffer";
type Buffer<X> = Buffer.Buffer<X>;
type BufNat = Buffer.Buffer<Nat>;
In the first line we declare a local type alias Buffer<X> by referring to the type inside the module. This new local type name takes in a generic type parameter <X>.
In the second line we declare another local alias BufNat which takes no parameters. It is always a Buffer of Nat.
Buffer.Buffer<X>(initCapacity : Nat)
The Buffer<X> class takes one argument initCapacity of type Nat, which represent the initial capacity of the buffer.
To construct a buffer object, we use the Buffer class:
import Buffer "mo:base/Buffer";
type Buffer<X> = Buffer.Buffer<X>;
let myBuffer : Buffer<Nat> = Buffer.Buffer<Nat>(100);
We construct an object myBuffer of type Buffer<Nat> by calling the class Buffer.Buffer with type parameter Nat and initial capacity 100.
func size() : Nat
The function size takes no argument and returns a Nat value.
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(10);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
intStorage.size()
func add(element : X) : ()
The function add takes one generic type X argument and returns a () value.
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
Buffer.toArray(intStorage)
func get(index : Nat) : X
The function get takes one Nat argument and returns a generic type value X .
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
intStorage.get(2);
func getOpt(index : Nat) : ?X
The function getOpt takes one Nat argument and returns a generic type value ?X.
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
func put(index : Nat, element : X) : ()
The function put takes one Natand one generic type x argument and returns a () value.
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(3);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
intStorage.put(3, 2);
Buffer.toArray(intStorage);
func removeLast() : ?X
The function removeLast takes no argument and returns a generic type value ?X.
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
let removeLast : ?Int = intStorage.removeLast();
Buffer.toArray(intStorage);
func remove(index : Nat) : X
The function remove takes one Nat argument and returns a generic type value X.
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
let remove : Int = intStorage.remove(1);
Buffer.toArray(intStorage);
func clear() : ()
The function clear takes no argument and returns a () value.
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
intStorage.clear();
Buffer.toArray(intStorage);
func filterEntries(predicate : (Nat, X) -> Bool) : ()
| Parameters | |
| Function argument | (Nat, X) -> Bool |
| Return type | () |
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-2);
intStorage.add(2);
intStorage.add(3);
intStorage.add(4);
intStorage.add(7);
func check(index : Nat, value : Int) : Bool {
value % 2 == 0;
};
intStorage.filterEntries(check);
Buffer.toArray(intStorage);
func capacity() : Nat
The function capacity takes no argument and returns a Nat value.
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
intStorage.capacity();
func reserve(capacity : Nat) : ()
The function reserve takes one Nat argument and returns a () value.
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
intStorage.reserve(4);
func append(buffer2 : Buffer<X>) : ()
The function append takes one generic type Buffer<X> argument and returns a () value.
import Buffer "mo:base/Buffer";
let intStorage1 = Buffer.Buffer<Int>(0);
intStorage1.add(-1);
intStorage1.add(0);
intStorage1.add(1);
let intStorage2 = Buffer.Buffer<Int>(0);
intStorage2.add(2);
intStorage1.append(intStorage2);
Buffer.toArray(intStorage1);
func insert(index : Nat, element : X) : ()
The function insert takes one Natand one generic type X argument and returns a () value.
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
intStorage.insert(3, 2);
Buffer.toArray(intStorage);
func insertBuffer(index : Nat, buffer2 : Buffer<X>) : ()
The function insertBuffer takes one Natand one generic type Buffer<X> argument and returns a () value.
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
let buffer1 = Buffer.Buffer<Int>(2);
buffer1.add(4);
intStorage.insertBuffer(3, buffer1);
Buffer.toArray(intStorage);
func sort(compare : (X, X) -> Order.Order) : ()
| Parameters | |
| Function argument | (X, X) -> Order.Order |
| Return type | () |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
intStorage.sort(Int.compare);
Buffer.toArray(intStorage);
func vals() : { next : () -> ?X }
The function vals takes no argument and returns a Iter value.
import Buffer "mo:base/Buffer";
import Iter "mo:base/Iter";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
let iter : Iter.Iter<Int> = intStorage.vals();
Iter.toArray(iter);
func isEmpty<X>(buffer : Buffer<X>) : Bool
| Parameters | |
| Generic parameters | X |
| Variable argument | buffer : Buffer<X> |
| Return type | Bool |
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
Buffer.isEmpty(intStorage);
func contains<X>(
buffer : Buffer<X>
element : X
equal : (X, X) -> Bool
) : Bool
| Parameters | |
| Generic parameters | X |
| Variable argument1 | buffer : Buffer<X> |
| Variable argument2 | element : X |
| Function argument | equal : (X, X) -> Bool |
| Return type | Bool |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
let element : Int = 2;
Buffer.contains(intStorage, element, Int.equal);
func clone<X>(buffer : Buffer<X>) : Buffer<X>
| Parameters | |
| Generic parameters | X |
| Variable argument | buffer : Buffer<X> |
| Return type | Buffer<X> |
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
let clone = Buffer.clone(intStorage);
Buffer.toArray(clone);
func max<X>(
buffer : Buffer<X>
compare : (X, X) -> Order
) : ?X
| Parameters | |
| Generic parameters | X |
| Variable argument | buffer : Buffer<X> |
| Function argument | compare : (X, X) -> Order |
| Return type | ?X |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
Buffer.max<Int>(intStorage, Int.compare)
func min<X>(
buffer : Buffer<X>
compare : (X, X) -> Order
) : ?X
| Parameters | |
| Generic parameters | X |
| Variable argument | buffer : Buffer<X> |
| Function argument | compare : (X, X) -> Order |
| Return type | ?X |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
Buffer.min<Int>(intStorage, Int.compare)
func equal<X>(
buffer1 : Buffer<X>
buffer2 : Buffer<X>
equal : (X, X) -> Bool
) : Bool
| Parameters | |
| Generic parameters | X |
| Variable argument1 | buffer1 : Buffer<X> |
| Variable argument2 | buffer2 : Buffer<X> |
| Function argument | equal : (X, X) -> Bool |
| Return type | Bool |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let intStorage1 = Buffer.Buffer<Int>(0);
intStorage1.add(-1);
intStorage1.add(0);
intStorage1.add(1);
let intStorage2 = Buffer.Buffer<Int>(0);
intStorage2.add(-1);
intStorage2.add(0);
intStorage2.add(1);
Buffer.equal(intStorage1, intStorage2, Int.equal);
func compare<X>(
buffer1 : Buffer<X>
buffer2 : Buffer<X>
compare : (X, X) -> Order.Order
) : Order.Order
| Parameters | |
| Generic parameters | X |
| Variable argument1 | buffer1 : Buffer<X> |
| Variable argument2 | buffer2 : Buffer<X> |
| Function argument | compare : (X, X) -> Order.Order) |
| Return type | Order.Order |
import Buffer "mo:base/Buffer";
import Order "mo:base/Order";
import Int "mo:base/Int";
let intStorage1 = Buffer.Buffer<Int>(0);
intStorage1.add(-1);
intStorage1.add(0);
intStorage1.add(1);
let intStorage2 = Buffer.Buffer<Int>(0);
intStorage2.add(-1);
intStorage2.add(0);
intStorage2.add(1);
Buffer.compare<Int>(intStorage1, intStorage2, Int.compare);
func toText<X>(
buffer : Buffer<X>
toText : X -> Text
) : Text
| Parameters | |
| Generic parameters | X |
| Variable argument | buffer : Buffer<X> |
| Function argument | toText : X -> Text |
| Return type | Text |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
Buffer.toText(intStorage, Int.toText)
func indexOf<X>(
element : X
buffer : Buffer<X>
equal : (X, X) -> Bool
) : ?Nat
| Parameters | |
| Generic parameters | X |
| Variable argument1 | element : X |
| Variable argument2 | buffer : Buffer<X> |
| Function argument | equal : (X, X) -> Bool |
| Return type | ?Nat |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
Buffer.indexOf<Int>(1, intStorage, Int.equal);
func indexOf<X>(
element : X
buffer : Buffer<X>
equal : (X, X) -> Bool
) : ?Nat
| Parameters | |
| Generic parameters | X |
| Variable argument1 | element : X |
| Variable argument2 | buffer : Buffer<X> |
| Function argument | equal : (X, X) -> Bool |
| Return type | ?Nat |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
intStorage.add(0);
intStorage.add(0);
Buffer.lastIndexOf<Int>(0, intStorage, Int.equal)
func indexOfBuffer<X>(
subBuffer : Buffer<X>
buffer : Buffer<X>
equal : (X, X) -> Bool
) : ?Nat
| Parameters | |
| Generic parameters | X |
| Variable argument1 | subBuffer : Buffer<X> |
| Variable argument2 | buffer : Buffer<X> |
| Function argument | equal : (X, X) -> Bool |
| Return type | ?Nat |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let intStorage1 = Buffer.Buffer<Int>(0);
intStorage1.add(-3);
intStorage1.add(-2);
intStorage1.add(-1);
intStorage1.add(0);
intStorage1.add(1);
let intStorage2 = Buffer.Buffer<Int>(0);
intStorage2.add(-1);
intStorage2.add(0);
intStorage2.add(1);
Buffer.indexOfBuffer<Int>(intStorage2, intStorage1, Int.equal)
func binarySearch<X>(
element : X
buffer : Buffer<X>
compare : (X, X) -> Order.Order
) : ?Nat
| Parameters | |
| Generic parameters | X |
| Variable argument1 | element : X |
| Variable argument2 | buffer : Buffer<X> |
| Function argument | compare : (X, X) -> Order.Order |
| Return type | ?Nat |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-3);
intStorage.add(-2);
intStorage.add(-1);
Buffer.binarySearch<Int>(-1, intStorage, Int.compare);
func subBuffer<X>(
buffer : Buffer<X>
start : Nat
length : Nat
) : Buffer<X>
| Parameters | |
| Generic parameters | X |
| Variable argument1 | buffer : Buffer<X> |
| Variable argument2 | start : Nat |
| variable argument3 | lenght : Nat |
| Return type | Buffer<X> |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-3);
intStorage.add(-2);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
let subBuffer : Buffer.Buffer<Int> = Buffer.subBuffer(intStorage, 2, 2);
Buffer.toText<Int>(subBuffer, Int.toText)
func isSubBufferOf<X>(
subBuffer : Buffer<X>
buffer : Buffer<X>
equal : (X, X) -> Bool
) : Bool
| Parameters | |
| Generic parameters | X |
| Variable argument1 | subBuffer : Buffer<X> |
| Variable argument2 | buffer : Buffer<X> |
| Function argument | equal : (X, X) -> Bool |
| Return type | Bool |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-3);
intStorage.add(-2);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
let subIntStorage = Buffer.Buffer<Int>(0);
intStorage.add(-3);
intStorage.add(-2);
Buffer.isSubBufferOf(subIntStorage, intStorage, Int.equal)
func isSubBufferOf<X>(
subBuffer : Buffer<X>
buffer : Buffer<X>
equal : (X, X) -> Bool
) : Bool
| Parameters | |
| Generic parameters | X |
| Variable argument1 | subBuffer : Buffer<X> |
| Variable argument2 | buffer : Buffer<X> |
| Function argument | equal : (X, X) -> Bool |
| Return type | Bool |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-2);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
let subIntStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
Buffer.isStrictSubBufferOf(subIntStorage, intStorage, Int.equal)
func prefix<X>(
buffer : Buffer<X>
length : Nat
) : Buffer<X>
| Parameters | |
| Generic parameters | X |
| Variable argument1 | buffer : Buffer<X> |
| variable argument2 | lenght : Nat |
| Return type | Buffer<X> |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-2);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
intStorage.add(2);
let prefix : Buffer.Buffer<Int> = Buffer.prefix(intStorage, 3);
Buffer.toText(prefix, Int.toText)
func isPrefixOf<X>(
prefix : Buffer<X>
buffer : Buffer<X>
equal : (X, X) -> Bool
) : Bool
| Parameters | |
| Generic parameters | X |
| Variable argument1 | prefix : Buffer<X> |
| Variable argument2 | buffer : Buffer<X> |
| Function argument | equal : (X, X) -> Bool |
| Return type | Bool |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-2);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
intStorage.add(2);
let prefix = Buffer.Buffer<Int>(0);
intStorage.add(-2);
intStorage.add(-1);
intStorage.add(0);
Buffer.isPrefixOf(prefix, intStorage, Int.equal)
func isStrictPrefixOf<X>(
prefix : Buffer<X>
buffer : Buffer<X>
equal : (X, X) -> Bool
) : Bool
| Parameters | |
| Generic parameters | X |
| Variable argument1 | prefix : Buffer<X> |
| Variable argument2 | buffer : Buffer<X> |
| Function argument | equal : (X, X) -> Bool |
| Return type | Bool |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-2);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
intStorage.add(2);
let prefix = Buffer.Buffer<Int>(0);
intStorage.add(-2);
intStorage.add(-1);
intStorage.add(0);
Buffer.isStrictPrefixOf(prefix, intStorage, Int.equal)
func suffix<X>(
buffer : Buffer<X>
length : Nat
) : Buffer<X>
| Parameters | |
| Generic parameters | X |
| Variable argument1 | buffer : Buffer<X> |
| variable argument2 | lenght : Nat |
| Return type | Buffer<X> |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-2);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
intStorage.add(2);
let suffix : Buffer.Buffer<Int> = Buffer.suffix(intStorage, 3);
Buffer.toText(suffix, Int.toText)
func isSuffixOf<X>(
suffix : Buffer<X>
buffer : Buffer<X>
equal : (X, X) -> Bool
) : Bool
| Parameters | |
| Generic parameters | X |
| Variable argument1 | suffix : Buffer<X> |
| Variable argument2 | buffer : Buffer<X> |
| Function argument | equal : (X, X) -> Bool |
| Return type | Bool |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-2);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
intStorage.add(2);
let suffix = Buffer.Buffer<Int>(0);
intStorage.add(0);
intStorage.add(1);
intStorage.add(2);
Buffer.isSuffixOf(suffix, intStorage, Int.equal)
func isSuffixOf<X>(
suffix : Buffer<X>
buffer : Buffer<X>
equal : (X, X) -> Bool
) : Bool
| Parameters | |
| Generic parameters | X |
| Variable argument1 | suffix : Buffer<X> |
| Variable argument2 | buffer : Buffer<X> |
| Function argument | equal : (X, X) -> Bool |
| Return type | Bool |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-2);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
intStorage.add(2);
let suffix = Buffer.Buffer<Int>(0);
intStorage.add(0);
intStorage.add(1);
intStorage.add(2);
Buffer.isStrictSuffixOf(suffix, intStorage, Int.equal)
func forAll<X>(
buffer : Buffer<X>
predicate : X -> Bool
) : Bool
| Parameters | |
| Generic parameters | X |
| Variable argument | buffer : Buffer<X> |
| Function argument | predicate : X -> Bool |
| Return type | Bool |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
func check(x : Int) : Bool {
x > 0;
};
Buffer.forAll<Int>(intStorage, check);
func forSome<X>(
buffer : Buffer<X>
predicate : X -> Bool
) : Bool
| Parameters | |
| Generic parameters | X |
| Variable argument | buffer : Buffer<X> |
| Function argument | predicate : X -> Bool |
| Return type | Bool |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
func check(x : Int) : Bool {
x > 0;
};
Buffer.forSome<Int>(intStorage, check);
func forNone<X>(
buffer : Buffer<X>
predicate : X -> Bool
) : Bool
| Parameters | |
| Generic parameters | X |
| Variable argument | buffer : Buffer<X> |
| Function argument | predicate : X -> Bool |
| Return type | Bool |
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
func check(x : Int) : Bool {
x > 1;
};
Buffer.forNone<Int>(intStorage, check);
func toArray<X>(buffer : Buffer<X>) : [X]
| Parameters | |
| Generic parameters | X |
| Variable argument | buffer : Buffer<X> |
| Return type | [X] |
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
Buffer.toArray<Int>(intStorage)
func toVarArray<X>(buffer : Buffer<X>) : [var X]
| Parameters | |
| Generic parameters | X |
| Variable argument | buffer : Buffer<X> |
| Return type | [var X] |
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
Buffer.toVarArray<Int>(intStorage)
func fromArray<X>(array : [X]) : Buffer<X>
| Parameters | |
| Generic parameters | X |
| Variable argument | array : [X] |
| Return type | Buffer<X> |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let array : [Int] = [-1, 0, 1];
let buffer : Buffer.Buffer<Int> = Buffer.fromArray<Int>(array);
Buffer.toText(buffer, Int.toText);
func fromArray<X>(array : [var X]) : Buffer<X>
| Parameters | |
| Generic parameters | X |
| Variable argument | array : [var X] |
| Return type | Buffer<X> |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let array : [Int] = [-1, 0, 1];
let buffer : Buffer.Buffer<Int> = Buffer.fromArray<Int>(array);
Buffer.toText(buffer, Int.toText);
func fromIter<X>(iter : { next : () -> ?X }) : Buffer<X>
| Parameters | |
| Generic parameters | X |
| Variable argument | iter : { next : () -> ?X } |
| Return type | Buffer<X> |
import Buffer "mo:base/Buffer";
import Iter "mo:base/Iter";
import Int "mo:base/Int";
let array : [Int] = [-1, 0, 1];
let iter : Iter.Iter<Int> = array.vals();
let buffer = Buffer.fromIter<Int>(iter);
Buffer.toText(buffer, Int.toText);
func trimToSize<X>(buffer : Buffer<X>) : ()
| Parameters | |
| Generic parameters | X |
| Variable argument | buffer : Buffer<X> |
| Return type | () |
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(10);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
Buffer.trimToSize<Int>(intStorage);
intStorage.capacity()
func map<X, Y>(
buffer : Buffer<X>
f : X -> Y
) : Buffer<Y>
| Parameters | |
| Generic parameters | X, Y |
| Variable argument | buffer : Buffer<X> |
| Function argument | f : X -> Y |
| Return type | Buffer<Y> |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let intStorage = Buffer.Buffer<Int>(10);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
func change(x : Int) : Int {
x ** 2;
};
let newBuffer : Buffer.Buffer<Int> = Buffer.map<Int, Int>(intStorage, change);
Buffer.toText(newBuffer, Int.toText)
func iterate<X>(
buffer : Buffer<X>
f : X -> ()
) : ()
| Parameters | |
| Generic parameters | X |
| Variable argument | buffer : Buffer<X> |
| Function argument | f : X -> () |
| Return type | () |
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(10);
intStorage.add(-2);
intStorage.add(-1);
intStorage.add(0);
func change(x : Int) : () {
number += x;
};
var number : Int = 10;
Buffer.iterate<Int>(intStorage, change)
func mapEntries<X, Y>(
buffer : Buffer<X>
f : (Nat, X) -> Y
) : Buffer<Y>
| Parameters | |
| Generic parameters | X, Y |
| Variable argument | buffer : Buffer<X> |
| Function argument | f : (Nat, X) -> Y |
| Return type | Buffer<Y> |
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(10);
intStorage.add(-2);
intStorage.add(-1);
intStorage.add(0);
func change(x : Int, y : Int) : Int {
x + y + 1;
};
let newBuffer : Buffer.Buffer<Int> = Buffer.mapEntries<Int, Int>(intStorage, change);
Buffer.toArray<Int>(newBuffer)
func mapFilter<X, Y>(
buffer : Buffer<X>
f : X -> ?Y
) : Buffer<Y>
| Parameters | |
| Generic parameters | X, Y |
| Variable argument | buffer : Buffer<X> |
| Function argument | f : X -> ?Y |
| Return type | Buffer<Y> |
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(10);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
intStorage.add(2);
func filter(x : Int) : ?Int {
if (x > 0) {
?(x * 10);
} else {
null;
};
};
let newBuffer : Buffer.Buffer<Int> = Buffer.mapFilter<Int, Int>(intStorage, filter);
Buffer.toArray<Int>(newBuffer)
func mapResult<X, Y, E>(
buffer : Buffer<X>
f : X -> Result.Result<Y, E>
) : Result.Result<Buffer<Y>, E>
| Parameters | |
| Generic parameters | X, Y, E |
| Variable argument | buffer : Buffer<X> |
| Function argument | f : X -> Result.Result<Y, E> |
| Return type | Result.Result<Buffer<Y>, E> |
import Buffer "mo:base/Buffer";
import Result "mo:base/Result";
let intStorage = Buffer.Buffer<Int>(10);
intStorage.add(0);
intStorage.add(1);
intStorage.add(2);
func filter(x : Int) : Result.Result<Int, Text> {
if (x > 0) {
#ok(x * 10);
} else {
#err("got negative number");
};
};
let result : Result.Result<Buffer.Buffer<Int>, Text> = Buffer.mapResult<Int, Int, Text>(intStorage, filter);
func toArray(arg : Buffer.Buffer<Int>) : [Int] {
let array : [Int] = Buffer.toArray<Int>(arg);
};
Result.mapOk<Buffer.Buffer<Int>, [Int], Text>(result, toArray)
func chain<X, Y>(
buffer : Buffer<X>
k : X -> Buffer<Y>
) : Buffer<Y>
| Parameters | |
| Generic parameters | X, Y |
| Variable argument | buffer : Buffer<X> |
| Function argument | k : X -> Buffer<Y> |
| Return type | Buffer<Y> |
import Buffer "mo:base/Buffer";
let intStorageA = Buffer.Buffer<Int>(0);
intStorageA.add(1);
intStorageA.add(2);
intStorageA.add(3);
func change(x : Int) : Buffer.Buffer<Int> {
let intStorageB = Buffer.Buffer<Int>(2);
intStorageB.add(x);
intStorageB.add(x ** 3);
intStorageB;
};
let chain = Buffer.chain<Int, Int>(intStorageA, change);
Buffer.toArray(chain);
func foldLeft<A, X>(
buffer : Buffer<X>
base : A
combine : (A, X) -> A
) : A
| Parameters | |
| Generic parameters | A, X |
| Variable argument1 | buffer : Buffer<X> |
| Variable argument2 | base : A |
| Function argument | combine : (A, X) -> A |
| Return type | A |
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(1);
intStorage.add(2);
func change(x : Int, y : Int) : Int {
x + y;
};
Buffer.foldLeft<Int, Int>(intStorage, 0, change)
func foldLeft<A, X>(
buffer : Buffer<X>
base : A
combine : (X, A) -> A
) : A
| Parameters | |
| Generic parameters | A, X |
| Variable argument1 | buffer : Buffer<X> |
| Variable argument2 | base : A |
| Function argument | combine : (X, A) -> A |
| Return type | A |
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(1);
intStorage.add(2);
func change(x : Int, y : Int) : Int {
x + y;
};
Buffer.foldRight<Int, Int>(intStorage, 0, change);
func first<X>(buffer : Buffer<X>) : X
| Parameters | |
| Generic parameters | X |
| Variable argument | buffer : Buffer<X> |
| Return type | X |
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(1);
intStorage.add(1);
func last<X>(buffer : Buffer<X>) : X
| Parameters | |
| Generic parameters | X |
| Variable argument | buffer : Buffer<X> |
| Return type | X |
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(1);
intStorage.add(1);
Buffer.last<Int>(intStorage)
func make<X>(element : X) : Buffer<X>
| Parameters | |
| Generic parameters | X |
| Variable argument | element : X |
| Return type | Buffer<X> |
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(1);
intStorage.add(1);
let make : Buffer.Buffer<Int> = Buffer.make<Int>(2);
Buffer.toArray(make)
func reverse<X>(buffer : Buffer<X>) : ()
| Parameters | |
| Generic parameters | X |
| Variable argument | buffer : Buffer<X> |
| Return type | () |
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
let reverse : () = Buffer.reverse<Int>(intStorage);
Buffer.toArray(intStorage)
func merge<X>(
buffer1 : Buffer<X>
buffer2 : Buffer<X>
compare : (X, X) -> Order
) : Buffer<X>
| Parameters | |
| Generic parameters | X |
| Variable argument1 | buffer1 : Buffer<X> |
| Variable argument2 | buffer2 : Buffer<X> |
| Function argument | combine : (X, X) -> Order |
| Return type | BufferX |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let intStorage1 = Buffer.Buffer<Int>(0);
intStorage1.add(-1);
intStorage1.add(0);
intStorage1.add(1);
let intStorage2 = Buffer.Buffer<Int>(0);
intStorage2.add(-2);
intStorage2.add(2);
intStorage2.add(3);
let merged : Buffer.Buffer<Int> = Buffer.merge<Int>(intStorage1, intStorage2, Int.compare);
Buffer.toArray<Int>(merged)
func removeDuplicates<X>(
buffer : Buffer<X>
compare : (X, X) -> Order
) : ()
| Parameters | |
| Generic parameters | X |
| Variable argument | buffer : Buffer<X> |
| Function argument | compare : (X, X) -> Order |
| Return type | () |
import Buffer "mo:base/Buffer";
import Int "mo:base/Int";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
intStorage.add(1);
intStorage.add(1);
let removeDuplicates : () = Buffer.removeDuplicates<Int>(intStorage, Int.compare);
Buffer.toArray(intStorage)
func partition<X>(
buffer : Buffer<X>
predicate : X -> Bool
) : (Buffer<X>, Buffer<X>)
| Parameters | |
| Generic parameters | X |
| Variable argument | buffer : Buffer<X> |
| Function argument | predicate : X -> Bool |
| Return type | (Buffer<X>, Buffer<X>) |
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-1);
intStorage.add(2);
intStorage.add(-2);
intStorage.add(1);
intStorage.add(3);
func part(x : Int) : Bool {
x > 0;
};
let partitions = Buffer.partition<Int>(intStorage, part);
let tuple : ([Int], [Int]) = (Buffer.toArray(partitions.0), Buffer.toArray(partitions.1))
func split<X>(
buffer : Buffer<X>
index : Nat
) : (Buffer<X>, Buffer<X>)
| Parameters | |
| Generic parameters | X |
| Variable argument1 | buffer : Buffer<X> |
| Variable argument2 | index : Nat |
| Return type | (Buffer<X>, Buffer<X>) |
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-2);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
intStorage.add(2);
let splits = Buffer.split<Int>(intStorage, 2);
let tuple : ([Int], [Int]) = (Buffer.toArray<Int>(splits.0), Buffer.toArray<Int>(splits.1))
func chunk<X>(
buffer : Buffer<X>
size : Nat
) : Buffer<Buffer<X>>
| Parameters | |
| Generic parameters | X |
| Variable argument1 | buffer : Buffer<X> |
| Variable argument2 | size : Nat |
| Return type | (Buffer<Buffer<X>>) |
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-2);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
intStorage.add(2);
let chunk : Buffer.Buffer<Buffer.Buffer<Int>> = Buffer.chunk<Int>(
intStorage,
3,
);
let array : [Buffer.Buffer<Int>] = Buffer.toArray<Buffer.Buffer<Int>>(chunk);
let array0 : [Int] = Buffer.toArray<Int>(array[0]);
let array1 : [Int] = Buffer.toArray<Int>(array[1]);
(array0, array1);
func groupBy<X>(
buffer : Buffer<X>
equal : (X, X) -> Bool
) : Buffer<Buffer<X>>
| Parameters | |
| Generic parameters | X |
| Variable argument | buffer : Buffer<X> |
| Function argument | equal : (X, X) -> Bool |
| Return type | (Buffer<Buffer<X>>) |
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-2);
intStorage.add(-2);
intStorage.add(0);
intStorage.add(0);
intStorage.add(2);
intStorage.add(2);
func edit(x : Int, y : Int) : Bool {
x == y;
};
let grouped : Buffer.Buffer<Buffer.Buffer<Int>> = Buffer.groupBy<Int>(
intStorage,
edit,
);
let array : [Buffer.Buffer<Int>] = Buffer.toArray<Buffer.Buffer<Int>>(grouped);
let array0 : [Int] = Buffer.toArray<Int>(array[0]);
let array1 : [Int] = Buffer.toArray<Int>(array[1]);
let array2 : [Int] = Buffer.toArray<Int>(array[2]);
(array0, array1, array2);
func flatten<X>(buffer : Buffer<Buffer<X>>) : Buffer<X>
| Parameters | |
| Generic parameters | X |
| Variable argument | buffer : Buffer<Buffer<X>> |
| Return type | Buffer<X> |
import Buffer "mo:base/Buffer";
let intStorageN = Buffer.Buffer<Int>(0);
intStorageN.add(-3);
intStorageN.add(-2);
intStorageN.add(-1);
let intStorageP = Buffer.Buffer<Int>(0);
intStorageP.add(0);
intStorageP.add(1);
intStorageP.add(3);
let bufferStorage = Buffer.Buffer<Buffer.Buffer<Int>>(1);
bufferStorage.add(intStorageN);
bufferStorage.add(intStorageP);
let flat : Buffer.Buffer<Int> = Buffer.flatten<Int>(bufferStorage);
Buffer.toArray<Int>(flat);
func zip<X, Y>(
buffer1 : Buffer<X>
buffer2 : Buffer<X>
) : Buffer<(X, Y)>
| Parameters | |
| Generic parameters | X, Y |
| Variable argument1 | buffer1 : Buffer<X> |
| Variable argument2 | buffer2 : Buffer<X> |
| Return type | Buffer<(X, Y)> |
import Buffer "mo:base/Buffer";
let intStorageN = Buffer.Buffer<Int>(0);
intStorageN.add(-3);
intStorageN.add(-2);
intStorageN.add(-1);
let intStorageP = Buffer.Buffer<Int>(0);
intStorageP.add(3);
intStorageP.add(2);
let zipped : Buffer.Buffer<(Int, Int)> = Buffer.zip<Int, Int>(
intStorageN,
intStorageP,
);
Buffer.toArray<(Int, Int)>(zipped)
func zipWith<X, Y, Z>(
buffer1 : Buffer<X>
buffer2 : Buffer<Y>
zip : (X, Y) -> Z
) : Buffer<Z>
| Parameters | |
| Generic parameters | X, Y, Z |
| Variable argument1 | buffer1 : Buffer<X> |
| Variable argument2 | buffer2 : Buffer<Y> |
| Function argument | zip : (X, Y) -> Z |
| Return type | Buffer<Z> |
import Buffer "mo:base/Buffer";
let intStorageN = Buffer.Buffer<Int>(0);
intStorageN.add(-3);
intStorageN.add(-2);
intStorageN.add(-1);
let intStorageP = Buffer.Buffer<Int>(0);
intStorageP.add(3);
intStorageP.add(2);
func edit(x : Int, y : Int) : Int {
x * y;
};
let zipped : Buffer.Buffer<Int> = Buffer.zipWith<Int, Int, Int>(
intStorageN,
intStorageP,
edit,
);
Buffer.toArray<Int>(zipped)
func takeWhile<X>(
buffer : Buffer<X>
predicate : X -> Bool
) : (Buffer<X>)
| Parameters | |
| Generic parameters | X |
| Variable argument | buffer : Buffer<X> |
| Function argument | predicate : X -> Bool |
| Return type | (Buffer<X>) |
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-3);
intStorage.add(-2);
intStorage.add(-1);
intStorage.add(0);
func check(x : Int) : Bool {
x < 0;
};
let newBuffer : Buffer.Buffer<Int> = Buffer.takeWhile<Int>(
intStorage,
check,
);
Buffer.toArray<Int>(newBuffer)
func dropWhile<X>(
buffer : Buffer<X>
predicate : X -> Bool
) : (Buffer<X>)
| Parameters | |
| Generic parameters | X |
| Variable argument | buffer : Buffer<X> |
| Function argument | predicate : X -> Bool |
| Return type | (Buffer<X>) |
import Buffer "mo:base/Buffer";
let intStorage = Buffer.Buffer<Int>(0);
intStorage.add(-3);
intStorage.add(-2);
intStorage.add(-1);
intStorage.add(0);
intStorage.add(1);
func check(x : Int) : Bool {
x < 0;
};
let newBuffer : Buffer.Buffer<Int> = Buffer.dropWhile<Int>(
intStorage,
check,
);
Buffer.toArray<Int>(newBuffer);