Skip to content

Commit 7079ddc

Browse files
committed
Initial commit
0 parents  commit 7079ddc

File tree

6 files changed

+252
-0
lines changed

6 files changed

+252
-0
lines changed

.gitignore

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
### Go ###
2+
# Binaries for programs and plugins
3+
*.exe
4+
*.exe~
5+
*.dll
6+
*.so
7+
*.dylib
8+
9+
# Test binary, built with `go test -c`
10+
*.test
11+
12+
# Output of the go coverage tool, specifically when used with LiteIDE
13+
*.out
14+
15+
# Dependency directories (remove the comment below to include it)
16+
# vendor/
17+
18+
### Go Patch ###
19+
/vendor/
20+
/Godeps/
21+
22+
23+
24+
### Intellij ###
25+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
26+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
27+
.idea
28+
29+
# User-specific stuff
30+
.idea/**/workspace.xml
31+
.idea/**/tasks.xml
32+
.idea/**/usage.statistics.xml
33+
.idea/**/dictionaries
34+
.idea/**/shelf
35+
36+
# Generated files
37+
.idea/**/contentModel.xml
38+
39+
# Sensitive or high-churn files
40+
.idea/**/dataSources/
41+
.idea/**/dataSources.ids
42+
.idea/**/dataSources.local.xml
43+
.idea/**/sqlDataSources.xml
44+
.idea/**/dynamic.xml
45+
.idea/**/uiDesigner.xml
46+
.idea/**/dbnavigator.xml
47+
48+
# Gradle
49+
.idea/**/gradle.xml
50+
.idea/**/libraries
51+
52+
# Gradle and Maven with auto-import
53+
# When using Gradle or Maven with auto-import, you should exclude module files,
54+
# since they will be recreated, and may cause churn. Uncomment if using
55+
# auto-import.
56+
# .idea/modules.xml
57+
# .idea/*.iml
58+
# .idea/modules
59+
# *.iml
60+
# *.ipr
61+
62+
# CMake
63+
cmake-build-*/
64+
65+
# Mongo Explorer plugin
66+
.idea/**/mongoSettings.xml
67+
68+
# File-based project format
69+
*.iws
70+
71+
# IntelliJ
72+
out/
73+
74+
# mpeltonen/sbt-idea plugin
75+
.idea_modules/
76+
77+
# JIRA plugin
78+
atlassian-ide-plugin.xml
79+
80+
# Cursive Clojure plugin
81+
.idea/replstate.xml
82+
83+
# Crashlytics plugin (for Android Studio and IntelliJ)
84+
com_crashlytics_export_strings.xml
85+
crashlytics.properties
86+
crashlytics-build.properties
87+
fabric.properties
88+
89+
# Editor-based Rest Client
90+
.idea/httpRequests
91+
92+
# Android studio 3.1+ serialized cache file
93+
.idea/caches/build_file_checksums.ser
94+
95+
### Intellij Patch ###
96+
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
97+
98+
# *.iml
99+
# modules.xml
100+
# .idea/misc.xml
101+
# *.ipr
102+
103+
# Sonarlint plugin
104+
.idea/**/sonarlint/
105+
106+
# SonarQube Plugin
107+
.idea/**/sonarIssues.xml
108+
109+
# Markdown Navigator plugin
110+
.idea/**/markdown-navigator.xml
111+
.idea/**/markdown-navigator/
112+

README.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Godash
2+
3+
Inspired from [Lodash](https://github.com/lodash/lodash) for golang
4+
5+
# Functions
6+
7+
## Map
8+
9+
Map applies a mapper function on each element of an input and sets it in output.
10+
11+
_Primitive types_
12+
13+
```go
14+
func main() {
15+
input := []int{1, 2, 3, 4, 5}
16+
output := make([]int, 0)
17+
18+
_ = godash.Map(input, &output, func(el int) int {
19+
return el * el
20+
})
21+
22+
fmt.Println(output) // prints 1 4 9 16 25
23+
}
24+
```
25+
26+
_Struct type_
27+
28+
```go
29+
type Person struct {
30+
Name string
31+
Age Int
32+
}
33+
34+
func main() {
35+
input := []Person{
36+
{Name: "John", Age: 22},
37+
{Name: "Doe", Age: 23},
38+
}
39+
output := make([]string, 0)
40+
41+
_ = godash.Map(input, &output, func(person Person) string {
42+
return person.Name
43+
})
44+
45+
fmt.Println(output) // prints John Doe
46+
}
47+
```

go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/thecasualcoder/godash
2+
3+
go 1.13
4+
5+
require github.com/stretchr/testify v1.4.0

go.sum

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6+
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
7+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
9+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
10+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
11+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

map.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package godash
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
)
7+
8+
// Map applies mapperFn on each item of in and puts it in out
9+
func Map(in, out, mapperFn interface{}) error {
10+
mapper := reflect.ValueOf(mapperFn)
11+
if mapper.Kind() != reflect.Func {
12+
return fmt.Errorf("mapperFn has to be a function")
13+
}
14+
input := reflect.ValueOf(in)
15+
output := reflect.ValueOf(out)
16+
if !output.Elem().CanSet() {
17+
return fmt.Errorf("cannot set out. Pass a reference to set output")
18+
}
19+
20+
if input.Kind() == reflect.Slice {
21+
result := reflect.MakeSlice(output.Elem().Type(), 0, input.Len())
22+
for i := 0; i < input.Len(); i++ {
23+
arg := input.Index(i)
24+
25+
returnValues := mapper.Call([]reflect.Value{arg})
26+
27+
result = reflect.Append(result, returnValues[0])
28+
}
29+
output.Elem().Set(result)
30+
31+
return nil
32+
}
33+
return fmt.Errorf("not implemented")
34+
}

map_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package godash_test
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"github.com/thecasualcoder/godash"
6+
"testing"
7+
)
8+
9+
func TestMap(t *testing.T) {
10+
t.Run("support primitive types", func(t *testing.T) {
11+
in := []int{1, 2, 3}
12+
out := make([]int, 0)
13+
14+
err := godash.Map(in, &out, func(element int) int {
15+
return element * element
16+
})
17+
18+
expected := []int{1, 4, 9}
19+
assert.NoError(t, err)
20+
assert.Equal(t, expected, out)
21+
})
22+
23+
t.Run("support structs", func(t *testing.T) {
24+
type person struct {
25+
name string
26+
age int
27+
}
28+
29+
in := []person{
30+
{name: "john", age: 20},
31+
{name: "doe", age: 23},
32+
}
33+
out := make([]string, 0)
34+
expected := []string{"john", "doe"}
35+
36+
err := godash.Map(in, &out, func(p person) string {
37+
return p.name
38+
})
39+
40+
assert.NoError(t, err)
41+
assert.Equal(t, expected, out)
42+
})
43+
}

0 commit comments

Comments
 (0)