MCPcopy
hub / github.com/bitfield/script / First

Method First

script.go:580–597  ·  view source on GitHub ↗

First produces only the first n lines of the pipe's contents, or all the lines if there are less than n. If n is zero or negative, there is no output at all. When n lines have been produced, First stops reading its input and sends EOF to its output.

(n int)

Source from the content-addressed store, hash-verified

578// at all. When n lines have been produced, First stops reading its input and
579// sends EOF to its output.
580func (p *Pipe) First(n int) *Pipe {
581 if p.Error() != nil {
582 return p
583 }
584 if n <= 0 {
585 return NewPipe()
586 }
587 return p.Filter(func(r io.Reader, w io.Writer) error {
588 scanner := newScanner(r)
589 for i := 0; i < n && scanner.Scan(); i++ {
590 _, err := fmt.Fprintln(w, scanner.Text())
591 if err != nil {
592 return err
593 }
594 }
595 return scanner.Err()
596 })
597}
598
599// Freq produces only the unique lines from the pipe's contents, each prefixed
600// with a frequency count, in descending numerical order (most frequent lines

Calls 4

ErrorMethod · 0.95
FilterMethod · 0.95
NewPipeFunction · 0.85
newScannerFunction · 0.85