Debian Codegraph @

dstream/mutate.go

raw symbols SCIP
1 package dstream
2
3 import "fmt"
4
5 // MutateFunc is a function that can be used to change the values of a variable in-place.
6 type MutateFunc func(interface{})
7
8 type mutated struct {
9 xform
10
11 // The name of the variable to be mutated
12 vname string
13
14 // The position of the variable to be mutated
15 vpos int
16
17 // The function that performs the mutation
18 f MutateFunc
19 }
20
21 // Mutate returns a Dstream in which the variable with the given name
22 // is transformed using the given function.
23 func Mutate(ds Dstream, name string, f MutateFunc) Dstream {
24
25 // Find the variable's position
26 vpos := -1
27 for j, n := range ds.Names() {
28 if n == name {
29 vpos = j
30 break
31 }
32 }
33 if vpos == -1 {
34 msg := fmt.Sprintf("Mutate: variable '%s' not found", name)
35 panic(msg)
36 }
37
38 m := &mutated{
39 xform: xform{
40 source: ds,
41 },
42 vname: name,
43 vpos: vpos,
44 f: f,
45 }
46
47 return m
48 }
49
50 func (m *mutated) Next() bool {
51 if !m.source.Next() {
52 return false
53 }
54
55 // Call the mutating function on the variable to be
56 // transformed.
57 m.f(m.GetPos(m.vpos))
58
59 return true
60 }