Last active
July 6, 2025 00:09
-
-
Save bbasata/ebf24ffa142746744e435b7ac37c7458 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "iter" | |
| "slices" | |
| "strconv" | |
| "example.org/int/typ" | |
| ) | |
| func main() { | |
| for item := range items { | |
| if item.StringValue() != nil { | |
| fmt.Println("Found a string value:", *item.StringValue()) | |
| if *item.StringValue() == "bat" { | |
| break | |
| } | |
| } | |
| } | |
| } | |
| func items(push func(typ.Variant) bool) { | |
| pullInt32, stopInt32 := iter.Pull(integers) | |
| pullString, stopString := iter.Pull(words) | |
| defer stopInt32() | |
| defer stopString() | |
| for { | |
| if nextInt32, ok := pullInt32(); ok { | |
| variant := typ.String(strconv.Itoa(int(nextInt32))) | |
| if !push(variant) { | |
| return | |
| } | |
| } | |
| if nextString, ok := pullString(); ok { | |
| variant := typ.String(nextString) | |
| if !push(variant) { | |
| return | |
| } | |
| } | |
| } | |
| } | |
| var integers iter.Seq[int32] = slices.Values([]int32{1, 2, 3_000, 4, 5}) | |
| var words iter.Seq[string] = slices.Values([]string{`ورد`, `hat`, `bat`, `cat`, `mat`}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package typ | |
| type Variant interface { | |
| StringValue() *string | |
| Int32Value() *int32 | |
| } | |
| type variant struct { | |
| stringValue *string | |
| int32Value *int32 | |
| } | |
| func String(value string) Variant { | |
| return variant{stringValue: &value} | |
| } | |
| func Int32(value int32) Variant { | |
| return variant{int32Value: &value} | |
| } | |
| func (t variant) StringValue() *string { | |
| return t.stringValue | |
| } | |
| func (t variant) Int32Value() *int32 { | |
| return t.int32Value | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment