Buffer

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";

On this page

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

Type Buffer.Buffer<X>

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.

Class Buffer.Buffer<X>

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.

Class methods

myBuffer.size

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()

myBuffer.add

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)

myBuffer.get

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);

myBuffer.getOpt

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);

myBuffer.put

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);

myBuffer.removeLast

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);

myBuffer.remove

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);

myBuffer.clear

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);

myBuffer.filterEntries

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);

myBuffer.capacity

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();

myBuffer.reserve

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);

myBuffer.append

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);

myBuffer.insert

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);

myBuffer.insertBuffer

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);

myBuffer.sort

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);

myBuffer.vals

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);

Module public functions

Buffer.isEmpty

func isEmpty<X>(buffer : Buffer<X>) : Bool
Parameters
Generic parametersX
Variable argumentbuffer : Buffer<X>
Return typeBool
import Buffer "mo:base/Buffer";

let intStorage = Buffer.Buffer<Int>(0);

Buffer.isEmpty(intStorage);

Buffer.contains

func contains<X>(

  buffer : Buffer<X>
 element : X
   equal : (X, X) -> Bool

) : Bool
Parameters
Generic parametersX
Variable argument1buffer : Buffer<X>
Variable argument2element : X
Function argumentequal : (X, X) -> Bool
Return typeBool
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);

Buffer.clone

func clone<X>(buffer : Buffer<X>) : Buffer<X>
Parameters
Generic parametersX
Variable argumentbuffer : Buffer<X>
Return typeBuffer<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);

Buffer.max

func max<X>(

  buffer : Buffer<X>
 compare : (X, X) -> Order

 ) : ?X
Parameters
Generic parametersX
Variable argumentbuffer : Buffer<X>
Function argumentcompare : (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)

Buffer.min

func min<X>(

  buffer : Buffer<X>
 compare : (X, X) -> Order

 ) : ?X
Parameters
Generic parametersX
Variable argumentbuffer : Buffer<X>
Function argumentcompare : (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)

Buffer.equal

func equal<X>(

 buffer1 : Buffer<X>
 buffer2 : Buffer<X>
   equal : (X, X) -> Bool

   ) : Bool
Parameters
Generic parametersX
Variable argument1buffer1 : Buffer<X>
Variable argument2buffer2 : Buffer<X>
Function argumentequal : (X, X) -> Bool
Return typeBool
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);

Buffer.compare

func compare<X>(

 buffer1 : Buffer<X>
 buffer2 : Buffer<X>
 compare : (X, X) -> Order.Order

 ) : Order.Order
Parameters
Generic parametersX
Variable argument1buffer1 : Buffer<X>
Variable argument2buffer2 : Buffer<X>
Function argumentcompare : (X, X) -> Order.Order)
Return typeOrder.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);

Buffer.toText

func toText<X>(

 buffer : Buffer<X>
 toText : X -> Text

 ) : Text
Parameters
Generic parametersX
Variable argumentbuffer : Buffer<X>
Function argumenttoText : X -> Text
Return typeText
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)

Buffer.indexOf

func indexOf<X>(

element : X
 buffer : Buffer<X>
  equal : (X, X) -> Bool

  ) : ?Nat
Parameters
Generic parametersX
Variable argument1element : X
Variable argument2buffer : Buffer<X>
Function argumentequal : (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);

Buffer.lastIndexOf

func indexOf<X>(

element : X
 buffer : Buffer<X>
  equal : (X, X) -> Bool

  ) : ?Nat
Parameters
Generic parametersX
Variable argument1element : X
Variable argument2buffer : Buffer<X>
Function argumentequal : (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)

Buffer.indexOfBuffer

func indexOfBuffer<X>(

 subBuffer : Buffer<X>
    buffer : Buffer<X>
     equal : (X, X) -> Bool

    ) : ?Nat
Parameters
Generic parametersX
Variable argument1subBuffer : Buffer<X>
Variable argument2buffer : Buffer<X>
Function argumentequal : (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)

Buffer.binarySearch

