FAQ
Thanx to everyone, I finally end doing something like this.

*// When I parse my SearchParams, I check which ones are present and I know
which filters to apply*

*// and append them to a slice of filters on the struct (pointers to
functions).*

type Filter func(*SearchParams, *Thing) bool

func (t Things) Search(s *SearchParams) Things {
   results := make(Things, 0, DefaultCap)
   for _, thing := range(t) { *// iterate over my things*
     for _, f := range(s.Filters) {
       if f(s, thing) { *//and over my filters*

         *// if any filter match, that thing is good to ship*

         results = append(results, thing)
         break
       } else { * // if any filter do not match, then we won't check the
others.*
         break
       }
     }
   }
   return results
}


An example of a filter.

func ByName(s *SearchParams, t *Thing) bool {
   if strings.Contains(t.Name, s.Name) {
     return true
   }
   return false
}

I'm liking the code right know, I tried to optimize as much as I could
while trying to keep the code understandable and small. Thoughts on
implementation are appreciated. And thanks everyone for their comments.

On Wednesday, October 1, 2014 1:43:24 PM UTC-5, egon wrote:


On Wednesday, 1 October 2014 21:34:09 UTC+3, Guillermo Estrada wrote:

Factories sounds nice!! Kinda like working in Ruby. Do you have any
example of this ? (ir any project in github using them) to give me am idea
of am idiomatic way to refactor this.

He just means a function that returns a function... e.g.

func ByName(name string) func(*Thing) bool {
return func(t *Thing) { return t.Name == name }
}

Also, you might be overthinking this..
for loops are simpler solution in these cases... e.g.

for _, t := range things {
if t.Name == "something" && t.Age > 10 {
fmt.Println(t)
}
}

+ egon
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 7 of 11 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 30, '14 at 3:11a
activeOct 4, '14 at 5:16a
posts11
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase