daily pastebin goal
64%
SHARE
TWEET

[Go][excerpt] list filter.

cwchen Nov 21st, 2017 136 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
Top