和弦有什么用?

发布于 2024-07-25 06:04:44 字数 43 浏览 9 评论 0原文

Nemerle 等语言支持和弦的概念。 我想知道它们的实际用途是什么。

Languages such as Nemerle support the idea of chords. I'd like to know what their practical use is.

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

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

发布评论

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

评论(3

青丝拂面 2024-08-01 06:04:44

该结构似乎也存在于 语言(以及 Polyphonic C# ),至少根据 [Wikipedia](http://en.wikipedia.org/wiki /Chord_(并发)

和弦的主要用途似乎涉及数据库编程(更具体地说,加入微积分),鉴于它是一个并发结构,这并不奇怪。恐怕我不知道。

The construct also seems to exist in the language (as well as Polyphonic C#), at least according to [Wikipedia](http://en.wikipedia.org/wiki/Chord_(concurrency).

The primary usage of chords appears to involve database programming (more specifically, join calculus), which is unsurprising given that it is a concurrency construct. More than that, I'm afraid I don't know.

白云悠悠 2024-08-01 06:04:44

和弦用于并发。 该定义可在此处获取。

您正在寻找的位:

在大多数语言中,包括 C#,类签名中的方法与其实现的代码是双射对应的——对于声明的每个方法,都有一个单独的、不同的定义,说明当该方法被调用时会发生什么。叫。 然而,在 Cω 中,主体可以与一组(同步和/或异步)方法相关联。 我们把这样的定义称为和弦,特定的方法可能会出现在几个和弦的头部。 和弦的主体只有在其标头中的所有方法都被调用后才能执行。 因此,当调用一个方法时,可能会启用零个、一个或多个和弦:

如果没有启用和弦,则该方法
调用已排队。 如果方法
是异步的,那么这只是
涉及添加参数(
消息的内容)到队列。
如果该方法是同步的,那么
调用线程被阻塞。 如果有
是一个启用的和弦,那么
涉及调用的参数
匹配已出队,任何被阻止
参与比赛的线程是
醒来,身体运转。 当一个
仅涉及异步的和弦
方法运行,然后它在一个新的
线。 如果有几个和弦
然后启用一个未指定的
其中一名被选中参加竞选。
同样,如果有多个调用
对于排队的特定方法,我们
不指定哪个电话将被
当有匹配时出队。

A chord is used for concurrency. The definition is available here.

The bit you are looking for:

In most languages, including C#, methods in the signature of a class are in bijective correspondence with the code of their implementations -- for each method which is declared, there is a single, distinct definition of what happens when that method is called. In Cω, however, a body may be associated with a set of (synchronous and/or asynchronous) methods. We call such a definition a chord, and a particular method may appear in the header of several chords. The body of a chord can only execute once all the methods in its header have been called. Thus, when a method is called there may be zero, one, or more chords which are enabled:

If no chord is enabled then the method
invocation is queued up. If the method
is asynchronous, then this simply
involves adding the arguments (the
contents of the message) to a queue.
If the method is synchronous, then the
calling thread is blocked. If there
is a single enabled chord, then the
arguments of the calls involved in the
match are de-queued, any blocked
thread involved in the match is
awakened, and the body runs. When a
chord which involves only asynchronous
methods runs, then it does so in a new
thread. If there are several chords
which are enabled then an unspecified
one of them is chosen to run.
Similarly, if there are multiple calls
to a particular method queued up, we
do not specify which call will be
de-queued when there is a match.

五里雾 2024-08-01 06:04:44

尝试 Nemerle 计算表达式:

https://code.google .com/p/nemerle/source/browse/nemerle/trunk/snippets/ComputationExpressions/

一些示例:

 def upTo (n : int)
  {
    comp enumerable
    {
      mutable i = 0;
      while (i < n)
      {
        i ++;
        yield i
      }
    }
  }

  def manyTimes : IEnumerable [int] =
    comp enumerable
    {
      yieldcomp upTo(2);   // 1 2
      yield 100;           // 100
      yieldcomp upTo(3);   // 1 2 3
      yield 100;           // 100
      yieldcomp upTo(10);  // 1 2 3 .. 10
    }

def fn(n)
  {
    comp async
    {
      if (n < 20)
        returncomp fn(n + 1);
      else
        return n;
    }
  }
  def f(n1, n2)
  {
    comp async
    {
      defcomp n1 = fn(n1);
      defcomp n2 = fn(n2);
      return $"$n1 $n2";
    }
  }

private HttpGet(url : string) : Async[string]
{
  comp async
  {
    def req = WebRequest.Create(url);
    using (defcomp resp = req.AsyncGetResponse())
    using (stream = resp.GetResponseStream())
    using (reader = StreamReader(stream))
      return reader.ReadToEnd();
  }
}

这里还有更多示例:(虽然文章是俄语,但代码是英语:) ) http://habrahabr.ru/blogs/programming/108184/

Try Nemerle Computation Expressions:

https://code.google.com/p/nemerle/source/browse/nemerle/trunk/snippets/ComputationExpressions/

Some examples:

 def upTo (n : int)
  {
    comp enumerable
    {
      mutable i = 0;
      while (i < n)
      {
        i ++;
        yield i
      }
    }
  }

  def manyTimes : IEnumerable [int] =
    comp enumerable
    {
      yieldcomp upTo(2);   // 1 2
      yield 100;           // 100
      yieldcomp upTo(3);   // 1 2 3
      yield 100;           // 100
      yieldcomp upTo(10);  // 1 2 3 .. 10
    }

def fn(n)
  {
    comp async
    {
      if (n < 20)
        returncomp fn(n + 1);
      else
        return n;
    }
  }
  def f(n1, n2)
  {
    comp async
    {
      defcomp n1 = fn(n1);
      defcomp n2 = fn(n2);
      return $"$n1 $n2";
    }
  }

private HttpGet(url : string) : Async[string]
{
  comp async
  {
    def req = WebRequest.Create(url);
    using (defcomp resp = req.AsyncGetResponse())
    using (stream = resp.GetResponseStream())
    using (reader = StreamReader(stream))
      return reader.ReadToEnd();
  }
}

Some more examples here: (Although article in Russian but code in English :) ) http://habrahabr.ru/blogs/programming/108184/

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