Skip to content

Commit 395d92a

Browse files
Merge pull request #80 from logic-building/PMapPtr-UserDefinedType
Auto-generated code has pointer version of function - PMap
2 parents 0c31258 + 646e809 commit 395d92a

File tree

5 files changed

+239
-0
lines changed

5 files changed

+239
-0
lines changed

gofp/gofp.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ func generateFPCode(pkg, dataTypes, imports string) (string, error) {
219219
template += template2.Pmap()
220220
template = r.Replace(template)
221221

222+
template += template2.PmapPtr()
223+
template = r.Replace(template)
224+
222225
template += template2.FilterMap()
223226
template = r.Replace(template)
224227

internal/employee/fp.go

Lines changed: 66 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/employer/fp.go

Lines changed: 66 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/gfp/fp.go

Lines changed: 66 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/template/pmap.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,41 @@ func PMap<CONDITIONAL_TYPE>(f func(<TYPE>) <TYPE>, list []<TYPE>) []<TYPE> {
3535
}
3636
`
3737
}
38+
39+
// PMapPtr is template to generate function(Pmap) for user defined data type
40+
func PmapPtr() string {
41+
return `
42+
// PMap<CONDITIONAL_TYPE>Ptr applies the function(1st argument) on each item of the list and returns new list.
43+
// Run in parallel. no_of_goroutines = no_of_items_in_list
44+
func PMap<CONDITIONAL_TYPE>Ptr(f func(*<TYPE>) *<TYPE>, list []*<TYPE>) []*<TYPE> {
45+
if f == nil {
46+
return []*<TYPE>{}
47+
}
48+
49+
ch := make(chan map[int]*<TYPE>)
50+
var wg sync.WaitGroup
51+
52+
for i, v := range list {
53+
wg.Add(1)
54+
55+
go func(wg *sync.WaitGroup, ch chan map[int]*<TYPE>, i int, v *<TYPE>) {
56+
defer wg.Done()
57+
ch <- map[int]*<TYPE>{i: f(v)}
58+
}(&wg, ch, i, v)
59+
}
60+
61+
go func() {
62+
wg.Wait()
63+
close(ch)
64+
}()
65+
66+
newList := make([]*<TYPE>, len(list))
67+
for m := range ch {
68+
for k, v := range m {
69+
newList[k] = v
70+
}
71+
}
72+
return newList
73+
}
74+
`
75+
}

0 commit comments

Comments
 (0)