在DAML选择中行使多个更新

时间:2019-09-23 06:36:26

标签: daml

在DAML中,是否可以使用类似地图的函数来遍历合同编号列表,检索合同编号并在每个合同编号上执行选择?在DAML中,这似乎受到限制,因为执行选择时所有内容都需要包装在一个Update中。

这是我尝试过的一个例子(注意到这些问题):

# Set up various surfaces for UI containers
GAME_SURFACE = pygame.Surface((1350, 755))
GAME_SURFACE_RECT = pygame.Surface.get_rect(GAME_SURFACE)
STATS_SURFACE = pygame.Surface((1350, 125))
CHAR_DETAILS_SURFACE = pygame.Surface((570, 540))
ENEMY_DETAILS_SURFACE = pygame.Surface((570, 540))
LOG_SURFACE = pygame.Surface((1350, 200))

# Initialise Objects
PLAYER = Entity(0, 0, PLAYER_CHAR, WHITE, GAME_SURFACE)

...

# --- Screen-clearing code goes here
SCREEN.fill(BLACK)
GAME_SURFACE.fill(BLACK)
STATS_SURFACE.fill(RED)
CHAR_DETAILS_SURFACE.fill(WHITE)
ENEMY_DETAILS_SURFACE.fill(GREEN)
LOG_SURFACE.fill(RED)

# --- Drawing code should go here
SCREEN.blit(STATS_SURFACE, (0, 0))
SCREEN.blit(CHAR_DETAILS_SURFACE, (1350, 0))
SCREEN.blit(ENEMY_DETAILS_SURFACE, (1350, 540))
SCREEN.blit(LOG_SURFACE, (0, 880))
SCREEN.blit(GAME_SURFACE, (0, 125))
PLAYER.draw()

如果这可以解决,您还可以解释为什么在DAML中返回多个元组时,它们似乎隐式地从exerciseChoice: ContractId ContractB -> Update (ContractId ContractB) exerciseChoice contractB = do (exercise contractB UpdateB with newText = "test") template ContractA with party : Party contracts: [ContractId ContractB] where signatory party controller party can nonconsuming UpdateA : [Update (ContractId ContractB)] with newText : Text do -- a <- create ContractB with party = party; text = newText -- a2 <- exerciseChoice a -- return [a2] #these lines work fine return map exerciseChoice contracts -- #this doesn't work due to DAML implicitly adding Update before return definition -- i.e. DAML expects type 'Update [Update (ContractId ContractB)]' based on this signature -- we need a function which converts something like: -- '[Update (ContractId ContractB)] -> Update [ContractId ContractB]' template ContractB with party : Party text: Text where signatory party controller party can UpdateB: ContractId ContractB with newText: Text do create this with text = newText 强制转换为(Update (ContractId A), Update (ContractId B))吗?

1 个答案:

答案 0 :(得分:1)

函数map的类型为(a -> b) -> [a] -> [b],因此map exerciseChoice contracts的类型为[Update (ContractId ContractB)]。将动作列表变成单个动作在概念上很简单。产生的动作是依次执行列表中的每个动作的动作。实际上,有一个功能可以使用:sequence : (Applicative m) => [m a] -> m [a]Update是应用程序,因此您可以编写sequence (map exerciseChoice contracts)。但是,这是一项常见的任务,因此存在专门的功能mapA : (Applicative m) => (a -> m b) -> [a] -> m [b]

mapA exerciseChoice contracts应该可以解决问题。