diff --git a/sentence/iterators101.ipynb b/iterators101.ipynb similarity index 88% rename from sentence/iterators101.ipynb rename to iterators101.ipynb index 8be4902..766333b 100644 --- a/sentence/iterators101.ipynb +++ b/iterators101.ipynb @@ -38,6 +38,21 @@ { "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": [], @@ -57,7 +72,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "id": "8c9bb0ec-065a-4cad-8250-2da7c9d71d13", "metadata": {}, "outputs": [ @@ -90,7 +105,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "id": "0b8e68c2-0631-4f8e-95d4-dc659eeea9b5", "metadata": {}, "outputs": [ @@ -123,7 +138,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "id": "8d408926-a972-4fec-a863-776afa7c946c", "metadata": {}, "outputs": [ @@ -151,15 +166,11 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 8, "id": "79994c10-86c9-4fb1-870a-3e6c05cbcbe8", "metadata": {}, "outputs": [], "source": [ - "func notWord(r rune) bool {\n", - "\treturn !(unicode.IsLetter(r) || unicode.IsDigit(r) || r == '-')\n", - "}\n", - "\n", "// Sentence wraps a piece of text and yields its words lazily.\n", "type Sentence struct {\n", "\ttext string\n", @@ -176,6 +187,10 @@ "\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", @@ -196,7 +211,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 9, "id": "1ae93130-4875-4e81-84d2-9b44b0834adc", "metadata": {}, "outputs": [ @@ -205,7 +220,8 @@ "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\"}The\n", + "main.Sentence\tmain.Sentence{text:\"The time has come to speak of many things\"}\n", + "The\n", "time\n", "has\n", "come\n", @@ -222,7 +238,7 @@ "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\", s, s)\n", + "fmt.Printf(\"%T\\t%#v\\n\", s, s)\n", "\n", "for word := range s.Words() {\n", " fmt.Println(word)\n", @@ -231,7 +247,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 10, "id": "5db65ffb-c7d0-4346-8785-158b659f6fd4", "metadata": {}, "outputs": [ diff --git a/sentence/sentence.ipynb b/sentence/sentence.ipynb new file mode 100644 index 0000000..0d66287 --- /dev/null +++ b/sentence/sentence.ipynb @@ -0,0 +1,131 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "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": 2, + "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": 3, + "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 +}