func binarySearch<X>(

element : X
 buffer : Buffer<X>
compare : (X, X) -> Order.Order

  ) : ?Nat
Parameters
Generic parametersX
Variable argument1element : X
Variable argument2buffer : Buffer<X>
Function argumentcompare : (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);

Buffer.subBuffer

func subBuffer<X>(

    buffer : Buffer<X>
     start : Nat
    length : Nat

    ) : Buffer<X>
Parameters
Generic parametersX
Variable argument1buffer : Buffer<X>
Variable argument2start : Nat
variable argument3lenght : Nat
Return typeBuffer<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)

Buffer.isSubBufferOf

func isSubBufferOf<X>(

 subBuffer : Buffer<X>
    buffer : Buffer<X>
     equal : (X, X) -> Bool

    ) : Bool
Parameters
Generic parametersX
Variable argument1subBuffer : Buffer<X>
Variable argument2buffer : Buffer<X>
Function argumentequal : (X, X) -> Bool
Return typeBool
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)

Buffer.isStrictSubBufferOf

func isSubBufferOf<X>(

 subBuffer : Buffer<X>
    buffer : Buffer<X>
     equal : (X, X) -> Bool

    ) : Bool
Parameters
Generic parametersX
Variable argument1subBuffer : Buffer<X>
Variable argument2buffer : Buffer<X>
Function argumentequal : (X, X) -> Bool
Return typeBool
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)

Buffer.prefix

func prefix<X>(

    buffer : Buffer<X>
    length : Nat

    ) : Buffer<X>
Parameters
Generic parametersX
Variable argument1buffer : Buffer<X>
variable argument2lenght : Nat
Return typeBuffer<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)

Buffer.isPrefixOf

func isPrefixOf<X>(

    prefix : Buffer<X>
    buffer : Buffer<X>
     equal : (X, X) -> Bool

    ) : Bool
Parameters
Generic parametersX
Variable argument1prefix : Buffer<X>
Variable argument2buffer : Buffer<X>
Function argumentequal : (X, X) -> Bool
Return typeBool
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)

Buffer.isStrictPrefixOf

func isStrictPrefixOf<X>(

    prefix : Buffer<X>
    buffer : Buffer<X>
     equal : (X, X) -> Bool

    ) : Bool
Parameters
Generic parametersX
Variable argument1prefix : Buffer<X>
Variable argument2buffer : Buffer<X>
Function argumentequal : (X, X) -> Bool
Return typeBool
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)

Buffer.suffix

func suffix<X>(

    buffer : Buffer<X>
    length : Nat

    ) : Buffer<X>
Parameters
Generic parametersX
Variable argument1buffer : Buffer<X>
variable argument2lenght : Nat
Return typeBuffer<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)

Buffer.isSuffixOf

func isSuffixOf<X>(

    suffix : Buffer<X>
    buffer : Buffer<X>
     equal : (X, X) -> Bool

    ) : Bool
Parameters
Generic parametersX
Variable argument1suffix : Buffer<X>
Variable argument2buffer : Buffer<X>
Function argumentequal : (X, X) -> Bool
Return typeBool
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)

Buffer.isStrictSuffixOf

func isSuffixOf<X>(

    suffix : Buffer<X>
    buffer : Buffer<X>
     equal : (X, X) -> Bool

    ) : Bool
Parameters
Generic parametersX
Variable argument1suffix : Buffer<X>
Variable argument2buffer : Buffer<X>
Function argumentequal : (X, X) -> Bool
Return typeBool
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)

Buffer.forAll

func forAll<X>(

    buffer : Buffer<X>
 predicate : X -> Bool

    ) : Bool
Parameters
Generic parametersX
Variable argumentbuffer : Buffer<X>
Function argumentpredicate : X -> Bool
Return typeBool
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);

Buffer.forSome

func forSome<X>(

    buffer : Buffer<X>
 predicate : X -> Bool

    ) : Bool
Parameters
Generic parametersX
Variable argumentbuffer : Buffer<X>
Function argumentpredicate : X -> Bool
Return typeBool
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);

Buffer.forNone

