src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/eval/vals/dissoc.go (about)

     1  package vals
     2  
     3  // Dissocer wraps the Dissoc method.
     4  type Dissocer interface {
     5  	// Dissoc returns a slightly modified version of the receiver with key k
     6  	// dissociated with any value.
     7  	Dissoc(k any) any
     8  }
     9  
    10  // Dissoc takes a container and a key, and returns a modified version of the
    11  // container, with the given key dissociated with any value. It is implemented
    12  // for the Map type and types satisfying the Dissocer interface. For other
    13  // types, it returns nil.
    14  func Dissoc(a, k any) any {
    15  	switch a := a.(type) {
    16  	case Map:
    17  		return a.Dissoc(k)
    18  	case StructMap:
    19  		return promoteToMap(a).Dissoc(k)
    20  	case Dissocer:
    21  		return a.Dissoc(k)
    22  	default:
    23  		return nil
    24  	}
    25  }