Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Projection #4

Open
jamesgarfield opened this issue Dec 19, 2014 · 0 comments
Open

Projection #4

jamesgarfield opened this issue Dec 19, 2014 · 0 comments

Comments

@jamesgarfield
Copy link
Contributor

Projection

Projection is a reference to both the deeper level of inference that is involved (projecting what will be needed) and to one of the key uses of the technique (projecting a set of one type into a set of another)

In that it differs greatly from mere implementation, it may require a separate sub-command for write (goast write project)

Consider the following User and Users definitions. It could reasonably be inferred that one might want to project a given Users into []string (slice of names) or []int (slice of ages)

type User struct {
    Name string
    Age  int
}
type Users []*User

With projection the following generic code could be transformed into multiple, distinct operations on Users

type X interface{}
type T struct {
    _ X
}
type Slice []*T

func (s Slice) MapTo_(fn func(T) X) (result []X) {
    for _, v := range s {
        result = append(result, fn(v))
    }
    return
}

Becomes

func (s Users) MapToString(fn func(*User) string) (result []string) {
    for _, v := range s {
        result = append(result, fn(v))
    }
    return
}

func (s Users) MapToInt(fn func(*User) int) (result []int) {
    for _, v := range s {
        result = append(result, fn(v))
    }
    return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant