用红色语言创建地图功能

时间:2017-10-15 08:17:42

标签: higher-order-functions rebol red

如何使用红色语言创建更高阶函数map。它应该将一个块和一个函数作为参数,并将发送的函数应用于块的每个成员。我尝试了以下代码:

Red []
mapfn: function[blk sfn][
    outblk: copy []
    foreach i blk[
        append outblk (sfn i) ]
    outblk ]

; to test: 
myblk: [" this " " is   " " a "  "    line " "for" "   testing " " only   "]
probe mapfn myblk 'reverse 
probe mapfn myblk 'trim

但它不起作用 - 它只是发送回原始块而不更改它或给出任何错误消息。如何纠正?

1 个答案:

答案 0 :(得分:0)

在Rebol中,您有夹层功能应用

    function _test(actions){
        return actions.reduce((chain, action) => {
          return chain.then(() => action.functionToCall(action.argumentToSend)).then(val => console.log(val));
        }, Promise.resolve());
    }
    
    function _one(data){
        return new Promise((resolve, reject) => {
          setTimeout(function(){  
            console.log(data); 
            resolve('resolvedFromOne');
          }, 2000);
        })
    }
    
    function _two(data){
        return new Promise((resolve, reject) => {
            setTimeout(function(){  
              console.log(data); 
              resolve('resolvedFromTwo');
            }, 2000);
        })
    }
    
    function _three(data){
        return new Promise((resolve, reject) => {
            setTimeout(function(){  
              console.log(data); 
              resolve('resolvedFromThree');
            }, 2000);
        })
    }
    
    // not required
    function _done(data){
      console.log(data);
    }
    
    const arrayOfObjects = [
        { functionToCall: _one, argumentToSend: 'Yay function one was called with this argument' },
        { functionToCall: _two, argumentToSend: 'Yay function two was called with this argument' },
        { functionToCall: _three, argumentToSend: 'Yay function three was called with this argument' },
        ];
    
    _test(arrayOfObjects);

请参阅 source apply

只要Red没有本机应用,您就可以编写自己的映射函数,例如

>> help apply
USAGE:
    APPLY func block /only 

DESCRIPTION:
     Apply a function to a reduced block of arguments.
     APPLY is a function value.

ARGUMENTS:
     func -- Function value to apply (Type: any-function)
     block -- Block of args, reduced first (unless /only) (Type: block)

REFINEMENTS:
     /only -- Use arg values as-is, do not reduce the block

(SPECIAL ATTRIBUTES)
     throw

获取函数:functionname

mapfn: function[blk sfn][
    outblk: copy []
    foreach i blk[
        append outblk sfn copy i 
    ]
    outblk 
]

另一种更好的方法,因为您无法复制所有数据类型,例如

>> myblk: [" this " " is   " " a "  "    line " "for" "   testing " " only   "]
== [" this " " is   " " a " "    line " "for" "   testing " " only   "]
>> probe mapfn myblk :reverse 
[" siht " "   si " " a " " enil    " "rof" " gnitset   " "   ylno "]
== [" siht " "   si " " a " " enil    " "rof" " gnitset   " "   ylno "]
>> probe mapfn myblk :trim
["this" "is" "a" "line" "for" "testing" "only"]
== ["this" "is" "a" "line" "for" "testing" "only"]
>> 

如果不想修改原始

,则以这种方式调用函数
mapfn: function[blk sfn][
    collect [
        foreach i blk[
            keep sfn i 
        ]
    ]
]