Julia: eval的一些用法

eval不管是在matlab,python,js等语言中,还是在Julia中,都有十分广泛的应用。有些是无法替代的,没有eval,可能还真玩不转。

下面主要讲一下Julia的eval:
一、如何把using放在函数中?
using 是不允许直接放在函数中的,不信你试试。但是,比如,我不同的情况需要调用不同的库,这个时侯如何处理,那么请eval出场是什么必要的事情。否则,真不知如何解决。

比如,一个画出策略出PL的函数:
需要注意的是,变量前加一个$。

function plotStrategy_PL(plotTool,isNeedPlot,dictPL,pl,strategyIOName) #2017-6-4
    if isNeedPlot == true
        x_axis = sort!(collect(keys(dictPL))) # 日期 为 X轴
        y_axis = pl;
        strname =string(strategyIOName, "strategy ")
        if plotTool =="PyPlot"
            eval(:(
            begin
                using PyPlot
                close();clf;
                PyPlot.plot($x_axis,$y_axis,color="red",linewidth = 2.0,linestyle ="-") # 变量前加一个$
                PyPlot.title($strname) # 变量前加一个$
            end))
        elseif plotTool =="Winston"
            eval(:(
            begin
                using Winston
                Winston.display(Winston.timeplot($x_axis,$y_axis,"r"))
                Winston.title($strname)
            end))
        elseif plotTool =="Gadfly"
            #draw(PNG("C:\\Users\\Administrator\\Desktop\\myplot.png", 24cm, 12cm), p)
            #show("C:\\Users\\Administrator\\Desktop\\myplot.png")
        end
    end
end

二、单句

    function test()
      num =5
      str =string("num : $num")
      eval(:(println($str)));
    end
    test()

三、语句段

    function test()
      num =5
      #eval(:(using PyPlot))
      str =string("num : $num")
      eval(:(begin
      println($str)
      println(10)
    end));
    end
    test()
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值