【问题标题】:Io language 'apply arguments'Io 语言“应用参数”
【发布时间】:2010-12-12 00:25:30
【问题描述】:

在Io编程语言中,有没有相当于lisp的apply函数。

例如,我有一个包装 writeln 的方法:

mymeth := method(
              //do some extra stuff

             writeln(call message arguments))
)

目前这只是打印列表,而不是评估它的内容,就好像它们是它自己的参数一样。

【问题讨论】:

    标签: iolanguage


    【解决方案1】:

    感谢建议 evalArgs 的人(不确定您的评论去了哪里)。

    无论如何,这已经解决了我的情况,但不幸的是我猜一般不会。

    您可以通过这样做来实现我所描述的:

    writeln(call evalArgs join)

    这会评估所有参数,然后将结果连接成一个字符串。

    【讨论】:

      【解决方案2】:

      是否有与 lisp 的 apply 函数等价的功能?

      看看performperformWithArgList 方法。


      退后一步,您可以在 Io 中以几种不同的方式复制 Lisp 的 FUNCALL

      1 - 传递一个函数,即。块():

      plotf := method (fn, min, max, step,
          for (i, min, max, step,
              fn call(i) roundDown repeat(write("*"))
              writeln
          )
      )
      
      plotf( block(n, n exp), 0, 4, 1/2 )
      

      2 - 传递消息:

      plotm := method (msg, min, max, step,
          for (i, min, max, step,
              i doMessage(msg) roundDown repeat(write("*"))
              writeln
          )
      )
      
      plotm( message(exp), 0, 4, 1/2 )
      

      3 - 将函数名作为字符串传递:

      plots := method (str, min, max, step,
          for (i, min, max, step,
              i perform(str) roundDown repeat(write("*"))
              writeln
          )
      )
      
      plots( "exp", 0, 4, 1/2 )
      


      因此,您可以从所有这些中创建一个 Lisp APPLY,如下所示:

      apply := method (
          args := call message argsEvaluatedIn(call sender)
          fn   := args removeFirst
          performWithArgList( fn, args )
      )
      
      apply( "plotf", block(n, n exp), 0, 4, 1/2 )
      
      apply( "plotm", message(exp), 0, 4, 1/2 )
      
      apply( "plots", "exp", 0, 4, 1/2 )
      

      【讨论】:

      • 这很有用。你知道如何将动态参数列表应用到未知数量的块吗?
      • @mcandre:抱歉刚刚看到你的评论。不是 100% 确定你在追求什么,因为上面可能提​​供你正在寻找的东西?我创建了一个更简洁的要点,希望能有所帮助:gist.github.com/1334070
      • 史蒂夫·德科特gets it。 :) 这是它的使用example
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-04
      • 2013-12-14
      • 1970-01-01
      • 2011-01-15
      相关资源
      最近更新 更多