从之前的字典创建一个新的键值字典

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

所以,我有一个名为

conquered
的字典。该字典的用途是存储列表的键、值字典,以使用分而治之方法的并发性来搜索值。

代码继续:

-module(divide).
-export([divide_the_cache/4]).

-define(divided, dict:new()).

divide_the_cache(cache, divided, thread_index, acc) ->
    case length(cache) =< acc of
        thread_index ->
            l = lists:merge([lists:nth(thread_index, divided)], [lists:nth(acc, cached)]);
            conquered = dict:append(thread_index, l, dict:from_list(divided));
            thread_index = if length(l) == 24 -> thread_index = thread_index + 1, end;

            divide_the_cache(cache, conquered, thread_index, acc+1);
    end.

我得到:

divide.erl:10:69: syntax error before: ';'
%   10|             conquered = dict:append(thread_index, l, dict:from_list(divided));
%     | 

当我不在 dict:append 上使用函数组合时,我得到:

divide.erl:10:36: syntax error before: ';'
%   10|             d_list = dict:from_list(divided);
%     |                                             ^

我已经查阅了网络上的文档和网站,但可能会遇到一个示例(除了使用 dict:from_list )并按照尝试 repl 的文档进行操作。

可能会出现什么问题?实现我的需求的最佳方法是什么?

预先感谢,卢卡斯。

dictionary erlang
1个回答
0
投票

您的代码有多个问题,其中大多数是语法问题。让我尝试以更“语法正确”的方式重写它。然后,您需要自行修复您想要赋予它的任何语义: -module(divide). -export([divide_the_cache/4]). divide_the_cache(Cache, Divided, ThreadIndex, Acc) when length(Cache) =< Acc -> L = lists:merge( [lists:nth(ThreadIndex, dict:to_list(Divided))], [lists:nth(Acc, Cache)] ), Conquered = dict:append(ThreadIndex, L, Divided), NewThreadIndex = case length(L) of 24 -> ThreadIndex + 1; _ -> ThreadIndex end, divide_the_cache(Cache, Conquered, NewThreadIndex, Acc+1).

我改变了什么:

Erlang 中的

    -define
  1. 用于宏,而您没有在任何地方使用
    ?divided
    宏。
    Erlang 中的变量以大写字母开头。
  2. case length(Cache) =< Acc of ThreadIndex ->
  3. 对我来说毫无意义,所以我用一个守卫代替了它(
    when length(Cache) =< Acc
    )。
  4. Divided
  5. 应该是一个字典或一个列表,但它首先被视为一个列表,然后是一个字典......所以我决定它始终是一个字典,因此,对于
    lists:nth/2
    ,我使用了
    dict:to_list/1
    在 Erlang 中,表达式以 
  6. ,
  7. 结尾(而不是
    ;
    ),函数以
    .
    结尾。
    你不应该在 Erlang 中编写 else-less if,所以我将你的 
  8. if
  9. 变成了
    case
    语句。
    Erlang 中的变量不能重新绑定,这就是我引入
  10. NewThreadIndex
  11. 的原因。
    
        
© www.soinside.com 2019 - 2024. All rights reserved.