GithubHelp home page GithubHelp logo

maxim2266 / strit Goto Github PK

View Code? Open in Web Editor NEW
84.0 6.0 0.0 35 KB

Package strit introduces a new type of string iterator, along with a number of iterator constructors, wrappers and combinators.

License: BSD 3-Clause "New" or "Revised" License

Go 100.00%
fluent-interface string-processing iteration function-composition golang pipeline-framework

strit's Introduction

strit

GoDoc Go Report Card License: BSD 3 Clause

Package strit (STRing ITerator) assists in development of string processing pipelines by providing a simple iteration model that allows for easy composition of processing stages.

Motivation

Suppose we want to develop a function that reads a file line by line, removes leading and trailing whitespace from each line, selects only non-empty lines that also do not start with the # symbol, and stores those lines in a slice of strings. Using the Go standard library one possible implementation of the function may look like this:

func ReadConfig(fileName string) ([]string, error) {
	file, err := os.Open(fileName)

	if err != nil {
		return nil, err
	}

	defer file.Close()

	var res []string
	src := bufio.NewScanner(file)

	for src.Scan() {
		line := bytes.TrimSpace(src.Bytes())

		if len(line) > 0 && line[0] != '#' {
			res = append(res, string(line))
		}
	}

	if err = src.Err(); err != nil {
		return nil, err
	}

	return res, nil
}

Using strit package the implementation can be simplified down to:

func ReadConfig(fileName string) ([]string, error) {
	return strit.FromFile(fileName).
           Map(bytes.TrimSpace).
           Filter(strit.Not(strit.Empty).AndNot(strit.StartsWith("#"))).
           Strings()

Features

More examples:

  • Naïve grep:
func main() {
	_, err := strit.FromReader(os.Stdin).
			Filter(regexp.MustCompile(os.Args[1]).Match).
			WriteSepTo(os.Stdout, "\n")

	if err != nil {
		os.Stderr.WriteString(err.Error() + "\n")
		os.Exit(1)
	}
}
  • Recursively find all the filesystem entries matching the given regular expression:
func selectEntries(root string, re *regexp.Regexp) ([]string, error) {
	return FromDirWalk(root, nil).Filter(re.Match).Strings()
}
  • Build a list of .flac files in the given directory, annotating each name with its corresponding track number from FLAC metadata:
func namesWithTrackNumbers(dir string) ([]string, error) {
	return strit.FromDir(dir, func(info os.FileInfo) bool { return info.Mode().IsRegular() }).
		Filter(strit.EndsWith(".flac")).
		GenMap(prependTrackNo).
		Strings()
}

func prependTrackNo(file []byte) ([]byte, error) {
	name := string(file)

	no, err := strit.FromCommand(exec.Command("metaflac", "--list", "--block-type=VORBIS_COMMENT", name)).
		FirstNonEmpty(func(s []byte) []byte {
			if m := match(s); len(m) == 2 {
				return m[1]
			}

			return nil
		}).
		String()

	if err != nil {
		return nil, err
	}

	if len(no) == 0 {
		return []byte("???: " + filepath.Base(name)), nil
	}

	return []byte(no + ": " + filepath.Base(name)), nil
}

var match = regexp.MustCompile(`tracknumber=([[:digit:]]+)$`).FindSubmatch

Project status

The project is in a beta state. Tested on Linux Mint 19.1, with Go version 1.12. Should also work on other platforms supported by Go runtime, but currently this is not very well tested.

License: BSD

strit's People

Contributors

maxim2266 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.