Advertisement
cwchen

[Go][excerpt] list filter.

Nov 21st, 2017
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.46 KB | None | 0 0
  1. // Excerpt
  2. // Select items from the list when item fulfill the predicate.
  3. // Implement grep function object to use this method.
  4. func (list *List) Select(grep func(interface{}) (bool, error)) (*List, error) {
  5.     newList := New()
  6.  
  7.     current := list.head
  8.     for current != nil {
  9.         b, err := grep(current.data)
  10.         if err != nil {
  11.             return newList, err
  12.         }
  13.  
  14.         if b == true {
  15.             newList.Push(current.data)
  16.         }
  17.  
  18.         current = current.next
  19.     }
  20.  
  21.     return newList, nil
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement