github.com/tursom/GoCollections@v0.3.10/util/mr/MapReduce.go (about)

     1  package mr
     2  
     3  import "github.com/tursom/GoCollections/lang/atomic"
     4  
     5  type (
     6  	MapReduce[V, R any] interface {
     7  		Map(value V) R
     8  		Reduce(results <-chan R) R
     9  	}
    10  )
    11  
    12  func LocalMap[V, R any](values <-chan V, m func(value V) R) <-chan R {
    13  	rc := make(chan R)
    14  
    15  	go func() {
    16  		for value := range values {
    17  			rc <- m(value)
    18  		}
    19  
    20  		close(rc)
    21  	}()
    22  
    23  	return rc
    24  }
    25  
    26  func LocalReduce[R any](values <-chan R, r func(results <-chan R) R) R {
    27  	return r(values)
    28  }
    29  
    30  func Local[V, R any](values <-chan V, mr MapReduce[V, R]) R {
    31  	return LocalReduce(LocalMap(values, mr.Map), mr.Reduce)
    32  }
    33  
    34  func MultiMap[V, R any](values <-chan V, m func(value V) R) <-chan R {
    35  	rc := make(chan R)
    36  
    37  	c := atomic.Int32(1)
    38  	for value0 := range values {
    39  		c.Add(1)
    40  		value := value0
    41  		go func() {
    42  			rc <- m(value)
    43  
    44  			if c.Add(-1) == 0 {
    45  				close(rc)
    46  			}
    47  		}()
    48  	}
    49  	if c.Add(-1) == 0 {
    50  		close(rc)
    51  	}
    52  
    53  	return rc
    54  }
    55  
    56  func MultiReduce[R any](values <-chan R, r func(results <-chan R) R) R {
    57  	rc := make(chan R)
    58  	go func() {
    59  		rc <- r(values)
    60  	}()
    61  
    62  	return <-rc
    63  }
    64  
    65  func Multi[V, R any](values <-chan V, mr MapReduce[V, R]) R {
    66  	return MultiReduce(MultiMap(values, mr.Map), mr.Reduce)
    67  }