Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# An Introduction to the Julia Programming Language"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Varialbles and Types"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"56*8"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"x=7"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"x+1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"x^2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"typeof(x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
" x/3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"cos(pi)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"typeof(ans)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"im^2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"typeof(ans)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"\"Hello\" * \" world\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"typeof(ans)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"70//15"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"true"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"12 ≠ 42"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"2 ∈ [6,3,5]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"typeof(ans)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"\"Like in perl you may write x=$x\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"\"... but also $(exp(x))\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Type Hierarchy "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The type hierarchy for numbers looks like:\n",
"![type hierarchy](type-hierarchy-for-julia-numbers.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Functions + Multiple Dispatch\n",
"One can define functions simply like that:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"f(x) = x ^ 3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"f(3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"f(\"ha \")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"function f(x::AbstractString) \n",
" println(\"\\nnow in f(x::AbstractString) with x=$x of type $(typeof(x))\")\n",
" \"Hello $x\"\n",
"end\n",
"\n",
"function f(x::AbstractFloat) \n",
" println(\"\\nnow in f(x::AbstractFloat) with x=$x of type $(typeof(x))\")\n",
" x+1.0\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"methods(f)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"@show f(2.5);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"@show f( Float16(2.5) );"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"@show f(\"abc\")\n",
"@show f(\"αβγ\");"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"methods(print)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"methods(println)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Composite Types"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"type Point\n",
" x::Int\n",
" y::Int\n",
" name::ASCIIString\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"p = Point(2,3,\"Siegfried\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"function f(p::Point)\n",
" println(\"\\nnow in f(x::Point) with p=$p of type $(typeof(p))\")\n",
" p.x = p.x + p.y\n",
" return p.name\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"methods(f)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"f(p)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"p"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### parametric types and methods"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"type Line{T}\n",
" x0::T\n",
" y0::T\n",
" x1::T\n",
" y1::T\n",
" name::ASCIIString\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"L1=Line(23,3,4,44,\"Julia\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"L2=Line(23.777,3.5,4.1,4.0,\"Rolf\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"function f{T}(x::Line{T}) \n",
" println(\"\\nnow in f(x::Line{T}) with x=$x of type $(typeof(x))\")\n",
" x.name\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"f(L1),f(L2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Arrays"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"A=[1,2,9]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"A == 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"A .== 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"A+2.1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"B=[1 2 3; 6 7 4; 6 2 1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"C=[ i*j for i in 1:3, j in 1:4 ]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"B * A"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#B =rand(500,500)\n",
"@time eigvals(B)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"@time svd(B)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Dictionary"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"d = Dict( \"A\"=>6, \"B\"=> 20)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"d[\"A\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"for (key,value) in d\n",
" print(\"$key ----> $value\\n\")\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Regular Expressions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"my_regex = r\"[ABC]\"\n",
"\n",
"@show ismatch(my_regex,\"Foo\")\n",
"@show my_regex(\"Bar\")\n",
"@show my_regex(\"boo\");"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"my_regex =r\"(\\d+)g\"\n",
"match(my_regex,\"take 500g sugar\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Flow Control"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"for i in 1:5 \n",
" println(i)\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"for i in 1:10\n",
" if i % 3 == 0\n",
" println(i)\n",
" end\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# Packages"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lots of packages are available - see http://pkg.julialang.org for a list.\n",
"\n",
"To view all locally installed packages:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"Pkg.status()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To add a package (and its dependencies): "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"Pkg.add(\"DataFrames\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To update installed packages to the last release:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"Pkg.update()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To stay on the top of the master branch:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"Pkg.checkout(\"DataFrames\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#Pkg.generate(\"someName\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# DataFrames"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"using DataFrames\n",
"d = DataFrame(x = 21:30, y = rand(10))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"mean(d[:x]), mean(d[:y])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"using StatsBase\n",
"cov(d[:x], d[:y])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"using RDatasets\n",
"iris = dataset(\"datasets\", \"iris\")\n",
"@show size(iris)\n",
"head(iris)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"by(iris, :Species, size)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"using DimensionalityReduction\n",
"iris_S = convert(Array,DataArray(iris[:,1:4]))\n",
"Xpca = pca(iris_S)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Graphics - PlotlyJS"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"using PlotlyJS\n",
"using Colors\n",
"using Blink\n",
"#using Rsvg\n",
"\n",
"#init_notebook()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"nms = unique(iris[:Species])\n",
"colors = [RGB(0.89, 0.1, 0.1), RGB(0.21, 0.50, 0.72), RGB(0.28, 0.68, 0.3)]\n",
"\n",
"data = GenericTrace[]\n",
"\n",
"for (i, nm) in enumerate(nms)\n",
" sc = Xpca.scores[iris[:Species] .== nm, :]\n",
" x=sc[:,1]\n",
" y=sc[:,2]\n",
" \n",
" trace = scatter(;name=nm, mode=\"markers\",\n",
" marker_size=10, marker_color=colors[i], marker_line_width=0,\n",
" x=x, y=y)\n",
" push!(data, trace)\n",
"end\n",
"\n",
"layout = Layout(width=800, height=550, autosize=false, title=\"Iris dataset\",\n",
" xaxis=attr(title=\"PC1\"),\n",
" yaxis=attr(title=\"PC2\")\n",
" )\n",
"\n",
"\n",
"p1=PlotlyJS.plot(data, layout)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"typeof(p1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#PlotlyJS.savefig(p1, \"output_filename.pdf\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"function clustering_alpha_shapes()\n",
" @eval using DataFrames, RDatasets, Colors\n",
"\n",
" # load data\n",
" iris = dataset(\"datasets\", \"iris\")\n",
" nms = unique(iris[:Species])\n",
" colors = [RGB(0.89, 0.1, 0.1), RGB(0.21, 0.50, 0.72), RGB(0.28, 0.68, 0.3)]\n",
"\n",
" data = GenericTrace[]\n",
"\n",
" for (i, nm) in enumerate(nms)\n",
" df = iris[iris[:Species] .== nm, :]\n",
" x=df[:SepalLength]\n",
" y=df[:SepalWidth]\n",
" z=df[:PetalLength]\n",
" trace = scatter3d(;name=nm, mode=\"markers\",\n",
" marker_size=3, marker_color=colors[i], marker_line_width=0,\n",
" x=x, y=y, z=z)\n",
" push!(data, trace)\n",
"\n",
" cluster = mesh3d(;color=colors[i], opacity=0.3, x=x, y=y, z=z)\n",
" push!(data, cluster)\n",
" end\n",
"\n",
" # notice the nested attrs to create complex JSON objects\n",
" layout = Layout(width=800, height=550, autosize=false, title=\"Iris dataset\",\n",
" scene=attr(xaxis=attr(gridcolor=\"rgb(255, 255, 255)\",\n",
" zerolinecolor=\"rgb(255, 255, 255)\",\n",
" showbackground=true,\n",
" backgroundcolor=\"rgb(230, 230,230)\"),\n",
" yaxis=attr(gridcolor=\"rgb(255, 255, 255)\",\n",
" zerolinecolor=\"rgb(255, 255, 255)\",\n",
" showbackground=true,\n",
" backgroundcolor=\"rgb(230, 230,230)\"),\n",
" zaxis=attr(gridcolor=\"rgb(255, 255, 255)\",\n",
" zerolinecolor=\"rgb(255, 255, 255)\",\n",
" showbackground=true,\n",
" backgroundcolor=\"rgb(230, 230,230)\"),\n",
" aspectratio=attr(x=1, y=1, z=0.7),\n",
" aspectmode = \"manual\"))\n",
" PlotlyJS.plot(data, layout)\n",
"end\n",
"clustering_alpha_shapes()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Calling R\n",
"https://github.com/JuliaStats/RCall.jl"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"using RCall"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"R\"rnorm(10)\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"L = 8\n",
"@rput L\n",
"R\"v <- rnorm(L)\"\n",
"@rget v\n",
"v"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"rcopy(R\"t.test(rnorm(1000))\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Calling C/C++\n",
"http://docs.julialang.org/en/latest/manual/calling-c-and-fortran-code/\n",
"\n",
"https://github.com/Keno/Cxx.jl"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
" ccall( (:clock, \"libc\"), Int32, ())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Calling external programs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"run(`echo Hello`)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"file = \"HelloWorld.jl\"\n",
"\n",
"run(`cat $file`)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# BioJulia\n",
"have a look here https://github.com/BioJulia"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"using Bio.Seq\n",
"\n",
"seq = dna\"ACGTTT\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"reverse_complement(seq)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"seq = dna\"ACAGCGTAGCT\";\n",
"@show approxsearch(seq, dna\"AGGG\", 0)\n",
"@show approxsearch(seq, dna\"AGGG\", 1)\n",
"@show approxsearch(seq, dna\"AGGG\", 2);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Vectorization"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"L = 100000000\n",
"A = rand(1:1000,L)\n",
"B = rand(1:1000,L)\n",
"\n",
"function sum1!(A,B)\n",
" for i in 1:length(A)\n",
" A[i] = A[i]+B[i]\n",
" end\n",
" A\n",
"end\n",
"\n",
"function sum2!(A,B)\n",
" @simd for i in 1:length(A)\n",
" @inbounds A[i] = A[i]+B[i]\n",
" end\n",
" A\n",
"end\n",
"\n",
"\n",
"\n",
"@time A+=B;\n",
"\n",
"@time sum1!(A,B)\n",
"\n",
"@time sum2!(A,B)\n",
"\n",
";\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Parallel Computing - Multiple Processors"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"procs()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"addprocs(2)\n",
"procs()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"r = remotecall(2, rand, 2, 2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"fetch(r)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#addprocs(fill(\"ergophobie.molgen.mpg.de\",10),dir=\"/home/arndt\",exename=\"/usr/bin/julia\")\n",
"nheads() = @parallel (+) for i=1:2000000000\n",
" Int(rand(Bool))\n",
" end\n",
"\n",
"@time nheads()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Graphics - Plots"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"using Plots\n",
"#plotlyjs()\n",
"gr()\n",
"#unicodeplots()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"Plots.plot(rand(10,5),w=4)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# Probabilistic Programming\n",
"\n",
"\n",
"https://github.com/JuliaStats/PGM.jl\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Tasks\n",
"offer python's _yield_ like behavior"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"function producer(max::Int = 4)\n",
" for i in 1:max\n",
" produce(i)\n",
" end\n",
"end\n",
"\n",
"for i in Task(producer)\n",
" println(i)\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"for i in Task(() -> producer(2))\n",
" println(i)\n",
"end"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 0.4.5",
"language": "julia",
"name": "julia-0.4"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "0.4.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}