func forNone<X>(

    buffer : Buffer<X>
 predicate : X -> Bool

    ) : Bool
Parameters
Generic parametersX
Variable argumentbuffer : Buffer<X>
Function argumentpredicate : X -> Bool
Return typeBool
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);

Buffer.toArray

func toArray<X>(buffer : Buffer<X>) : [X]
Parameters
Generic parametersX
Variable argumentbuffer : 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)

Buffer.toVarArray

func toVarArray<X>(buffer : Buffer<X>) : [var X]
Parameters
Generic parametersX
Variable argumentbuffer : 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)

Buffer.fromArray

func fromArray<X>(array : [X]) : Buffer<X>
Parameters
Generic parametersX
Variable argumentarray : [X]
Return typeBuffer<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);

Buffer.fromVarArray

func fromArray<X>(array : [var X]) : Buffer<X>
Parameters
Generic parametersX
Variable argumentarray : [var X]
Return typeBuffer<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);

Buffer.fromIter

func fromIter<X>(iter : { next : () -> ?X }) : Buffer<X>
Parameters
Generic parametersX
Variable argumentiter : { next : () -> ?X }
Return typeBuffer<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);

Buffer.trimToSize

func trimToSize<X>(buffer : Buffer<X>) : ()
Parameters
Generic parametersX
Variable argumentbuffer : 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()

Buffer.map

func map<X, Y>(

buffer : Buffer<X>
     f : X -> Y

  ) : Buffer<Y>
Parameters
Generic parametersX, Y
Variable argumentbuffer : Buffer<X>
Function argumentf : X -> Y
Return typeBuffer<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)

Buffer.iterate

func iterate<X>(

buffer : Buffer<X>
     f : X -> ()

  ) : ()
Parameters
Generic parametersX
Variable argumentbuffer : Buffer<X>
Function argumentf : 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)

Buffer.mapEntries

func mapEntries<X, Y>(

buffer : Buffer<X>
     f : (Nat, X) -> Y

 ) : Buffer<Y>
Parameters
Generic parametersX, Y
Variable argumentbuffer : Buffer<X>
Function argumentf : (Nat, X) -> Y
Return typeBuffer<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)

Buffer.mapFilter

func mapFilter<X, Y>(

buffer : Buffer<X>
     f :  X -> ?Y

 ) : Buffer<Y>
Parameters
Generic parametersX, Y
Variable argumentbuffer : Buffer<X>
Function argumentf : X -> ?Y
Return typeBuffer<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)

Buffer.mapResult

func mapResult<X, Y, E>(

buffer : Buffer<X>
     f : X -> Result.Result<Y, E>

     ) : Result.Result<Buffer<Y>, E>
Parameters
Generic parametersX, Y, E
Variable argumentbuffer : Buffer<X>
Function argumentf : X -> Result.Result<Y, E>
Return typeResult.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)

Buffer.chain

func chain<X, Y>(

buffer : Buffer<X>
     k : X -> Buffer<Y>

     ) : Buffer<Y>
Parameters
Generic parametersX, Y
Variable argumentbuffer : Buffer<X>
Function argumentk : X -> Buffer<Y>
Return typeBuffer<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);

Buffer.foldLeft

func foldLeft<A, X>(

   buffer : Buffer<X>
     base : A
  combine : (A, X) -> A

      ) : A
Parameters
Generic parametersA, X
Variable argument1buffer : Buffer<X>
Variable argument2base : A
Function argumentcombine : (A, X) -> A
Return typeA
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)

Buffer.foldRight

func foldLeft<A, X>(

   buffer : Buffer<X>
     base : A
  combine : (X, A) -> A

      ) : A
Parameters
Generic parametersA, X
Variable argument1buffer : Buffer<X>
Variable argument2base : A
Function argumentcombine : (X, A) -> A
Return typeA
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);

Buffer.first

func first<X>(buffer : Buffer<X>) : X
Parameters
Generic parametersX
Variable argumentbuffer : Buffer<X>
Return typeX
import Buffer "mo:base/Buffer";

let intStorage = Buffer.Buffer<Int>(0);

