7po3j-syaaa-aaaal-qbqea-cai.icp0.io
Open in
urlscan Pro
2a00:fb01:400:200:5000:45ff:feb5:f777
Public Scan
URL:
https://7po3j-syaaa-aaaal-qbqea-cai.icp0.io/base-library/data-structures/buffer.html
Submission: On December 19 via api from US — Scanned from FR
Submission: On December 19 via api from US — Scanned from FR
Form analysis
1 forms found in the DOM<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
Text Content
1. The Motoko Programming Language 2. 3. Part 1 4. 1. Introduction ❱ 5. 1. 1.1. Getting Started 6. 2. Common Programming Concepts ❱ 7. 1. 2.1. Variables 2. 2.2. Mutability 3. 2.3. Comments 4. 2.4. Types ❱ 5. 1. 2.4.1. Tuples 2. 2.4.2. Records 3. 2.4.3. Variants 4. 2.4.4. Immutable Arrays 5. 2.4.5. Mutable Arrays 6. 2.5. Operators ❱ 7. 1. 2.5.1. Numeric operators 2. 2.5.2. Relational operators 3. 2.5.3. Assignment operators 4. 2.5.4. Text concatenation 5. 2.5.5. Logical expressions 6. 2.5.6. Bitwise operators 7. 2.5.7. Operator precedence 8. 2.6. Pattern Matching 9. 2.7. Functions 10. 2.8. Options and Results 11. 2.9. Control Flow ❱ 12. 1. 2.9.1. If Expression 2. 2.9.2. If Else Expression 3. 2.9.3. Switch Expression 13. 2.10. Objects and Classes ❱ 14. 1. 2.10.1. Objects 2. 2.10.2. Classes 15. 2.11. Modules and Imports 16. 2.12. Assertions 8. 3. Internet Computer Programming Concepts ❱ 9. 1. 3.1. Actors ❱ 2. 1. 3.1.1. From Actor to Canister 2. 3.1.2. Canister Calls from Clients 3. 3.2. Principals and Authentication 4. 3.3. Async Data ❱ 5. 1. 3.3.1. Shared Types 2. 3.3.2. Candid 6. 3.4. Basic Memory Persistence ❱ 7. 1. 3.4.1. Upgrades 2. 3.4.2. Stable Variables 10. 11. Part 2 12. 4. Advanced Types ❱ 13. 1. 4.1. Generic Types 2. 4.2. Subtyping 3. 4.3. Recursive Types 4. 4.4. Type Bounds 14. 5. The Base Library ❱ 15. 1. 5.1. Primitive Types ❱ 2. 1. 5.1.1. Bool 2. 5.1.2. Nat 3. 5.1.3. Int 4. 5.1.4. Float 5. 5.1.5. Principal 6. 5.1.6. Text 7. 5.1.7. Char 8. 5.1.8. Bounded Number Types ❱ 9. 1. 5.1.8.1. Nat8 2. 5.1.8.2. Nat16 3. 5.1.8.3. Nat32 4. 5.1.8.4. Nat64 5. 5.1.8.5. Int8 6. 5.1.8.6. Int16 7. 5.1.8.7. Int32 8. 5.1.8.8. Int64 10. 5.1.9. Blob 3. 5.2. Utility Modules ❱ 4. 1. 5.2.1. Iterators 2. 5.2.2. Hash 3. 5.2.3. Option 4. 5.2.4. Result 5. 5.2.5. Order 6. 5.2.6. Error 7. 5.2.7. Debug 5. 5.3. Data Structures ❱ 6. 1. 5.3.1. Array 2. 5.3.2. List 3. 5.3.3. Buffer 4. 5.3.4. HashMap 5. 5.3.5. RBTree 7. 5.4. More Data Structures 8. 5.5. IC APIs ❱ 9. 1. 5.5.1. Time 2. 5.5.2. Timer 3. 5.5.3. CertifiedData 4. 5.5.4. Random 5. 5.5.5. Experimental 16. 6. Advanced Concepts ❱ 17. 1. 6.1. Async Programming 2. 6.2. Scalability ❱ 3. 1. 6.2.1. Actor Classes 2. 6.2.2. Stable Storage 4. 6.3. System API's ❱ 5. 1. 6.3.1. Message Inspection 2. 6.3.2. Timers 3. 6.3.3. Certified Variables 4. 6.3.4. Pre-upgrade and Post-upgrade 5. 6.3.5. Cryptographic Randomness 18. 19. Part 3 20. 7. Project Deployment ❱ 21. 1. 7.1. Installing the SDK 2. 7.2. Local Deployment 3. 7.3. Canister Status 4. 7.4. Identities and PEM Files 5. 7.5. Cycles and ICP 6. 7.6. Cycles Wallet 7. 7.7. IC Deployment 22. 8. Common Internet Computer Canisters ❱ 23. 1. 8.1. IC Management Canister 2. 8.2. ICP Ledger Canister 3. 8.3. Cycle Minting Canister 24. 9. Internet Computer Standards ❱ 25. 1. 9.1. ICRC1 26. 10. Tokenized Comments Example 27. 28. APPENDIX 29. 11. TABLES * Light * Rust * Coal * Navy * Ayu THE MOTOKO PROGRAMMING LANGUAGE BOOK 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);