@@ -142,7 +142,7 @@ go get github.com/goforj/collection
142142| ** Serialization** | [ ToJSON] ( #tojson ) [ ToPrettyJSON] ( #toprettyjson ) |
143143| ** Set Operations** | [ Difference] ( #difference ) [ Intersect] ( #intersect ) [ SymmetricDifference] ( #symmetricdifference ) [ Union] ( #union ) [ Unique] ( #unique ) [ UniqueBy] ( #uniqueby ) |
144144| ** Slicing** | [ Chunk] ( #chunk ) [ Filter] ( #filter ) [ Partition] ( #partition ) [ Pop] ( #pop ) [ PopN] ( #popn ) [ Skip] ( #skip ) [ SkipLast] ( #skiplast ) [ Take] ( #take ) [ TakeLast] ( #takelast ) [ TakeUntil] ( #takeuntil ) [ TakeUntilFn] ( #takeuntilfn ) |
145- | ** Transformation** | [ Append] ( #append ) [ Concat] ( #concat ) [ Each] ( #each ) [ Map] ( #map ) [ MapTo] ( #mapto ) [ Merge] ( #merge ) [ Multiply] ( #multiply ) [ Pipe] ( #pipe ) [ Pluck] ( #pluck ) [ Prepend] ( #prepend ) [ Push] ( #push ) [ Tap] ( #tap ) [ Times] ( #times ) [ Transform] ( #transform ) |
145+ | ** Transformation** | [ Append] ( #append ) [ Concat] ( #concat ) [ Each] ( #each ) [ Map] ( #map ) [ MapTo] ( #mapto ) [ Merge] ( #merge ) [ Multiply] ( #multiply ) [ Pipe] ( #pipe ) [ Pluck] ( #pluck ) [ Prepend] ( #prepend ) [ Push] ( #push ) [ Tap] ( #tap ) [ Times] ( #times ) [ Transform] ( #transform ) [ Zip ] ( #zip ) [ ZipWith ] ( #zipwith ) |
146146
147147
148148## Access
@@ -3936,4 +3936,127 @@ collection.Dump(c3.Items())
39363936// 1 => {ID:2 Name:"BOB"} #collection.User
39373937// ]
39383938```
3939+
3940+ ### <a id =" zip " ></a >Zip · immutable
3941+
3942+ Zip combines two collections element-wise into a collection of tuples.
3943+ The resulting length is the smaller of the two inputs.
3944+
3945+ _ Example: integers and strings_
3946+
3947+ ``` go
3948+ nums := collection.New ([]int {1 , 2 , 3 })
3949+ words := collection.New ([]string {" one" , " two" })
3950+
3951+ out := collection.Zip (nums, words)
3952+ collection.Dump (out.Items ())
3953+ // #[]collection.Tuple[int,string] [
3954+ // 0 => #collection.Tuple[int,string] {
3955+ // +First => 1 #int
3956+ // +Second => "one" #string
3957+ // }
3958+ // 1 => #collection.Tuple[int,string] {
3959+ // +First => 2 #int
3960+ // +Second => "two" #string
3961+ // }
3962+ // ]
3963+ ```
3964+
3965+ _ Example: structs_
3966+
3967+ ``` go
3968+ type User struct {
3969+ ID int
3970+ Name string
3971+ }
3972+
3973+ users := collection.New ([]User {
3974+ {ID: 1 , Name: " Alice" },
3975+ {ID: 2 , Name: " Bob" },
3976+ })
3977+
3978+ roles := collection.New ([]string {" admin" , " user" , " extra" })
3979+
3980+ out2 := collection.Zip (users, roles)
3981+ collection.Dump (out2.Items ())
3982+ // #[]collection.Tuple[main.User,string] [
3983+ // 0 => #collection.Tuple[main.User,string] {
3984+ // +First => #main.User {
3985+ // +ID => 1 #int
3986+ // +Name => "Alice" #string
3987+ // }
3988+ // +Second => "admin" #string
3989+ // }
3990+ // 1 => #collection.Tuple[main.User,string] {
3991+ // +First => #main.User {
3992+ // +ID => 2 #int
3993+ // +Name => "Bob" #string
3994+ // }
3995+ // +Second => "user" #string
3996+ // }
3997+ // ]
3998+ ```
3999+
4000+ ### <a id =" zipwith " ></a >ZipWith · immutable
4001+
4002+ ZipWith combines two collections element-wise using combiner fn.
4003+ The resulting length is the smaller of the two inputs.
4004+
4005+ _ Example: sum ints_
4006+
4007+ ``` go
4008+ a := collection.New ([]int {1 , 2 , 3 })
4009+ b := collection.New ([]int {10 , 20 })
4010+
4011+ out := collection.ZipWith (a, b, func (x, y int ) int {
4012+ return x + y
4013+ })
4014+
4015+ collection.Dump (out.Items ())
4016+ // #[]int [
4017+ // 0 => 11 #int
4018+ // 1 => 22 #int
4019+ // ]
4020+ ```
4021+
4022+ _ Example: format strings_
4023+
4024+ ``` go
4025+ names := collection.New ([]string {" alice" , " bob" })
4026+ roles := collection.New ([]string {" admin" , " user" , " extra" })
4027+
4028+ out2 := collection.ZipWith (names, roles, func (name, role string ) string {
4029+ return name + " :" + role
4030+ })
4031+
4032+ collection.Dump (out2.Items ())
4033+ // #[]string [
4034+ // 0 => "alice:admin" #string
4035+ // 1 => "bob:user" #string
4036+ // ]
4037+ ```
4038+
4039+ _ Example: structs_
4040+
4041+ ``` go
4042+ type User struct {
4043+ Name string
4044+ }
4045+
4046+ type Role struct {
4047+ Title string
4048+ }
4049+
4050+ users := collection.New ([]User {{Name: " Alice" }, {Name: " Bob" }})
4051+ roles2 := collection.New ([]Role {{Title: " admin" }})
4052+
4053+ out3 := collection.ZipWith (users, roles2, func (u User , r Role ) string {
4054+ return u.Name + " -> " + r.Title
4055+ })
4056+
4057+ collection.Dump (out3.Items ())
4058+ // #[]string [
4059+ // 0 => "Alice -> admin" #string
4060+ // ]
4061+ ```
39394062<!-- api:embed:end -->
0 commit comments