Something went wrong. Try again.
Showcasing the power of iterators
Something went wrong. Try again.
iterator-power iterators101.ipynb
6.6 kB · 295 lines
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "101b1bfd-d20e-429d-b6d0-8c846f8eb8c5", "metadata": {}, "outputs": [], "source": [ "func gen123(yield func(int) bool) {\n", "\t_ = yield(1) && yield(2) && yield(3)\n", "}" ] }, { "cell_type": "code", "execution_count": 2, "id": "d015fe66-05ee-4b12-91b6-523235dbc726", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "%%\n", "for n := range gen123 {\n", " fmt.Println(n)\n", "}" ] }, { "cell_type": "code", "execution_count": 3, "id": "18fb40fc-a704-4c2a-9e31-50294e370c7a", "metadata": {}, "outputs": [], "source": [ "func YIELD(_ int) bool {\n", " return true\n", "}\n", "\n", "%%\n", "gen123(YIELD) // returns no value" ] }, { "cell_type": "code", "execution_count": 4, "id": "05dbdca4-13d4-4a12-a982-4ca9dfd8fad5", "metadata": {}, "outputs": [], "source": [ "func genAB(yield func(rune) bool) {\n", " fmt.Println(\"[começando]\")\n", "\tif ! yield('A') {\n", " return\n", " }\n", " fmt.Println(\"[seguindo]\")\n", "\tif ! yield('B') {\n", " return\n", " }\n", " fmt.Println(\"[terminando]\")\n", "}" ] }, { "cell_type": "code", "execution_count": 5, "id": "8c9bb0ec-065a-4cad-8250-2da7c9d71d13", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[começando]\n", "-> 'A'\n", "[seguindo]\n", "-> 'B'\n", "[terminando]\n" ] } ], "source": [ "%%\n", "for c := range genAB {\n", " fmt.Printf(\"-> %q\\n\", c)\n", "}" ] }, { "cell_type": "markdown", "id": "aaff0af1-d12a-4717-9d40-8efd000b26f7", "metadata": {}, "source": [ "Split string into fields:" ] }, { "cell_type": "code", "execution_count": 6, "id": "0b8e68c2-0631-4f8e-95d4-dc659eeea9b5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\"The\"\n", "\"quick\"\n", "\"brown\"\n", "\"fox\"\n" ] } ], "source": [ "%%\n", "text := \"The quick brown fox\"\n", "for word := range strings.FieldsSeq(text) {\n", " fmt.Printf(\"%q\\n\", word)\n", "}" ] }, { "cell_type": "markdown", "id": "8a00836c-e3ba-440f-9712-78e8b7ae1133", "metadata": {}, "source": [ "Extract integers:" ] }, { "cell_type": "code", "execution_count": 7, "id": "8d408926-a972-4fec-a863-776afa7c946c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\"123\"\n", "\"456\"\n" ] } ], "source": [ "func notDigit(c rune) bool {\n", " return c < '0' || c > '9'\n", "}\n", "\n", "%%\n", "\n", "mixedText := \"abc123def456ghi\"\n", "for intStr := range strings.FieldsFuncSeq(mixedText, notDigit) {\n", " fmt.Printf(\"%q\\n\", intStr)\n", "}\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "79994c10-86c9-4fb1-870a-3e6c05cbcbe8", "metadata": {}, "outputs": [], "source": [ "// Sentence wraps a piece of text and yields its words lazily.\n", "type Sentence struct {\n", "\ttext string\n", "}\n", "\n", "// New returns a Sentence over the given text.\n", "func New(text string) *Sentence {\n", "\treturn &Sentence{text: text}\n", "}\n", "\n", "// String truncates long text so debugging output for\n", "// a huge sentence stays readable.\n", "func (s *Sentence) String() string {\n", "\treturn fmt.Sprintf(\"Sentence{%#v}\", shortRepr(s.text, 30))\n", "}\n", "\n", "func notWord(r rune) bool {\n", "\treturn !(unicode.IsLetter(r) || unicode.IsDigit(r) || r == '-')\n", "}\n", "\n", "// Words returns an iterator over the words in the sentence.\n", "func (s *Sentence) Words() iter.Seq[string] {\n", "\treturn strings.FieldsFuncSeq(s.text, notWord)\n", "}\n", "\n", "// shortRepr mimics reprlib.repr for strings: quote the value, and\n", "// if it's longer than maxLen runes, replace the middle with \"…\".\n", "func shortRepr(s string, maxLen int) string {\n", "\tif utf8.RuneCountInString(s) <= maxLen {\n", "\t\treturn fmt.Sprintf(\"%s\", s)\n", "\t}\n", "\trunes := []rune(s)\n", "\thead := string(runes[:maxLen/2])\n", "\ttail := string(runes[len(runes)-maxLen/2:])\n", "\treturn fmt.Sprintf(\"%s\", head+\"…\"+tail)\n", "}" ] }, { "cell_type": "code", "execution_count": 9, "id": "1ae93130-4875-4e81-84d2-9b44b0834adc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{The time has come to speak of many things}\n", "main.Sentence\tmain.Sentence{text:\"The time has come to speak of many things\"}\n", "The\n", "time\n", "has\n", "come\n", "to\n", "speak\n", "of\n", "many\n", "things\n" ] } ], "source": [ "%%\n", "s := Sentence{\"The time has come to speak of many things\"}\n", " \n", "fmt.Println(s) // calls Sentence.String() via the Stringer interface\n", "fmt.Printf(\"%T\\t%#v\\n\", s, s)\n", "\n", "for word := range s.Words() {\n", " fmt.Println(word)\n", "}" ] }, { "cell_type": "code", "execution_count": 10, "id": "5db65ffb-c7d0-4346-8785-158b659f6fd4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "main.Sentence\tmain.Sentence{text:\"\\\"The time has c…f many things:\\\"\"}" ] } ], "source": [ "%%\n", "s := Sentence{`\"The time has c…f many things:\"`}\n", "fmt.Printf(\"%T\\t%#v\", s, s)" ] }, { "cell_type": "code", "execution_count": null, "id": "77d101ce-6220-45ad-a369-ca64beae0d9e", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Go (gonb)", "language": "go", "name": "gonb" }, "language_info": { "codemirror_mode": "", "file_extension": ".go", "mimetype": "text/x-go", "name": "go", "nbconvert_exporter": "", "pygments_lexer": "", "version": "go1.25.8" } }, "nbformat": 4, "nbformat_minor": 5}