-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #279 from rudderlabs/nisshar/VariadicFunctions
Adding support for variadic functions.
- Loading branch information
Showing
6 changed files
with
160 additions
and
41 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Copyright 2018 The go-python Authors. All rights reserved. | ||
# Use of this source code is governed by a BSD-style | ||
# license that can be found in the LICENSE file. | ||
import variadic, go | ||
|
||
############### Non Variadic ############## | ||
nonvarResult = variadic.NonVariFunc(1, go.Slice_int([2,3,4]),5) | ||
print("NonVariadic 1+[2+3+4]+5 = %d" % nonvarResult) | ||
|
||
############### Variadic Over Int ############## | ||
varResult = variadic.VariFunc(1,2,3,4,5) | ||
print("Variadic 1+2+3+4+5 = %d" % varResult) | ||
|
||
############### Variadic Over Struct ############## | ||
varStructResult = variadic.VariStructFunc(variadic.NewIntStrUct(1), variadic.NewIntStrUct(2), variadic.NewIntStrUct(3)) | ||
print("Variadic Struct s(1)+s(2)+s(3) = %d" % varStructResult) | ||
|
||
############### Variadic Over InterFace ############## | ||
varInterFaceResult = variadic.VariInterFaceFunc(variadic.NewIntStrUct(1), variadic.NewIntStrUct(2), variadic.NewIntStrUct(3)) | ||
print("Variadic InterFace i(1)+i(2)+i(3) = %d" % varInterFaceResult) | ||
|
||
############### Final ############## | ||
if isinstance(varResult, int): | ||
print("Type OK") | ||
else: | ||
print("Type Not OK") |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package variadic | ||
|
||
/////////////// Non Variadic ////////////// | ||
func NonVariFunc(arg1 int, arg2 []int, arg3 int) int{ | ||
total := arg1 | ||
for _, num := range arg2 { | ||
total += num | ||
} | ||
total += arg3 | ||
|
||
return total | ||
} | ||
|
||
/////////////// Variadic Over Int ////////////// | ||
func VariFunc(vargs ...int) int{ | ||
total := 0 | ||
for _, num := range vargs { | ||
total += num | ||
} | ||
return total | ||
} | ||
|
||
/////////////// Variadic Over Struct ////////////// | ||
type IntStrUct struct { | ||
p int | ||
} | ||
|
||
func NewIntStrUct(n int) IntStrUct { | ||
return IntStrUct { | ||
p:n, | ||
} | ||
} | ||
|
||
func VariStructFunc(vargs ...IntStrUct) int{ | ||
total := 0 | ||
for _, inst := range vargs { | ||
total += inst.p | ||
} | ||
return total | ||
} | ||
|
||
/////////////// Variadic Over Interface ////////////// | ||
type IntInterFace interface { | ||
Number() int | ||
} | ||
|
||
func (is *IntStrUct) Number() int { | ||
return is.p | ||
} | ||
|
||
func VariInterFaceFunc(vargs ...IntInterFace) int{ | ||
total := 0 | ||
for _, inst := range vargs { | ||
total += inst.Number() | ||
} | ||
return total | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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