Comments (1)
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)
- Add fun.EqualTo()()
- Add slice.Partition
- Add pair.Both
- Add io.LiftFunc
- Add io.MapSlice
- Add slice.Reduce
- Add Cancellable
- Add slice.BuildUniqueIndex
- PairBoth -> p.Both
- slice.Head, slice.Tail, slice.HeadTail
- slice.Remove, slice.Intersection, Union HOT 1
- Add slice.Intersperse
- Build on other platforms
- Move ExecutionContext to unsafe HOT 1
- Add slice.Reverse
- Add io.Memoize
- Add slice.Distinct, sets.ToSlice
- Pool test fails from time to time
- Example package HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from goio.