{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### 5-5-4 Enumerator" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = []" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "#" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.each" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "#" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[].each" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[]\n" ] }, { "data": { "text/plain": [ "[]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p a \n", "a" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "#" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[].each\n" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "#" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{}.each" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "#\n" ] }, { "data": { "text/plain": [ "#" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p (1..10).each\n", "''.each_char" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "#" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10.times" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "#" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "loop" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "#\n" ] }, { "data": { "text/plain": [ "#" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p [1, 2, 3].to_enum\n", "'Alice'.enum_for(:each_char)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "#\n", "[6, 4, 8]\n" ] }, { "data": { "text/plain": [ "[6, 4, 8]" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lines = <:map>" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "enum.map" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0: Alice\n", "1: Bob\n", "2: Charlie\n" ] }, { "data": { "text/plain": [ "[\"Alice\", \"Bob\", \"Charlie\"]" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%w(Alice Bob Charlie).each.with_index do |n, i|\n", " puts \"#{i}: #{n}\"\n", "end" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[\"Luige\", \"Link\"]" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%w(Mario Luige Link).select.with_index {|n, i|\n", " i > 0\n", "}" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "4\n", "2\n" ] }, { "data": { "text/plain": [ "2" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "enum = [4, 4, 2, 3].to_enum\n", "\n", "p enum.next\n", "p enum.next\n", "p enum.next" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "enum.rewind\n", "\n", "enum.next" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "4\n", "2\n", "3\n" ] }, { "data": { "text/plain": [ "[4, 4, 2, 3]" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "enum.rewind\n", "\n", "loop do\n", " puts enum.next\n", "end" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A (14)\n", "B (32)\n", "C (28)\n" ] }, { "data": { "text/plain": [ "[\"A\", \"B\", \"C\"]" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people = %w(A B C).to_enum\n", "ages = [14, 32, 28].to_enum\n", "\n", "loop do \n", " person = people.next\n", " age = ages.next\n", " \n", " puts \"#{person} (#{age})\"\n", "end" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A (14)\n", "B (32)\n", "C (28)\n" ] }, { "data": { "text/plain": [ "[[\"A\", 14], [\"B\", 32], [\"C\", 28]]" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people.zip(ages).each do |p, a|\n", " puts \"#{p} (#{a})\"\n", "end" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "#" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people.zip(ages).to_enum" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A (14)\n", "B (32)\n", "C (28)\n" ] } ], "source": [ "people.zip(ages) do |p, a|\n", " puts \"#{p} (#{a})\"\n", "end\n", "# メモリは食ってる?" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[\"Alice\", \"Charlie\"]\n" ] } ], "source": [ "enum = %w(Alice Bob Charlie).select\n", "\n", "loop do \n", " begin\n", " person = enum.next\n", " \n", " enum.feed /li/ === person\n", " rescue StopIteration => e\n", " p e.result\n", " \n", " break\n", " end\n", "end\n" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[\"Bob\"]\n" ] } ], "source": [ "enum = %w(Alice Bob Charlie).select\n", "\n", "loop do \n", " begin\n", " person = enum.next\n", " \n", " enum.feed person.size < 4\n", " rescue StopIteration => e\n", " p e.result\n", " \n", " break\n", " end\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "次回は \"5-5-5 Enumrator::Lazy\" から" ] } ], "metadata": { "kernelspec": { "display_name": "Ruby 2.4.1", "language": "ruby", "name": "ruby" }, "language_info": { "file_extension": ".rb", "mimetype": "application/x-ruby", "name": "ruby", "version": "2.4.1" } }, "nbformat": 4, "nbformat_minor": 2 }