github.com/mmatczuk/gohan@v0.0.0-20170206152520-30e45d9bdb69/extension/gohanscript/lib/list.go (about)

     1  // Copyright (C) 2016  Juniper Networks, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  // implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package lib
    17  
    18  //Append add element to the list and return new list.
    19  func Append(list []interface{}, value interface{}) []interface{} {
    20  	return append(list, value)
    21  }
    22  
    23  //Contains checks if a value is in a list.
    24  func Contains(list []interface{}, value interface{}) bool {
    25  	for _, item := range list {
    26  		if item == value {
    27  			return true
    28  		}
    29  	}
    30  	return false
    31  }
    32  
    33  //Size checks length of a list
    34  func Size(list []interface{}) int {
    35  	return len(list)
    36  }
    37  
    38  //Shift retrives first element and left
    39  func Shift(list []interface{}) (interface{}, []interface{}) {
    40  	return list[0], list[1:]
    41  }
    42  
    43  //Unshift pushes an item on the beginnings
    44  func Unshift(list []interface{}, value interface{}) []interface{} {
    45  	return append([]interface{}{value}, list...)
    46  }
    47  
    48  //Copy copies a list
    49  func Copy(list []interface{}) []interface{} {
    50  	return append([]interface{}(nil), list...)
    51  }
    52  
    53  //Delete deletes an item from list
    54  func Delete(list []interface{}, index int) []interface{} {
    55  	return append(list[:index], list[index+1:]...)
    56  }
    57  
    58  //First return the first element
    59  func First(list []interface{}) interface{} {
    60  	if len(list) == 0 {
    61  		return nil
    62  	}
    63  	return list[0]
    64  }
    65  
    66  //Last return the last element
    67  func Last(list []interface{}) interface{} {
    68  	if len(list) == 0 {
    69  		return nil
    70  	}
    71  	return list[len(list)-1]
    72  }