Skip to content

Instantly share code, notes, and snippets.

@flyq
Last active May 11, 2021 03:11
Show Gist options
  • Save flyq/804a4c48b5ec5cee3adc7c18ebccd29b to your computer and use it in GitHub Desktop.
Save flyq/804a4c48b5ec5cee3adc7c18ebccd29b to your computer and use it in GitHub Desktop.
import List "mo:base/List";
actor Example {
var sum = 0;
private var test = List.fromArray<Nat>([5,4,3,2,1,0]);
private var test1 = List.nil<Nat>();
let f2 = func self(x: Nat) {
sum := sum + x;
};
public func sumf() : async Nat {
List.iterate(test, f2);
return sum;
};
public func set() : async [Nat] {
List.iterate(test, func(x: Nat){
test1 := List.push<Nat>(x, test1); // 注意 List 里面 push 的实现是在 List 头加入的,因此反序
});
return List.toArray(test1);
};
};
/* 结果
➜ beta dfx canister call beta sumf
(15)
➜ beta sudo dfx canister install beta -m=reinstall
➜ beta dfx canister call beta set
(vec { 0; 1; 2; 3; 4; 5 })
➜ beta
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment