Code Monkey home page Code Monkey logo

anno's People

Contributors

matryer 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

Watchers

 avatar  avatar  avatar

anno's Issues

On the subject of interfaces

Hi Mat,
I was brought here by the Medium article so this is very much a learning question for me.
In trying to grok the article and figure out why you used an interface I refactored anno.go without the interface and it seemed to work but I wonder have I missed something.

Listening to "Go Time" you said that you had overused interfaces early on and I have Googled the subject and found opposing advice: 'use interfaces freely' and 'don't overuse interfaces'.

I've only refactored anno.go and run its test so there may be something in the rest of the code that's pertinent to the interface.

So, I'll stop rambling, is there something in this code that is not idiomatic or will come and bite me in the bum or is it just a matter of style:

package main

// This has bare minimum changes to make work without interface.

import (
	"bytes"
	"fmt"
	"log"
)

// ErrNoMatch is returned when a match was expected but
// can not be found.
// Can be returned from FieldFunc.
type ErrNoMatch []byte

func (e ErrNoMatch) Error() string {
	return "no match for '" + string(e) + "'"
}

// Note represents something interesting within
// text.
type Note struct {
	Val   []byte `json:"val"`
	Start int    `json:"start"`
	Kind  string `json:"kind"`
}

// Notes is a sortable slice of Note objects.
type Notes []*Note

// End calculates the end position of this note.
func (n *Note) End() int {
	return n.Start + len(n.Val)
}

// This satisfies sort.Interface
func (n Notes) Len() int           { return len(n) }
func (n Notes) Swap(i, j int)      { n[i], n[j] = n[j], n[i] }
func (n Notes) Less(i, j int) bool { return n[i].Start < n[j].Start }

// FinderFunc represents a function capable of finding
// notes.
type FinderFunc func(s []byte) (Notes, error)

// FieldFunc returns a FinderFunc that finds notes on a per field basis.
// The fn returns true if it's a match, and optionally a subset of the the
// match.
// The returned []byte must be contained in the source string, otherwise
// ErrNoMatch will be returned.
func FieldFunc(kind string, fn func(b []byte) (bool, []byte)) FinderFunc {
	return func(src []byte) (Notes, error) {
		var pos int
		var notes Notes
		fields := bytes.Fields(src)
		for _, f := range fields {
			if ok, match := fn(f); ok {
				s := bytes.Index(src[pos:], match) + pos
				if s == -1 {
					// true was returned without the returned bytes
					// appearing in the match.
					return nil, ErrNoMatch(match)
				}
				notes = append(notes, &Note{
					Val:   match,
					Start: s,
					Kind:  kind,
				})
			}
			pos += len(f) + 1
		}
		return notes, nil
	}
}

// FindManyString runs all finders against the source and returns a
// slice of notes or an error.
func FindManyString(src string, finders ...FinderFunc) (Notes, error) {
	return FindMany([]byte(src), finders...)
}

// FindMany runs all finders against the source and returns a
// slice of notes or an error.
func FindMany(src []byte, finders ...FinderFunc) (Notes, error) {
	var allNotes Notes
	for _, finder := range finders {
		notes, err := finder(src)
		if err != nil {
			return nil, err
		}
		allNotes = append(allNotes, notes...)
	}
	return allNotes, nil
}

func main() {
	// s := "Find http://www.websites.com/ and  # #hashtags and @mentions and @more mentions easily"
	s := "A simple string with no notes."
	notes, err := FindManyString(s, Emails, URLs, Mentions, Hashtags)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("Source:", s)

	for _, note := range notes {
		fmt.Printf("Found a %s at [%d:%d]: \"%s\"\n", note.Kind, note.Start, note.End(), note.Val)
	}
}

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.