intStorage.add(-1);
intStorage.add(1);
intStorage.add(1);

Buffer.last

func last<X>(buffer : Buffer<X>) : X
Parameters
Generic parametersX
Variable argumentbuffer : Buffer<X>
Return typeX
import Buffer "mo:base/Buffer";

let intStorage = Buffer.Buffer<Int>(0);

intStorage.add(-1);
intStorage.add(1);
intStorage.add(1);

Buffer.last<Int>(intStorage)

Buffer.make

func make<X>(element : X) : Buffer<X>
Parameters
Generic parametersX
Variable argumentelement : X
Return typeBuffer<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)

Buffer.reverse

func reverse<X>(buffer : Buffer<X>) : ()
Parameters
Generic parametersX
Variable argumentbuffer : 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)

Buffer.merge

func merge<X>(

  buffer1 : Buffer<X>
  buffer2 : Buffer<X>
  compare : (X, X) -> Order

  ) : Buffer<X>
Parameters
Generic parametersX
Variable argument1buffer1 : Buffer<X>
Variable argument2buffer2 : Buffer<X>
Function argumentcombine : (X, X) -> Order
Return typeBufferX
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)

Buffer.removeDuplicates

func removeDuplicates<X>(

     buffer : Buffer<X>
    compare : (X, X) -> Order

    ) : ()
Parameters
Generic parametersX
Variable argumentbuffer : Buffer<X>
Function argumentcompare : (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)

Buffer.partition

func partition<X>(

    buffer : Buffer<X>
 predicate : X -> Bool

 ) : (Buffer<X>, Buffer<X>)
Parameters
Generic parametersX
Variable argumentbuffer : Buffer<X>
Function argumentpredicate : 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))

Buffer.split

func split<X>(

 buffer : Buffer<X>
  index : Nat

 ) : (Buffer<X>, Buffer<X>)
Parameters
Generic parametersX
Variable argument1buffer : Buffer<X>
Variable argument2index : 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))

Buffer.chunk

func chunk<X>(

   buffer : Buffer<X>
     size : Nat

 ) : Buffer<Buffer<X>>
Parameters
Generic parametersX
Variable argument1buffer : Buffer<X>
Variable argument2size : 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);

Buffer.groupBy

func groupBy<X>(

   buffer : Buffer<X>
    equal : (X, X) -> Bool

 ) : Buffer<Buffer<X>>
Parameters
Generic parametersX
Variable argumentbuffer : Buffer<X>
Function argumentequal : (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);

Buffer.flatten

func flatten<X>(buffer : Buffer<Buffer<X>>) : Buffer<X>
Parameters
Generic parametersX
Variable argumentbuffer : Buffer<Buffer<X>>
Return typeBuffer<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);

Buffer.zip

func zip<X, Y>(

  buffer1 : Buffer<X>
  buffer2 : Buffer<X>

  ) : Buffer<(X, Y)>
Parameters
Generic parametersX, Y
Variable argument1buffer1 : Buffer<X>
Variable argument2buffer2 : Buffer<X>
Return typeBuffer<(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)

Buffer.zipWith

func zipWith<X, Y, Z>(

buffer1 : Buffer<X>
buffer2 : Buffer<Y>

    zip : (X, Y) -> Z

      ) : Buffer<Z>
Parameters
Generic parametersX, Y, Z
Variable argument1buffer1 : Buffer<X>
Variable argument2buffer2 : Buffer<Y>
Function argumentzip : (X, Y) -> Z
Return typeBuffer<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)

Buffer.takeWhile

func takeWhile<X>(

    buffer : Buffer<X>
 predicate : X -> Bool

 ) : (Buffer<X>)
Parameters
Generic parametersX
Variable argumentbuffer : Buffer<X>
Function argumentpredicate : 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)

Buffer.dropWhile

func dropWhile<X>(

    buffer : Buffer<X>
 predicate : X -> Bool

 ) : (Buffer<X>)
Parameters
Generic parametersX
Variable argumentbuffer : Buffer<X>
Function argumentpredicate : 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);