77 "mz.attahri.com/code/lines"
88)
99
10+ // ExampleHead demonstrates iterating over the first n lines of a reader.
1011func ExampleHead () {
1112 r := strings .NewReader ("one\n two\n three\n four\n five" )
1213 for line , err := range lines .Head (r , 3 ) {
@@ -21,6 +22,7 @@ func ExampleHead() {
2122 // three
2223}
2324
25+ // ExampleTail demonstrates iterating over the last n lines of a reader in reverse order.
2426func ExampleTail () {
2527 r := strings .NewReader ("one\n two\n three\n four\n five" )
2628 for line , err := range lines .Tail (r , 2 ) {
@@ -34,6 +36,7 @@ func ExampleTail() {
3436 // four
3537}
3638
39+ // ExampleFilter demonstrates filtering lines that exactly match a given value.
3740func ExampleFilter () {
3841 r := strings .NewReader ("apple\n banana\n apricot\n cherry" )
3942 for line , err := range lines .Filter (lines .All (r ), "banana" ) {
@@ -46,6 +49,7 @@ func ExampleFilter() {
4649 // Line 2: banana
4750}
4851
52+ // ExampleFilterFunc demonstrates filtering lines using a custom predicate function.
4953func ExampleFilterFunc () {
5054 r := strings .NewReader ("apple\n banana\n apricot\n cherry" )
5155 for line , err := range lines .FilterFunc [string ](lines .All (r ), func (s string ) bool {
@@ -61,6 +65,7 @@ func ExampleFilterFunc() {
6165 // apricot
6266}
6367
68+ // ExampleTake demonstrates limiting a scanner to the first n lines.
6469func ExampleTake () {
6570 r := strings .NewReader ("one\n two\n three\n four\n five" )
6671 for line , err := range lines .Take (lines .All (r ), 2 ) {
@@ -74,6 +79,7 @@ func ExampleTake() {
7479 // two
7580}
7681
82+ // ExampleSkip demonstrates skipping the first n lines of a scanner.
7783func ExampleSkip () {
7884 r := strings .NewReader ("one\n two\n three\n four\n five" )
7985 for line , err := range lines .Skip (lines .All (r ), 3 ) {
@@ -87,6 +93,7 @@ func ExampleSkip() {
8793 // five
8894}
8995
96+ // ExampleRange demonstrates extracting a range of lines by line number.
9097func ExampleRange () {
9198 r := strings .NewReader ("one\n two\n three\n four\n five" )
9299 for line , err := range lines .Range (lines .All (r ), 2 , 4 ) {
@@ -101,6 +108,7 @@ func ExampleRange() {
101108 // four
102109}
103110
111+ // ExampleTakeWhile demonstrates yielding lines while a predicate holds true.
104112func ExampleTakeWhile () {
105113 r := strings .NewReader ("# comment 1\n # comment 2\n code\n # not a header" )
106114 for line , err := range lines .TakeWhile [string ](lines .All (r ), func (s string ) bool {
@@ -116,6 +124,7 @@ func ExampleTakeWhile() {
116124 // # comment 2
117125}
118126
127+ // ExampleSkipWhile demonstrates skipping lines while a predicate holds true.
119128func ExampleSkipWhile () {
120129 r := strings .NewReader ("# comment 1\n # comment 2\n code\n more code" )
121130 for line , err := range lines .SkipWhile [string ](lines .All (r ), func (s string ) bool {
@@ -131,6 +140,7 @@ func ExampleSkipWhile() {
131140 // more code
132141}
133142
143+ // ExampleMap demonstrates transforming each line's content.
134144func ExampleMap () {
135145 r := strings .NewReader ("hello\n world" )
136146 for upper , err := range lines .Map [string ](lines .All (r ), func (l * lines.Line ) string {
@@ -146,6 +156,7 @@ func ExampleMap() {
146156 // WORLD
147157}
148158
159+ // ExampleCollect demonstrates materializing a scanner into a slice.
149160func ExampleCollect () {
150161 r := strings .NewReader ("one\n two\n three" )
151162 all , err := lines .Collect (lines .All (r ))
@@ -159,6 +170,7 @@ func ExampleCollect() {
159170 // two
160171}
161172
173+ // ExampleGet demonstrates retrieving a specific line by number.
162174func ExampleGet () {
163175 r := strings .NewReader ("one\n two\n three" )
164176 line , err := lines .Get (lines .All (r ), 2 )
@@ -170,6 +182,7 @@ func ExampleGet() {
170182 // two
171183}
172184
185+ // ExampleContains demonstrates checking if a line exists in a scanner.
173186func ExampleContains () {
174187 r := strings .NewReader ("apple\n banana\n cherry" )
175188 found , err := lines .Contains (lines .All (r ), "banana" )
@@ -181,6 +194,7 @@ func ExampleContains() {
181194 // true
182195}
183196
197+ // ExampleIndex demonstrates finding the line number of the first matching line.
184198func ExampleIndex () {
185199 r := strings .NewReader ("apple\n banana\n cherry" )
186200 lineno , err := lines .Index (lines .All (r ), "banana" )
@@ -192,6 +206,7 @@ func ExampleIndex() {
192206 // 2
193207}
194208
209+ // ExampleLastIndex demonstrates finding the line number of the last matching line.
195210func ExampleLastIndex () {
196211 r := strings .NewReader ("apple\n banana\n apple\n cherry" )
197212 lineno , err := lines .LastIndex (lines .All (r ), "apple" )
@@ -203,6 +218,7 @@ func ExampleLastIndex() {
203218 // 3
204219}
205220
221+ // ExampleFindAll demonstrates finding all line numbers matching a predicate.
206222func ExampleFindAll () {
207223 r := strings .NewReader ("TODO: first\n done\n TODO: second\n done" )
208224 matches , err := lines .FindAll [string ](lines .All (r ), func (s string ) bool {
@@ -216,6 +232,7 @@ func ExampleFindAll() {
216232 // [1 3]
217233}
218234
235+ // ExampleCount demonstrates counting lines equal to a given value.
219236func ExampleCount () {
220237 r := strings .NewReader ("apple\n banana\n apple\n cherry" )
221238 n , err := lines .Count (lines .All (r ), "apple" )
@@ -227,6 +244,7 @@ func ExampleCount() {
227244 // 2
228245}
229246
247+ // ExampleCountFunc demonstrates counting lines matching a predicate.
230248func ExampleCountFunc () {
231249 r := strings .NewReader ("short\n a]very long line here\n medium" )
232250 n , err := lines .CountFunc (lines .All (r ), func (s string ) bool {
0 commit comments