首页 \ 问答 \ Io语言'应用参数'(Io language 'apply arguments')

Io语言'应用参数'(Io language 'apply arguments')

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

所以例如我有一个方法来包装writeln:

mymeth := method(
              //do some extra stuff

             writeln(call message arguments))
)

目前这只是打印清单,并没有评估它的内容,就好像它是自己的参数一样。


In the Io programming language, is there an equivalent to lisp's apply function.

So for example I have a method to wrap writeln :

mymeth := method(
              //do some extra stuff

             writeln(call message arguments))
)

At the moment this just prints the list, and doesn't evaluate it's contents as if they were it's own args.


原文:https://stackoverflow.com/questions/4419779
更新时间:2023-02-23 12:02

最满意答案

Nimrand回答你的问题(缺少catch ),但这里是你的代码,没有承诺构造函数antipattern

function getDataFromServer() {
  var result = [];

  function fetchData(nextPageToken) {
    return server.getData(nextPageToken).then(function(response) {
      result.push(response.data);
      if (response.nextPageToken) {
        return fetchData(response.nextPageToken);
      } else {
        return result;
      }
    });
  }
  return fetchData(null);
}

getDataFromServer().then(function(result) {
  console.log(result);
})
.catch(function(e) {
  console.error(e);
});

正如你所看到的,递归在承诺方面很有效。

var console = { log: function(msg) { div.innerHTML += "<p>"+ msg +"</p>"; }};

var responses = [
  { data: 1001, nextPageToken: 1 },
  { data: 1002, nextPageToken: 2 },
  { data: 1003, nextPageToken: 3 },
  { data: 1004, nextPageToken: 4 },
  { data: 1005, nextPageToken: 0 },
];

var server = {
  getData: function(token) {
    return new Promise(function(resolve) { resolve(responses[token]); });
  }
};

function getDataFromServer() {
  var result = [];

  function fetchData(nextPageToken) {
    return server.getData(nextPageToken).then(function(response) {
      result.push(response.data);
      if (response.nextPageToken) {
        return fetchData(response.nextPageToken);
      } else {
        return result;
      }
    });
  }
  return fetchData(0);
}

getDataFromServer().then(function(result) {
  console.log(result);
})
.catch(function(e) { console.log(e); });
<div id="div"></div>


Nimrand answers your question (missing catch), but here is your code without the promise constructor antipattern:

function getDataFromServer() {
  var result = [];

  function fetchData(nextPageToken) {
    return server.getData(nextPageToken).then(function(response) {
      result.push(response.data);
      if (response.nextPageToken) {
        return fetchData(response.nextPageToken);
      } else {
        return result;
      }
    });
  }
  return fetchData(null);
}

getDataFromServer().then(function(result) {
  console.log(result);
})
.catch(function(e) {
  console.error(e);
});

As you can see, recursion works great with promises.

var console = { log: function(msg) { div.innerHTML += "<p>"+ msg +"</p>"; }};

var responses = [
  { data: 1001, nextPageToken: 1 },
  { data: 1002, nextPageToken: 2 },
  { data: 1003, nextPageToken: 3 },
  { data: 1004, nextPageToken: 4 },
  { data: 1005, nextPageToken: 0 },
];

var server = {
  getData: function(token) {
    return new Promise(function(resolve) { resolve(responses[token]); });
  }
};

function getDataFromServer() {
  var result = [];

  function fetchData(nextPageToken) {
    return server.getData(nextPageToken).then(function(response) {
      result.push(response.data);
      if (response.nextPageToken) {
        return fetchData(response.nextPageToken);
      } else {
        return result;
      }
    });
  }
  return fetchData(0);
}

getDataFromServer().then(function(result) {
  console.log(result);
})
.catch(function(e) { console.log(e); });
<div id="div"></div>

相关问答

更多
  • 您可以嵌套承诺: getAccessToken(req) .then(function(accessToken) { return getUserProfile(req, accessToken) .then(findUser.bind(null, req, res)) .then(function(user) { // use accessToken & user }) }); 或者将解析后的值存储在外部作用域中: var _accessToken; getAccessToken ...
  • 我会使用一个对象来包装该值。 这样你可以拥有一个完整的属性来让循环知道你已经完成了。 // fn should return an object like // { // done: false, // value: foo // } function loop(promise, fn) { return promise.then(fn).then(function (wrapper) { return !wrapper.done ? loop(Q(wrapper.value), fn) ...
  • 这并不是真正的承诺。 如果你在一个循环中创建回调, 你需要一个额外的闭包范围 ,除了它非常标准。 但是,对于您的特定情况,最简单的方法是仅使用单个回调,因为它们都会附加到相同的承诺并且将获得相同的值。 所以你用 require('q').when('test').then(function(val) { for (var i=1; i<=4; i++) { console.log(val + '-' + i); } }); At last I could do I want ...
  • Nimrand回答你的问题(缺少catch ),但这里是你的代码,没有承诺构造函数antipattern : function getDataFromServer() { var result = []; function fetchData(nextPageToken) { return server.getData(nextPageToken).then(function(response) { result.push(response.data); if (r ...
  • 最简单,最干净的方法是使用async/await 。 该代码不会并行运行(除非我们等待Promise.all ) .then(async() => { // Loop through each object in the orderArray for(let i = 0; i < orderArray.length; i++) { // Some may argue no await inside loop... // We wait for rp to ...
  • 您可以使用$ .ajax返回promise,使用$ .map而不是$ .each,并使用$ .when等待所有ajax完成,并在$中成功回调中执行的操作。 when()。然后回调...... var loadTemplates = function (templatesObject) { $.when.apply($, $.map(templatesObject.template, function (index, template) { template.callback = te ...
  • 而不是这样做, a().then(function(result) { b(result).then(function(result) { c(result).then(function(result) { console.log("done"); }); }); }); 你可以链接顶级的所有承诺。 a() .then(function(result) { return b(result); }) ...
  • 您可以将标识符返回给承诺 const promises = []; const promiser = (number, id) => new Promise((resolve, reject) => window.setTimeout(() => resolve({ number, id }), number * 100)); for (let index = 0; index < 10; index++) { const promise = promiser(index, "id_" + index) ...
  • 您正在异步构造typePromises列表 - 当您调用Q.all(typePromises) ,它仍然是空的。 相反,您需要立即返回数据库结果的承诺,您可以立即将其收集到列表中。 如果您还不知道这些承诺的返回值是什么 - 不用担心, then在redis结果到达后使用如下任务来Q.all(chorePromises)等任务。 我还建议使用map而不是在each循环中推送到一个数组 - 这也有助于确保承诺立即构建。 myClass.prototype.doMaintenance = function() { ...
  • 您需要从方法中返回承诺才能生效。 p.methodOne = function() { var prm_dataCount = $.ajax({'get-data'}); prm_dataCount.done(function(data){p.dataCount = data;}); return prm_dataCount; } p.methodTwo = function() { var prm_dataCountTwo = $.ajax({'get-data-two'}); pr ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。