“全新” 编程语言 Julia 了解一下

2018-08-14 20:02:16 +08:00
 hansonwang99


本文共 851 字,阅读大约需要 3 分钟 !


概 述

Julia 是一个 “全新”的高性能动态编程语言,前两天迎来了其 1.0 正式版的重大更新。Julia 集 Python、C、R、Ruby 之所长,感觉就像一种脚本语言,并且对交互式使用有很好的支持。而且其天生的高性能、通用性与专业性使得其非常适用于科学数值计算、机器学习项目等前沿场景。我看完这个消息以后也迫不及待想尝试一下。

注: 本文原载于 My Personal Blog:CodeSheep · 程序羊

本文内容脑图如下:


Julia 的特性

正是由于这些特性,才使其应用场景宽泛,而且都是当下前沿热门应用场景:


编程环境支持

Julia 通过提供了一系列插件支持,从而可以在大多数常见的编辑器中进行编程,具体包括


Julia 安装和部署

Julia 提供了各种平台和环境的安装包,具体可以去其官网进行下载:

安装非常简单,像 Windows 平台,基本上只需要点按下一步即可安装到位,而 MacOS 平台使用 brew 包管理器也仅需 一行命令 即可完成安装。

下面我以 Linux CentOS 7.4 平台为例,介绍一些其安装过程:

CentOS7 上 Julia 安装也无需复杂的过程,只需要下载对应的可执行版本,并置于系统的命令路径中即可愉快的使用:

wget https://julialang-s3.julialang.org/bin/linux/x64/1.0/julia-1.0.0-linux-x86_64.tar.gz
tar zxvf julia-1.0.0-linux-x86_64.tar.gz
cd /usr/bin
ln -s /root/julia-1.0.0/bin/julia

此时执行 julia 命令即可启动 Julia 控制台,顺便来向世界问一下好吧:

下面做一些上手实验,大致来感受一下该语言精炼、灵活的风格。即使不使用任何文字说明,也能很容易地理解各个命令的含义,这也说明该语言很好上手。


Julia 上手体验

julia> x=10
10

julia> x+1
11

julia> x^2
100

julia> pi
π = 3.1415926535897...

julia> sqrt(100)
10.0

julia> ~123
-124

julia> 123 & 234
106

julia> ~UInt32(123)
0xffffff84

julia> [1,2,3] .^ 3
3-element Array{Int64,1}:
  1
  8
 27

julia> 1 == 1
true

julia> NaN == NaN
false

julia> NaN != NaN
true

julia>
julia> 'x'
'x': ASCII/Unicode U+0078 (category Ll: Letter, lowercase)

julia> Int('x')
120

julia> str = "Hello, world.\n"
"Hello, world.\n"

julia> str[1]
'H': ASCII/Unicode U+0048 (category Lu: Letter, uppercase)

julia> str[end]
'\n': ASCII/Unicode U+000a (category Cc: Other, control)

julia> str[4:9]
"lo, wo"

julia> greet = "Hello"
"Hello"
julia> whom = "world"
"world"
julia> "$greet, $whom.\n"
"Hello, world.\n"

julia> findfirst(isequal('x'), "xylophone")
1
julia> findnext(isequal('o'), "xylophone", 1)
4
julia> findnext(isequal('o'), "xylophone", 5)
7
julia> findnext(isequal('o'), "xylophone", 8)

julia> occursin("world", "Hello, world.")
true
julia> repeat(".:Z:.", 10)
".:Z:..:Z:..:Z:..:Z:..:Z:..:Z:..:Z:..:Z:..:Z:..:Z:."

julia> occursin(r"^\s*(?:#|$)", "not a comment")
false
julia> occursin(r"^\s*(?:#|$)", "# a comment")
true
julia> match(r"^\s*(?:#|$)", "# a comment")
RegexMatch("#")
julia> x = 12
12

julia> typeof(x)
Int64

julia> convert(UInt8, x)
0x0c

julia> convert(AbstractFloat, x)
12.0

julia> a = Any[1 2 3; 4 5 6]
2×3 Array{Any,2}:
 1  2  3
 4  5  6

julia> convert(Array{Float64}, a)
2×3 Array{Float64,2}:
 1.0  2.0  3.0
 4.0  5.0  6.0

julia> promote(1, 2.5)
(1.0, 2.5)

julia> promote(2, 3//4)
(2//1, 3//4)

julia> promote(1.5, im)
(1.5 + 0.0im, 0.0 + 1.0im)

julia> promote(1 + 2im, 3//4)
(1//1 + 2//1*im, 3//4 + 0//1*im)
julia> f(x,y) = x + y
f (generic function with 1 method)

julia> f(2,3)
5

julia> g = f
f (generic function with 1 method)

julia> g(2,3)
5

julia> ∑(x,y) = x + y
∑ (generic function with 1 method)

julia> ∑(2, 3)
5

julia> +(1,2,3)
6

julia> x -> x^2 + 2x - 1
#3 (generic function with 1 method)

julia> map(x -> x^2 + 2x - 1, [1,3,-1])
3-element Array{Int64,1}:
  2
 14
 -2

julia> function foo(a,b)
                a+b, a*b
          end;

julia> foo(2,3)
(5, 6)

julia> x, y = foo(2,3);

julia> x
5

julia> y
6

julia> z = begin
         x = 1
         y = 2
         x + y
       end
3

julia> function test(x, y)
         if x < y
           println("x is less than y")
         elseif x > y
           println("x is greater than y")
         else
           println("x is equal to y")
         end
       end
test (generic function with 1 method)

julia> test(1, 2)
x is less than y

julia> test(2, 1)
x is greater than y

julia> test(1, 1)
x is equal to y

julia> println(x < y ? "less than" : "not less than")
less than

julia> while i <= 5
         println(i)
         i += 1
       end
1
2
3
4
5

julia> for i = 1:5
         println(i)
       end
1
2
3
4
5

外部构造方式:

julia> struct Foo
           bar
           baz
       end

julia> 

julia> fun=Foo(1,2)
Foo(1, 2)

julia> fun.bar
1

julia> fun.baz
2

julia> Foo(x) = Foo(x,x)
Foo

julia> Foo(1)
Foo(1, 1)

julia> Foo() = Foo(0)
Foo

julia> Foo()
Foo(0, 0)

内部构造方式:

julia> struct OrderedPair
           x::Real
           y::Real
           OrderedPair(x,y) = x > y ? error("out of order") : new(x,y)
       end

julia> 

julia> OrderedPair(1, 2)
OrderedPair(1, 2)

julia> OrderedPair(2,1)
ERROR: out of order
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] OrderedPair(::Int64, ::Int64) at ./REPL[45]:4
 [3] top-level scope at none:0

迭代操作:

julia>  struct Squares
               count::Int
       end

julia> Base.iterate(S::Squares, state=1) = state > S.count ? nothing : (state*state, state+1)

julia> for i in Squares(7)
                  println(i)
       end
1
4
9
16
25
36
49

julia> 25 in Squares(10)
true

julia> using Statistics

julia> mean(Squares(100))
3383.5

julia> std(Squares(100))
3024.355854282583

julia> Base.eltype(::Type{Squares}) = Int

julia> Base.length(S::Squares) = S.count

julia> collect(Squares(4))
4-element Array{Int64,1}:
  1
  4
  9
 16

索引操作:

julia> function Base.getindex(S::Squares, i::Int)
               1 <= i <= S.count || throw(BoundsError(S, i))
               return i*i
       end

julia> Squares(100)[23]
529

julia> Base.firstindex(S::Squares) = 1

julia> Base.lastindex(S::Squares) = length(S)

julia> Squares(23)[end]
529

julia> Base.getindex(S::Squares, i::Number) = S[convert(Int, i)]

julia> Base.getindex(S::Squares, I) = [S[i] for i in I]

julia> Squares(10)[[3,4.,5]]
3-element Array{Int64,1}:
  9
 16
 25

基本的语言特性就体验到这,剩余的还有一些高级特性,包括:

不在此文一一赘述,详细了解就去参考官方文档吧。


后 记

由于能力有限,若有错误或者不当之处,还请大家批评指正,一起学习交流!



7759 次点击
所在节点    推广
44 条回复
des
2018-08-14 20:19:54 +08:00
打扰了
zetary
2018-08-14 20:22:18 +08:00
做科学计算的话用这个比较靠谱,比 matlab 好,不适合 v 站上绝大部分人吧
hansonwang99
2018-08-14 20:23:08 +08:00
也适合机器学习
luozic
2018-08-14 20:24:57 +08:00
科学计算可以,别的为啥不用 C++或者 python ?
hansonwang99
2018-08-14 20:25:27 +08:00
效率高于 Py
lihongjie0209
2018-08-14 20:28:55 +08:00
请问 Python 吹怎么看
d5
2018-08-14 20:30:48 +08:00
python 吹表示健身先健脑,多半不是因为性能瓶颈
dacapoday
2018-08-14 20:41:01 +08:00
这个语言的后台是哪个公司?现在谁在推广?准备在哪个领域“搞革命”?
hansonwang99
2018-08-14 20:59:34 +08:00
其实这语言一直在国外用,由于中文生态圈不好估计,所以国内没怎么听说
SimbaPeng
2018-08-14 21:01:59 +08:00
库上了 Python 的量级再说吧
SimbaPeng
2018-08-14 21:04:36 +08:00
我只知道这个语言的 seo 怕是上不去了
gettext
2018-08-14 21:10:50 +08:00
My Personal Blog:,CodeSheep · 程序羊 !


来推广博客不是语言的,最近推广很勤,大家别当真
agagega
2018-08-14 21:22:08 +08:00
定义 struct 不带类型标注就是泛型么?听说 Julia 的类型系统很强大
ghhardy
2018-08-14 21:47:07 +08:00
序列的索引从 1 开始...可以开一场圣战了
Or2
2018-08-14 21:47:28 +08:00
@dacapoday MIT 的 Julia 实验室
glasslion
2018-08-14 22:11:54 +08:00
全新? 这货比 rust 都老
glasslion
2018-08-14 22:12:57 +08:00
@hansonwang99 适合科学(数值)计算没错, 哪里看出适合科学计算了
glasslion
2018-08-14 22:13:59 +08:00
@hansonwang99 前一条 typo, 适合科学(数值)计算没错, 哪里看出适合深度学习了
iRiven
2018-08-14 22:36:30 +08:00
感觉没啥特殊的地方
dsp2138
2018-08-14 22:40:16 +08:00
能编写 windows 程序吗

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/479801

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX