github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/eval/builtin_fn_pred.go (about)

     1  package eval
     2  
     3  import "src.elv.sh/pkg/eval/vals"
     4  
     5  // Basic predicate commands.
     6  
     7  //elvdoc:fn bool
     8  //
     9  // ```elvish
    10  // bool $value
    11  // ```
    12  //
    13  // Convert a value to boolean. In Elvish, only `$false` and errors are booleanly
    14  // false. Everything else, including 0, empty strings and empty lists, is booleanly
    15  // true:
    16  //
    17  // ```elvish-transcript
    18  // ~> bool $true
    19  // ▶ $true
    20  // ~> bool $false
    21  // ▶ $false
    22  // ~> bool $ok
    23  // ▶ $true
    24  // ~> bool ?(fail haha)
    25  // ▶ $false
    26  // ~> bool ''
    27  // ▶ $true
    28  // ~> bool []
    29  // ▶ $true
    30  // ~> bool abc
    31  // ▶ $true
    32  // ```
    33  //
    34  // @cf not
    35  
    36  func init() {
    37  	addBuiltinFns(map[string]interface{}{
    38  		"bool":   vals.Bool,
    39  		"not":    not,
    40  		"is":     is,
    41  		"eq":     eq,
    42  		"not-eq": notEq,
    43  	})
    44  }
    45  
    46  //elvdoc:fn not
    47  //
    48  // ```elvish
    49  // not $value
    50  // ```
    51  //
    52  // Boolean negation. Examples:
    53  //
    54  // ```elvish-transcript
    55  // ~> not $true
    56  // ▶ $false
    57  // ~> not $false
    58  // ▶ $true
    59  // ~> not $ok
    60  // ▶ $false
    61  // ~> not ?(fail error)
    62  // ▶ $true
    63  // ```
    64  //
    65  // **Note**: The related logical commands `and` and `or` are implemented as
    66  // [special commands](language.html#special-commands) instead, since they do not
    67  // always evaluate all their arguments. The `not` command always evaluates its
    68  // only argument, and is thus a normal command.
    69  //
    70  // @cf bool
    71  
    72  func not(v interface{}) bool {
    73  	return !vals.Bool(v)
    74  }
    75  
    76  //elvdoc:fn is
    77  //
    78  // ```elvish
    79  // is $values...
    80  // ```
    81  //
    82  // Determine whether all `$value`s have the same identity. Writes `$true` when
    83  // given no or one argument.
    84  //
    85  // The definition of identity is subject to change. Do not rely on its behavior.
    86  //
    87  // ```elvish-transcript
    88  // ~> is a a
    89  // ▶ $true
    90  // ~> is a b
    91  // ▶ $false
    92  // ~> is [] []
    93  // ▶ $true
    94  // ~> is [a] [a]
    95  // ▶ $false
    96  // ```
    97  //
    98  // @cf eq
    99  //
   100  // Etymology: [Python](https://docs.python.org/3/reference/expressions.html#is).
   101  
   102  func is(args ...interface{}) bool {
   103  	for i := 0; i+1 < len(args); i++ {
   104  		if args[i] != args[i+1] {
   105  			return false
   106  		}
   107  	}
   108  	return true
   109  }
   110  
   111  //elvdoc:fn eq
   112  //
   113  // ```elvish
   114  // eq $values...
   115  // ```
   116  //
   117  // Determine whether all `$value`s are structurally equivalent. Writes `$true` when
   118  // given no or one argument.
   119  //
   120  // ```elvish-transcript
   121  // ~> eq a a
   122  // ▶ $true
   123  // ~> eq [a] [a]
   124  // ▶ $true
   125  // ~> eq [&k=v] [&k=v]
   126  // ▶ $true
   127  // ~> eq a [b]
   128  // ▶ $false
   129  // ```
   130  //
   131  // @cf is not-eq
   132  //
   133  // Etymology: [Perl](https://perldoc.perl.org/perlop.html#Equality-Operators).
   134  
   135  func eq(args ...interface{}) bool {
   136  	for i := 0; i+1 < len(args); i++ {
   137  		if !vals.Equal(args[i], args[i+1]) {
   138  			return false
   139  		}
   140  	}
   141  	return true
   142  }
   143  
   144  //elvdoc:fn not-eq
   145  //
   146  // ```elvish
   147  // not-eq $values...
   148  // ```
   149  //
   150  // Determines whether every adjacent pair of `$value`s are not equal. Note that
   151  // this does not imply that `$value`s are all distinct. Examples:
   152  //
   153  // ```elvish-transcript
   154  // ~> not-eq 1 2 3
   155  // ▶ $true
   156  // ~> not-eq 1 2 1
   157  // ▶ $true
   158  // ~> not-eq 1 1 2
   159  // ▶ $false
   160  // ```
   161  //
   162  // @cf eq
   163  
   164  func notEq(args ...interface{}) bool {
   165  	for i := 0; i+1 < len(args); i++ {
   166  		if vals.Equal(args[i], args[i+1]) {
   167  			return false
   168  		}
   169  	}
   170  	return true
   171  }