博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ten minutes to let you know the Julia language
阅读量:5781 次
发布时间:2019-06-18

本文共 3349 字,大约阅读时间需要 11 分钟。

hot3.png

Introduction

Julia is a flexible dynamic language for science and numerical computing with performance comparable to traditional statically typed languages. Perhaps you can use it for the computation of machine learning that has recently received much attention.This blog takes 10 minutes to let you know the language. Then you will be attracted by the language.

Modules,Variables and Functions

Modules in Julia are separate variable workspaces, i.e. they introduce a new global scope. To load a module you defined, two main keywords can be used: using and import. 

source:

module FirstModuleexport x, y, zx() = "x"y() = "y"z() = "z"end

source:

module SecondModuleusing Main.FirstModule: ximport Main.FirstModule: y, zprintln(x())println(y())println(z())end

output:

xyz

A variable, in Julia, is a name associated (or bound) to a value. It's useful when you want to store a value (that you obtained after some math, for example) for later use.

source:

x = 10y = 23.0z = x + ym = sqrt(100)str = "hello world."println(z)println(m)println(str)

output:

33.010.0hello world.

In Julia, a function is an object that maps a tuple of argument values to a return value.

source:

function area(x, y)    return x * yendperimeter = (x, y) -> (x + y) * 2println(area(5, 10))println(perimeter(5, 10))

output:

5030

Control Flow

source:

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")    endendtest(5, 10)test(10, 5)test(10, 10)i = 1while i <= 5   println(i)   global i += 1endprintln("\n")for i = 1:5    println(i)endtry    sqrt(-1)catch err    println(err)end

output:

x is less than yx is greater than yx is equal to y1234512345DomainError(-1.0, "sqrt will only return a complex result if called with a complex argument. Try sqrt(Complex(x)).")

More types: Tuples,Sets,Arrays,Dicts

source:

# Tuplerect = (10, 20)println(rect)println(rect[1])println("\n")# Setx = Set([1,2,3,4])println(x)push!(x, 5)println(x)push!(x, 2)println("Cannot insert an existing value 2")println(x)println("\n")# Arrayy = Array([1,2,3,4])println(y)push!(y, 5)println(y)push!(y, 6, 7)println(y)println("\n")# Dictrect = Dict([("length",10), ("width",20)])println(rect["length"])println(rect["width"])

output:

(10, 20)10Set([4, 2, 3, 1])Set([4, 2, 3, 5, 1])Cannot insert an existing value 2Set([4, 2, 3, 5, 1])[1, 2, 3, 4][1, 2, 3, 4, 5][1, 2, 3, 4, 5, 6, 7]1020

Structs and Methods

Struct keyword is used to build complex data types.

source:

# Structstruct Box    length::Float64    width::Float64    height::Float64endbox = Box(10.0, 20.0, 30.0)println(box.length)

output:

10.0

Method used to implement the concept of Polymorphism.

source:

# Methodfunction f(x::Float64, y::Float64)    return 2x + 3yendprintln(f(1.0, 2.0))println(f(1, 2))

output:

8.0MethodError: no method matching f(::Int64, ::Int64)

Why? Let's  add a function with the same name f.

source:

# Methodfunction f(x::Float64, y::Float64)    return 2x + 3yendfunction f(x::Int64, y::Int64)    return 2x + 3yendprintln(f(1.0, 2.0))println(f(1, 2))

output:

8.08

After reading the blog, I believe that you have a overall understanding of julia. If you are interested in this language, please visit its official website for more interesting things.

official website:

转载于:https://my.oschina.net/bluecrystal/blog/1933791

你可能感兴趣的文章
Hoshin Kanri在丰田的应用
查看>>
又拍云沈志华:如何打造一款安全的App
查看>>
Nginx 学习书单整理
查看>>
克服大数据集群的挑战
查看>>
PostgreSQL并发控制(MVCC, 事务,事务隔离级别)
查看>>
12月19日一周一次【Python基础语法】
查看>>
DM***的第二阶段OSPF
查看>>
python socket编程
查看>>
20180702搭建青岛RAC记录
查看>>
安装部署TIDB分布式数据库
查看>>
Spring Security OAuth 实现OAuth 2.0 授权
查看>>
linux文件及简单命令学习
查看>>
dubbo源码分析-架构
查看>>
新 Terraform 提供商: Oracle OCI, Brightbox, RightScale
查看>>
6套毕业设计PPT模板拯救你的毕业答辩
查看>>
IT兄弟连 JavaWeb教程 JSP与Servlet的联系
查看>>
Windows phone 8 学习笔记
查看>>
linux并发连接数:Linux下高并发socket最大连接数所受的各种限制
查看>>
洛谷——P2176 [USACO14FEB]路障Roadblock
查看>>
详解区块链中EOS的作用。
查看>>