Last active
July 4, 2020 08:37
-
-
Save genkuroki/2bdf5999454c176ffb27d85e3e9d8b7b to your computer and use it in GitHub Desktop.
Iterators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "https://docs.julialang.org/en/v1/base/iterators/" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "iter1 = Iterators.repeated(\"hoge\")\niter1_take3 = Iterators.take(iter1, 3)\ncollect(iter1_take3)", | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 1, | |
"data": { | |
"text/plain": "3-element Array{String,1}:\n \"hoge\"\n \"hoge\"\n \"hoge\"" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "iter2 = Iterators.countfrom(0)\niter2_take5 = Iterators.take(iter2, 5)\ncollect(iter2_take5)", | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 2, | |
"data": { | |
"text/plain": "5-element Array{Int64,1}:\n 0\n 1\n 2\n 3\n 4" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "cat = Iterators.flatten((iter1_take3, iter2_take5))\ncollect(cat)", | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 3, | |
"data": { | |
"text/plain": "8-element Array{Any,1}:\n \"hoge\"\n \"hoge\"\n \"hoge\"\n 0\n 1\n 2\n 3\n 4" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "iter3 = Iterators.countfrom(0x0)\niter3_take4 = Iterators.take(iter3, 4)\np = Iterators.product(iter3_take4, iter2_take5)\ncollect(p)", | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 4, | |
"data": { | |
"text/plain": "4×5 Array{Tuple{UInt8,Int64},2}:\n (0x00, 0) (0x00, 1) (0x00, 2) (0x00, 3) (0x00, 4)\n (0x01, 0) (0x01, 1) (0x01, 2) (0x01, 3) (0x01, 4)\n (0x02, 0) (0x02, 1) (0x02, 2) (0x02, 3) (0x02, 4)\n (0x03, 0) (0x03, 1) (0x03, 2) (0x03, 3) (0x03, 4)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "iter4 = Iterators.cycle([:red, :green, :blue])\niter4_take8 = Iterators.take(iter4, 8)\ncollect(iter4_take8)", | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 5, | |
"data": { | |
"text/plain": "8-element Array{Symbol,1}:\n :red\n :green\n :blue\n :red\n :green\n :blue\n :red\n :green" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "x, state = iterate(iter4)", | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 6, | |
"data": { | |
"text/plain": "(:red, 2)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "x, state = iterate(iter4, state)", | |
"execution_count": 7, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 7, | |
"data": { | |
"text/plain": "(:green, 3)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "x, state = iterate(iter4, state)", | |
"execution_count": 8, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 8, | |
"data": { | |
"text/plain": "(:blue, 4)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "x, state = iterate(iter4, state)", | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 9, | |
"data": { | |
"text/plain": "(:red, 2)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "string.(iter4_take8)", | |
"execution_count": 10, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 10, | |
"data": { | |
"text/plain": "8-element Array{String,1}:\n \"red\"\n \"green\"\n \"blue\"\n \"red\"\n \"green\"\n \"blue\"\n \"red\"\n \"green\"" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "iter4_take8_not_red = Iterators.filter(x -> x != :red, iter4_take8)\ncollect(iter4_take8_not_red)", | |
"execution_count": 11, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 11, | |
"data": { | |
"text/plain": "5-element Array{Symbol,1}:\n :green\n :blue\n :green\n :blue\n :green" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "ac = Iterators.accumulate((x, y) -> x + (2y - 1), iter2_take5)\ncollect(ac)", | |
"execution_count": 12, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 12, | |
"data": { | |
"text/plain": "5-element Array{Int64,1}:\n 0\n 1\n 4\n 9\n 16" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "rev = Iterators.reverse(1:6)\ncollect(rev)", | |
"execution_count": 13, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 13, | |
"data": { | |
"text/plain": "6-element Array{Int64,1}:\n 6\n 5\n 4\n 3\n 2\n 1" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "using Printf\n\na = [\"abc$(@sprintf \"%04d\" k)\" for k in 0:10]\n@show a\niter = Iterators.partition(a, 4)\n@show length(iter)\ncollect(iter)", | |
"execution_count": 14, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "a = [\"abc0000\", \"abc0001\", \"abc0002\", \"abc0003\", \"abc0004\", \"abc0005\", \"abc0006\", \"abc0007\", \"abc0008\", \"abc0009\", \"abc0010\"]\nlength(iter) = 3\n", | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"execution_count": 14, | |
"data": { | |
"text/plain": "3-element Array{SubArray{String,1,Array{String,1},Tuple{UnitRange{Int64}},true},1}:\n [\"abc0000\", \"abc0001\", \"abc0002\", \"abc0003\"]\n [\"abc0004\", \"abc0005\", \"abc0006\", \"abc0007\"]\n [\"abc0008\", \"abc0009\", \"abc0010\"]" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "collect(Iterators.partition(collect(1:10), 4))", | |
"execution_count": 15, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 15, | |
"data": { | |
"text/plain": "3-element Array{SubArray{Int64,1,Array{Int64,1},Tuple{UnitRange{Int64}},true},1}:\n [1, 2, 3, 4]\n [5, 6, 7, 8]\n [9, 10]" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "collect(Iterators.partition(1:10, 4))", | |
"execution_count": 16, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 16, | |
"data": { | |
"text/plain": "3-element Array{UnitRange{Int64},1}:\n 1:4\n 5:8\n 9:10" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "for (d, files) in enumerate(Iterators.partition(a, 4))\n outputfile = @sprintf \"output%04d\" d\n @show files\n @show outputfile\n @show `foo $files -o $outputfile`\n println()\nend", | |
"execution_count": 17, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "files = [\"abc0000\", \"abc0001\", \"abc0002\", \"abc0003\"]\noutputfile = \"output0001\"\n`foo $files -o $outputfile` = `foo abc0000 abc0001 abc0002 abc0003 -o output0001`\n\nfiles = [\"abc0004\", \"abc0005\", \"abc0006\", \"abc0007\"]\noutputfile = \"output0002\"\n`foo $files -o $outputfile` = `foo abc0004 abc0005 abc0006 abc0007 -o output0002`\n\nfiles = [\"abc0008\", \"abc0009\", \"abc0010\"]\noutputfile = \"output0003\"\n`foo $files -o $outputfile` = `foo abc0008 abc0009 abc0010 -o output0003`\n\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "See alsp https://julialang.org/blog/2013/04/put-this-in-your-pipe/" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "julia-1.4", | |
"display_name": "Julia 1.4.2", | |
"language": "julia" | |
}, | |
"toc": { | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": false, | |
"base_numbering": 1, | |
"title_cell": "Table of Contents", | |
"title_sidebar": "Contents", | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": true, | |
"toc_window_display": false | |
}, | |
"language_info": { | |
"file_extension": ".jl", | |
"name": "julia", | |
"mimetype": "application/julia", | |
"version": "1.4.2" | |
}, | |
"@webio": { | |
"lastKernelId": null, | |
"lastCommId": null | |
}, | |
"gist": { | |
"id": "2bdf5999454c176ffb27d85e3e9d8b7b", | |
"data": { | |
"description": "Iterators", | |
"public": true | |
} | |
}, | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/2bdf5999454c176ffb27d85e3e9d8b7b" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment