Code Monkey home page Code Monkey logo

Comments (1)

Primetalk avatar Primetalk commented on July 29, 2024

Hey, @wayddmldbzz,
Thank you for your interest in this framework. Your stream-based Java-example looks nice.
The Java DSL uses an important language feature. Stream interface approximately looks like this:

interface Stream<T> {
  <R> map(Function<T, R>)
  ...
}

Unfortunately, to my knowledge Go does not support this kind of interfaces.
The only similar feature that I was able to find was to use plain functions that can take generic parameters:

func Map[T, R](Stream[T], func(T)R)

Hence, the nice Java-style DSL seems to be infeasible in the current version of Go.

Your code looks almost right. In order to make it nicer, I would make a small refactoring:

func isEnabled(h KVBase) bool {
  return h.Enabled
}

func toKeyValuePair(h KVBase) fun.Pair[string, string] {
  return fun.NewPair(h.Key, h.Value)
}

func Foo() {
        sort.Slice(headers, func(i, j int) bool {
		return headers[i].Index < headers[j].Index
	})
        headersStream := stream.FromSlice(headers)
	enabled := stream.Filter(headersStream, isEnabled)
	keyValues := stream.Map(enabled, toKeyValuePair)
	m := make(map[string]string)
	preserveKeyValue := func(f fun.Pair[string, string]) {
		m[f.V1] = f.V2
	}
	delayedStreamExecution := stream.ForEach(keyValues, preserveKeyValue)
        _, err := io.UnsafeRunSync(delayedStreamExecution)
       ...
}

Note that stream is lazy and needs UnsafeRunSync at the end.

from goio.

Related Issues (20)

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.