Skip to content

write code in xmap with kimi-k2-0711-preview#9

Merged
dashjay merged 2 commits intomainfrom
feat/write-code-with-moonshot-ai
Aug 8, 2025
Merged

write code in xmap with kimi-k2-0711-preview#9
dashjay merged 2 commits intomainfrom
feat/write-code-with-moonshot-ai

Conversation

@dashjay
Copy link
Owner

@dashjay dashjay commented Aug 8, 2025

Summary:

AI did a good job on this kind of codes
But kimi-k2-0711-preview made a mistake is that map in golang is unorderd
so the testing function generated will not pass

       136 +      // Test with duplicate key generation (last one wins)
       137 +      m2 := map[int]string{1: "a", 2: "b", 3: "c"}
       138 +      fn2 := func(k int, v string) int {
       139 +        return k % 2 // This will create duplicate keys: 1%2=1, 2%2=0, 3%2=1
       140 +      }
       141 +      result2 := xmap.MapKeys(m2, fn2)
       142 +      // Should have keys 0 and 1, with values from the last occurrence of each key
       143 +      expected2 := map[int]string{0: "b", 1: "c"}
       144 +      assert.Equal(t, expected2, result2)

Tips for getting started:

  1. Run /init to create a CLAUDE.md file with instructions for Claude
  2. Run /terminal-setup to set up terminal integration
  3. Use Claude to help with file analysis, editing, bash commands and git
  4. Be as specific as you would with another engineer for the best results

write a new function like MapValues[K comparable, V1, V2 any] in pkg/xmap/xmap_common.go but name MapKeys[K comparable, V1](in map[K]V1, fn func(K, V1) K)
map[K]V1 which do things like MapValues but on keys, and generate a test in pkg/xmap/xmap_test.go

⏺ I'll implement the MapKeys function and add tests for it. Let me start by examining the existing code to understand the pattern used in MapValues.

⏺ Read(pkg/xmap/xmap_common.go)
⎿  Read 75 lines (ctrl+r to expand)

⏺ Now let me look at the test file to understand the testing pattern:

⏺ Read(pkg/xmap/xmap_test.go)
⎿  Read 114 lines (ctrl+r to expand)

⏺ Now I'll implement the MapKeys function based on the existing MapValues pattern, and add tests for it.

⏺ Update(pkg/xmap/xmap_common.go)

╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Edit file │
│ ╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │
│ │ pkg/xmap/xmap_common.go │ │
│ │ │ │
│ │ 72 func MapValues[K comparable, V1, V2 any](in map[K]V1, fn func(K, V1) V2) │ │

⏺ Update(pkg/xmap/xmap_common.go)

╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Edit file │
│ ╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │
│ │ pkg/xmap/xmap_common.go │ │
│ │ │ │
│ │ 72 func MapValues[K comparable, V1, V2 any](in map[K]V1, fn func(K, V1) V2) map[K]V2 { │ │

⏺ Update(pkg/xmap/xmap_common.go)
⎿  Updated pkg/xmap/xmap_common.go with 24 additions
72 func MapValues[K comparable, V1, V2 any](in map[K]V1, fn func(K, V1) V2) map[K]V2 {
73 return xiter.ToMap(xiter.Map2(func(k K, v V1) (K, V2) { return k, fn(k, v) }, xiter.FromMapKeyAndValues(in)))
74 }
75 +
76 + // MapKeys transforms the keys of a map using the provided function while keeping values unchanged.
77 + // This is useful for transforming data structures while preserving the value associations.
78 + //
79 + // Parameters:
80 + //
81 + // in map[K]V1: The input map to transform
82 + // fn func(K, V1) K: A function that takes a key and its corresponding value, and returns a new key
83 + //
84 + // Returns:
85 + //
86 + // map[K]V1: A new map with the same values as the input map but with transformed keys
87 + //
88 + // Example:
89 + //
90 + // m := map[string]int{"a": 1, "b": 2, "c": 3}
91 + // fn := func(k string, v int) string {
92 + // return k + "_key"
93 + // }
94 + // result := MapKeys(m, fn)
95 + // // result will be map[string]int{"a_key": 1, "b_key": 2, "c_key": 3}
96 + func MapKeys[K comparable, V1 any](in map[K]V1, fn func(K, V1) K) map[K]V1 {
97 + return xiter.ToMap(xiter.Map2(func(k K, v V1) (K, V1) { return fn(k, v), v }, xiter.FromMapKeyAndValues(in)))
98 + }

⏺ Now I'll add tests for the new MapKeys function:

⏺ Update(pkg/xmap/xmap_test.go)
⎿  User rejected update to pkg/xmap/xmap_test.go
110 emptyResult := xmap.MapValues(emptyMap, fn)
111 assert.Equal(t, map[string]string{}, emptyResult)
112 })
113 +
114 + t.Run("map keys", func(t *testing.T) {
115 + m := map[string]int{"a": 1, "b": 2, "c": 3}
116 + fn := func(k string, v int) string {
117 + return k + "key"
118 + }
119 + var result = xmap.MapKeys(m, fn)
120 + expected := map[string]int{"a_key": 1, "b_key": 2, "c_key": 3}
121 + assert.Equal(t, expected, result)
122 +
123 + // Test with value usage in transformation
124 + fnWithValue := func(k string, v int) string {
125 + return fmt.Sprintf("%s
%d", k, v)
126 + }
127 + resultWithValue := xmap.MapKeys(m, fnWithValue)
128 + expectedWithValue := map[string]int{"a_1": 1, "b_2": 2, "c_3": 3}
129 + assert.Equal(t, expectedWithValue, resultWithValue)
130 +
131 + // Test with empty map
132 + emptyMap := map[string]int{}
133 + emptyResult := xmap.MapKeys(emptyMap, fn)
134 + assert.Equal(t, map[string]int{}, emptyResult)
135 +
136 + // Test with duplicate key generation (last one wins)
137 + m2 := map[int]string{1: "a", 2: "b", 3: "c"}
138 + fn2 := func(k int, v string) int {
139 + return k % 2 // This will create duplicate keys: 1%2=1, 2%2=0, 3%2=1
140 + }
141 + result2 := xmap.MapKeys(m2, fn2)
142 + // Should have keys 0 and 1, with values from the last occurrence of each key
143 + expected2 := map[int]string{0: "b", 1: "c"}
144 + assert.Equal(t, expected2, result2)
145 + })
146 }

@codecov
Copy link

codecov bot commented Aug 8, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.55%. Comparing base (6cacab3) to head (43a6ccc).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main       #9   +/-   ##
=======================================
  Coverage   99.55%   99.55%           
=======================================
  Files          15       15           
  Lines        1114     1118    +4     
=======================================
+ Hits         1109     1113    +4     
  Misses          3        3           
  Partials        2        2           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dashjay dashjay merged commit 00256a3 into main Aug 8, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant