
本文共 851字,阅读大约需要 3分钟 !
概 述
Julia 是一个 “全新”的高性能动态编程语言,前两天迎来了其 1.0 正式版的重大更新。Julia集 Python、C、R、Ruby 之所长,感觉就像一种脚本语言,并且对交互式使用有很好的支持。而且其天生的高性能、通用性与专业性使得其非常适用于科学数值计算、机器学习项目等前沿场景。我看完这个消息以后也迫不及待想尝试一下。
注: 本文原载于 My Personal Blog:, CodeSheep · 程序羊 !
本文内容脑图如下:

Julia的特性
- 高性能:Julia 通过 LLVM 为多个平台编译高效本地代码,性能很高
- 动态性:编程范式灵活,代码信噪比极高
- 通用性:易于表达OOP和函数式编程范式,并且其标准库提供异步I / O,进程控制,日志记录,概要分析,包管理器等。
- 专业性:擅长数值计算,支持许多数值数据类型,并且提供开箱即用的并行性。
- 可组合性:Julia 的包之间可以很好地协同工作。
正是由于这些特性,才使其应用场景宽泛,而且都是当下前沿热门应用场景:

编程环境支持
Julia通过提供了一系列插件支持,从而可以在大多数常见的编辑器中进行编程,具体包括
- Atom
- VS Code
- Jupyter
- Vim
- Emacs
- SublimeText
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.gztar zxvf julia-1.0.0-linux-x86_64.tar.gzcd /usr/binln -s /root/julia-1.0.0/bin/julia此时执行 julia 命令即可启动 Julia控制台,顺便来向世界问一下好吧:

下面做一些上手实验,大致来感受一下该语言精炼、灵活的风格。即使不使用任何文字说明,也能很容易地理解各个命令的含义,这也说明该语言很好上手。
Julia上手体验
- 变量操作
julia> x=1010julia> x+111julia> x^2100julia> piπ = 3.1415926535897...julia> sqrt(100)10.0julia> ~123-124julia> 123 & 234106julia> ~UInt32(123)0xffffff84julia> [1,2,3] .^ 33-element Array{Int64,1}: 1 8 27julia> 1 == 1truejulia> NaN == NaNfalsejulia> NaN != NaNtruejulia>julia> 'x''x': ASCII/Unicode U+0078 (category Ll: Letter, lowercase)julia> Int('x')120julia> str = "Hello, world.
""Hello, world.
"julia> str[1]'H': ASCII/Unicode U+0048 (category Lu: Letter, uppercase)julia> str[end]'
': 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.
""Hello, world.
"julia> findfirst(isequal('x'), "xylophone")1julia> findnext(isequal('o'), "xylophone", 1)4julia> findnext(isequal('o'), "xylophone", 5)7julia> findnext(isequal('o'), "xylophone", 8)julia> occursin("world", "Hello, world.")truejulia> repeat(".:Z:.", 10)".:Z:..:Z:..:Z:..:Z:..:Z:..:Z:..:Z:..:Z:..:Z:..:Z:."julia> occursin(r"^s*(?:#|$)", "not a comment")falsejulia> occursin(r"^s*(?:#|$)", "# a comment")truejulia> match(r"^s*(?:#|$)", "# a comment")RegexMatch("#")- 类型转换和提升
julia> x = 1212julia> typeof(x)Int64julia> convert(UInt8, x)0x0cjulia> convert(AbstractFloat, x)12.0julia> a = Any[1 2 3; 4 5 6]2×3 Array{Any,2}: 1 2 3 4 5 6julia> convert(Array{Float64}, a)2×3 Array{Float64,2}: 1.0 2.0 3.0 4.0 5.0 6.0julia> 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 + yf (generic function with 1 method)julia> f(2,3)5julia> g = ff (generic function with 1 method)julia> g(2,3)5julia> ∑(x,y) = x + y∑ (generic function with 1 method)julia> ∑(2, 3)5julia> +(1,2,3)6julia> 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 -2julia> function foo(a,b) a+b, a*b end;julia> foo(2,3)(5, 6)julia> x, y = foo(2,3);julia> x5julia> y6- 控制流
julia> z = begin x = 1 y = 2 x + y end3julia> 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 endtest (generic function with 1 method)julia> test(1, 2)x is less than yjulia> test(2, 1)x is greater than yjulia> test(1, 1)x is equal to yjulia> println(x < y ? "less than" : "not less than")less thanjulia> while i <= 5 println(i) i += 1 end12345julia> for i = 1:5 println(i) end12345- 对象构造
外部构造方式:
julia> struct Foo bar baz endjulia> julia> fun=Foo(1,2)Foo(1, 2)julia> fun.bar1julia> fun.baz2julia> Foo(x) = Foo(x,x)Foojulia> Foo(1)Foo(1, 1)julia> Foo() = Foo(0)Foojulia> Foo()Foo(0, 0)内部构造方式:
julia> struct OrderedPair x::Real y::Real OrderedPair(x,y) = x > y ? error("out of order") : new(x,y) endjulia> julia> OrderedPair(1, 2)OrderedPair(1, 2)julia> OrderedPair(2,1)ERROR: out of orderStacktrace: [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 endjulia> .iterate(S::Squares, state=1) = state > S.count ? nothing : (state*state, state+1)julia> for i in Squares(7) println(i) end14916253649julia> 25 in Squares(10)truejulia> using Statisticsjulia> mean(Squares(100))3383.5julia> std(Squares(100))3024.355854282583julia> .eltype(::Type{Squares}) = Intjulia> .length(S::Squares) = S.countjulia> collect(Squares(4))4-element Array{Int64,1}: 1 4 9 16索引操作:
julia> function .getindex(S::Squares, i::Int) 1 <= i <= S.count || throw(BoundsError(S, i)) return i*i endjulia> Squares(100)[23]529julia> .firstindex(S::Squares) = 1julia> .lastindex(S::Squares) = length(S)julia> Squares(23)[end]529julia> .getindex(S::Squares, i::Number) = S[convert(Int, i)]julia> .getindex(S::Squares, I) = [S[i] for i in I]julia> Squares(10)[[3,4.,5]]3-element Array{Int64,1}: 9 16 25基本的语言特性就体验到这,剩余的还有一些高级特性,包括:
- 模块
- 元编程
- 并行计算
- 网络和流
- 交互
- ......
不在此文一一赘述,详细了解就去参考官方文档吧。
后 记
由于能力有限,若有错误或者不当之处,还请大家批评指正,一起学习交流!
继续阅读与本文标签相同的文章
上一篇 :
记一次mysql报错
-
小程序更新机制
2026-05-25栏目: 教程
-
3D打印高跟鞋Mycelium Shoe 前卫与舒适兼俱
2026-05-25栏目: 教程
-
C++输入总结
2026-05-25栏目: 教程
-
Docker安装
2026-05-25栏目: 教程
-
Docker中使用systemctl启动服务报错的解决办法
2026-05-25栏目: 教程
