如何执行在DAML N次一些代码?

问题描述 投票:1回答:1

说我有一个选择是使用表示一个数一个整数,我想创建一个合同中的时间,即数执行的代码,很多时候一些块。

在Ruby例如,这可能是这样的:

n.times do 
  #run some code here
end

如何在DAML实现这一目标?

daml
1个回答
2
投票

TLDR

要应用台账操作N倍,最简单的方法是使用replicateA功能从DA.Action

Example

daml 1.2
module ReplicateDaml
where

import DA.Action

template Demo
  with
    sig: Party
    total: Int
  where
    signatory sig

testReplicate = scenario do
  p <- getParty "party"
  let
    total = 10

  p `submit` replicateA total $ create Demo with sig=p; total

Discussion

对于replicateA类型签名是:

-- | `replicateA n act` performs the action n times, gathering the results.
replicateA : (Applicative m) => Int -> m a -> m [a]

您可以阅读如下:

此功能支持具有用于m类型类(API或接口)的实例(实现)的任何类型的Applicative。它的第一个参数是一个Int其第二个是类型m提供类型的值a它返回重复效果N次,收集的结果以列表的的结果的“效果”

您所描述的create的类型是:Update (ContractId a);和Update实例化(具有实现)的Applicative类型类,你可以使用上Applicative的作品上Update的任何功能 - 其中自然包括replicateA

当使用这种方式,代替Updatem和类型签名(ContractId t)a,所以:

replicateA : Int -> Update (ContractId t) -> Update [ContractId t]

© www.soinside.com 2019 - 2024. All rights reserved.