Code Monkey home page Code Monkey logo

Comments (3)

xyctruth avatar xyctruth commented on May 30, 2024

是否可以通过 Reduce 接收的函数的第一个参数result 进行判断后初始化?

	stream.NewSlice([]int{1, 2, 3}).Reduce(func(result int, e int) int {
		if result == 0 {
			result = 100
		}
		return result + e
	})

from stream.

xjqxziii avatar xjqxziii commented on May 30, 2024

是否可以通过 Reduce 接收的函数的第一个参数result 进行判断后初始化?

	stream.NewSlice([]int{1, 2, 3}).Reduce(func(result int, e int) int {
		if result == 0 {
			result = 100
		}
		return result + e
	})

我想通过reduce生成一个map,然后发现需要这样写


		Reduce(func(result map[string]string, element string) map[string]string {
			if result == nil {
				return map[string]string{
					element: element,
				}
			}
			result[element] = element
			return result
		})

可以考虑增加一个类似这种的接口吗?这样就可以少初始化一次

func (stream SliceMappingStream[E, MapE, ReduceE]) Reduce2(result ReduceE, accumulator func(ReduceE, E) ReduceE) ReduceE {
	stream.evaluation()
	if len(stream.source) == 0 {
		return result
	}

	for _, v := range stream.source {
		result = accumulator(result, v)
	}
	return result
}

from stream.

xyctruth avatar xyctruth commented on May 30, 2024

是否可以通过 Reduce 接收的函数的第一个参数result 进行判断后初始化?

	stream.NewSlice([]int{1, 2, 3}).Reduce(func(result int, e int) int {
		if result == 0 {
			result = 100
		}
		return result + e
	})

我想通过reduce生成一个map,然后发现需要这样写


		Reduce(func(result map[string]string, element string) map[string]string {
			if result == nil {
				return map[string]string{
					element: element,
				}
			}
			result[element] = element
			return result
		})

可以考虑增加一个类似这种的接口吗?这样就可以少初始化一次

func (stream SliceMappingStream[E, MapE, ReduceE]) Reduce2(result ReduceE, accumulator func(ReduceE, E) ReduceE) ReduceE {
	stream.evaluation()
	if len(stream.source) == 0 {
		return result
	}

	for _, v := range stream.source {
		result = accumulator(result, v)
	}
	return result
}

ok,稍后会在 Reduce 函数直接增加初始值的参数。这样在迭代 Reduce 时可以少做一次判断

from stream.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.