<< 和>> Erlang 中的符号

发布于 2024-07-29 06:11:00 字数 965 浏览 4 评论 0 原文

首先,我是一名 Erlang 菜鸟。 我需要与 MySQL 数据库交互,我找到了 erlang-mysql-driver 。 我正在尝试,但对某些语法有点困惑。

我可以用这个从数据库中获取一行数据(为了简洁起见,这里大大简化了):

Result = mysql:fetch(P1, ["SELECT column1, column2 FROM table1 WHERE column2='", Key, "'"]),  
case Result of  
    {data, Data} ->  
        case mysql:get_result_rows(Data) of  
            [] -> not_found;  
            Res ->  
              %% Now 'Res' has the row

所以现在这是“Res”的一个示例:

[[<<"value from column1">>, <<"value from column2">>]]

我知道它是一个记录列表。 在本例中,查询返回 1 行 2 列。

我的问题是:
<<>> 符号的含义是什么? 将这样的列表转换为我定义的记录的最佳(Erlang 推荐)语法是什么:

-record(  
    my_record,  
    {   
      column1 = ""  
      ,column2 = ""  
    }  
 ).     

First of all, I'm an Erlang rookie here. I need to interface with a MySQL database and I found the erlang-mysql-driver. I'm trying that out, and am a little confused by some of the syntax.

I can get a row of data from the database with this (greatly oversimplified for brevity here):

Result = mysql:fetch(P1, ["SELECT column1, column2 FROM table1 WHERE column2='", Key, "'"]),  
case Result of  
    {data, Data} ->  
        case mysql:get_result_rows(Data) of  
            [] -> not_found;  
            Res ->  
              %% Now 'Res' has the row

So now here is an example of what `Res' has:

[[<<"value from column1">>, <<"value from column2">>]]

I get that it's a list of records. In this case, the query returned 1 row of 2 columns.

My question is:
What do the << and >> symbols mean? And what is the best (Erlang-recommended) syntax for turning a list like this into a records which I have defined like:

-record(  
    my_record,  
    {   
      column1 = ""  
      ,column2 = ""  
    }  
 ).     

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

这个俗人 2024-08-05 06:11:00

需要注意的是:结果并不是位串推导式,而是位串。 但是,您可以使用位串推导式来生成位串序列(上面使用生成器等进行了描述),就像列表和列表推导式一样。

您可以使用 erlang:binary_to_list/1 和 erlang:list_to_binary/1 在二进制和字符串(列表)之间进行转换。

mysql 驱动程序返回位字符串的原因可能是因为它们的操作速度要快得多。

Just a small note: the results are not bit string comprehensions per see, they are just bit strings. However you can use bit string comprehensions to produce a sequence of bit strings (which is described above with the generators and that), much like lists and lists comprehensions.

you can use erlang:binary_to_list/1 and erlang:list_to_binary/1 to convert between binary and strings (lists).

The reason the mysql driver returns bit strings is probably because they are much faster to manipulate.

悍妇囚夫 2024-08-05 06:11:00

在您的具体示例中,您可以通过匹配返回的列值来进行转换,然后创建一个新记录,如下所示:

case mysql:get_result_rows(Data) of
  [] ->
    not_found;  
  [[Col1, Col2]] ->  
    #my_record{column1 = Col1, column2 = Col2}
end

In your specific example, you can do the conversion by matching on the returned column values, and then creating a new record like this:

case mysql:get_result_rows(Data) of
  [] ->
    not_found;  
  [[Col1, Col2]] ->  
    #my_record{column1 = Col1, column2 = Col2}
end
冰雪梦之恋 2024-08-05 06:11:00

这些是位串推导式。

位串推导式类似于列表推导式。 它们用于高效、简洁地生成位串。

位串推导式使用以下语法编写:

<< BitString || Qualifier1,...,QualifierN >>

BitString 是位串表达式,每个限定符是生成器、位串生成器或过滤器。

• 生成器写为:

 Pattern <- ListExpr. 

ListExpr 必须是一个计算结果为术语列表的表达式。

• 位串生成器编写为:

 BitstringPattern <= BitStringExpr. 

BitStringExpr 必须是计算结果为位串的表达式。

• 过滤器是一个计算结果为 true 或 false 的表达式。
生成器模式中的变量隐藏了围绕位串推导式的函数子句中的变量。

位串推导式返回一个位串,该位串是通过连接对所有过滤器都为 true 的位串生成器元素的每个组合评估 BitString 的结果而创建的。

例子:

1> << << (X*2) >> || 
<<X>> <= << 1,2,3 >> >>.
<<2,4,6>>

These are bit string comprehensions.

Bit string comprehensions are analogous to List Comprehensions. They are used to generate bit strings efficiently and succinctly.

Bit string comprehensions are written with the following syntax:

<< BitString || Qualifier1,...,QualifierN >>

BitString is a bit string expression, and each Qualifier is either a generator, a bit string generator or a filter.

• A generator is written as:

 Pattern <- ListExpr. 

ListExpr must be an expression which evaluates to a list of terms.

• A bit string generator is written as:

 BitstringPattern <= BitStringExpr. 

BitStringExpr must be an expression which evaluates to a bitstring.

• A filter is an expression which evaluates to true or false.
The variables in the generator patterns shadow variables in the function clause surrounding the bit string comprehensions.

A bit string comprehension returns a bit string, which is created by concatenating the results of evaluating BitString for each combination of bit string generator elements for which all filters are true.

Example:

1> << << (X*2) >> || 
<<X>> <= << 1,2,3 >> >>.
<<2,4,6>>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文