Julia:通过map()函数传递关键字参数 [英] Julia: Passing keyword arguments to function through the map() function

查看:640
本文介绍了Julia:通过map()函数传递关键字参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 map()在(nx 1)数组上应用函数 quandl 字符串。 ( http://quandljl.readthedocs.io/en/latest/get_data.html



然而,我希望将更多的字符串作为参数传递给函数,例如<= c $ c> from = Date1 和 to = Date2 。我似乎无法找到一种方法让< map()将函数应用于字符串数组,同时还传递关键字参数以从 Date1下载数据 Date2



更常见的问题是:如何使用 map()在几个元素上应用一个函数,同时还传递额外的参数给这个函数?

解决方案你需要创建一个匿名函数,用适当的参数调用 quandl 并映射到数据上。由于我对你想如何调用 quandl 有点不清楚,我只会用一个制作的例子。假设 f 有两个位置参数和一个关键字 k ;假设你想用 v 作为第二个参数并将 2 应用到 k =abc。您可以这样做:

  map(x  - > f(x,2,k =abc), v)

如果匿名函数体很大或很复杂,您可能想要使用Julia的 do-block语法并编写像这样的计算相反:

$ $ p $ code $ map $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b结束

在这个例子中,这没有多大意义,但是如果匿名函数是多个代码行,那么这可能是可取的。


I am trying to use map() to apply the function quandl on an array of (n x 1) strings. (http://quandljl.readthedocs.io/en/latest/get_data.html)

However, I wish to pass on more than just the strings as arguments to the function, such as from = Date1 and to = Date2. I cannot seem to find a way to have map() apply the function on the array of strings while also passing the keyword arguments to download data from Date1 to Date2.

The more general question is: how can I use map() to apply a function on several elements while also passing additional arguments to this function?

解决方案

You'll want to create an anonymous function that calls quandl with the appropriate arguments and map that over your data. Since I'm a little unclear on how you want to call quandl, I'll just use a made up example. Suppose f takes two positional arguments and a keyword k; suppose you want to apply it to each value of v with 2 as the second argument and k = "abc". You would do this like so:

map(x -> f(x, 2, k = "abc"), v)

If the anonymous function body is large or complicated, you may want to use Julia's do-block syntax and write the computation like this instead:

map(v) do x
    f(x, 2, k = "abc")
end

In this example, this doesn't make much sense, but if the anonymous function is multiple lines of code, then this can be preferable.

这篇关于Julia:通过map()函数传递关键字参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