Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add map function #1

Merged
merged 1 commit into from
Apr 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/apply/apply.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package apply

import (
"reflect"
)

type Type struct {
slice interface{}
}

// Lift Factory method to encapsulate the type within Type
func Lift(data interface{}) Type {
// check if this is a slice
s := reflect.ValueOf(data)
if s.Kind() != reflect.Slice {
panic("InterfaceSlice() given a non-slice type")
}
return Type{slice: data}
}

// Map
// panic if map function input and output dont have the same type
// panic if slice type and f input type is not the same type
func (t *Type) Apply(f interface{}) Type {
// make sure that this is a function
v := reflect.ValueOf(f)
if v.Kind() != reflect.Func {
panic("f should be a function")
}
// check the underlying type of the slice
fType := v.Type()
// check function take one parameter of type t and return one out put of the same type
if fType.NumOut() != 1 || fType.NumIn() != 1 {
panic("Function must take one input and produces one output")
}
// get th inbound param type
inV := fType.In(0)
inKind := inV.Kind()
// get outbound param type
outV := fType.Out(0)
outKind := outV.Kind()
// check types for input and output
if inKind != outKind {
panic("function input type != output type")
}
if reflect.TypeOf(t.slice).Elem().Kind() != inKind {
panic("slice type != map function input type")
}
// create empty slice based on the original slice
origSlice := reflect.ValueOf(t.slice)
newSlice := make([]interface{}, origSlice.Len())
// apply the method then append the item to the new slice
for i := 0; i < origSlice.Len(); i++ {
value := origSlice.Index(i)
newSlice[i] = v.Call([]reflect.Value{value})[0].Interface()
}
// return new mapped results
return Type{slice: newSlice}
}

func (t *Type) Get() interface{} {
return t.slice
}
1 change: 1 addition & 0 deletions src/apply/apply_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package apply_test
16 changes: 16 additions & 0 deletions src/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"fmt"

"github.com/sghaida/fp/src/apply"
)

func main() {
data := []int{1, 2, 3, 4, 5, 6, 7, 8}
lifted := apply.Lift(data)
addOne := func(item int) int { return item + 1 }
newData := lifted.Apply(addOne)
fmt.Println(newData.Get())
fmt.Println(data)
}