github.com/searKing/golang/go@v1.2.74/container/stream/op/reduce/op.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package reduce 6 7 import ( 8 "context" 9 10 "github.com/searKing/golang/go/container/stream/op/terminal" 11 "github.com/searKing/golang/go/util/function/binary" 12 "github.com/searKing/golang/go/util/object" 13 "github.com/searKing/golang/go/util/optional" 14 "github.com/searKing/golang/go/util/spliterator" 15 ) 16 17 type ReduceOp struct { 18 terminal.TODOOperation 19 20 sinkNewer func() *ReducingSink 21 } 22 23 func NewReduceOp(sinkNewer func() *ReducingSink) *ReduceOp { 24 reduceOp := &ReduceOp{ 25 sinkNewer: sinkNewer, 26 } 27 reduceOp.SetDerived(reduceOp) 28 return reduceOp 29 } 30 31 func NewReduceOp3(seed optional.Optional, reducer binary.BiFunction, combiner binary.BiFunction) terminal.Operation { 32 object.RequireNonNil(reducer) 33 return NewReduceOp(func() *ReducingSink { 34 return NewReducingSink(seed, reducer, combiner) 35 }) 36 } 37 38 func (op ReduceOp) MakeSink() *ReducingSink { 39 object.RequireNonNull(op.sinkNewer) 40 return op.sinkNewer() 41 } 42 43 func (op ReduceOp) EvaluateParallel(ctx context.Context, spliterator spliterator.Spliterator) optional.Optional { 44 return terminal.WrapAndCopyInto(ctx, op.MakeSink(), spliterator).Get() 45 } 46 47 func (op ReduceOp) EvaluateSequential(ctx context.Context, spliterator spliterator.Spliterator) optional.Optional { 48 return NewReduceTask(op, spliterator).Invoke(ctx).Get() 49 }