aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-07-04 21:14:02 +0100
committerGitHub <noreply@github.com>2022-07-04 21:14:02 +0100
commit5e7c1ffacc5e1097d3585ba5873c48950f7a2523 (patch)
tree18717047e7a2269edaf2be7badb53fff1314e407
parentdd596a0ad89f6f3bd110730db92e32f67be47214 (diff)
parent274c0106e82d4a18a070f154ca916f40dda869e3 (diff)
downloadperlweeklychallenge-club-5e7c1ffacc5e1097d3585ba5873c48950f7a2523.tar.gz
perlweeklychallenge-club-5e7c1ffacc5e1097d3585ba5873c48950f7a2523.tar.bz2
perlweeklychallenge-club-5e7c1ffacc5e1097d3585ba5873c48950f7a2523.zip
Merge pull request #6390 from pokgopun/pwc171
pwc171 in go - task#2 use switch to handle fuctions passed
-rw-r--r--challenge-171/pokgopun/go/ch-2.go21
1 files changed, 14 insertions, 7 deletions
diff --git a/challenge-171/pokgopun/go/ch-2.go b/challenge-171/pokgopun/go/ch-2.go
index 077f784927..4621e92334 100644
--- a/challenge-171/pokgopun/go/ch-2.go
+++ b/challenge-171/pokgopun/go/ch-2.go
@@ -1,11 +1,14 @@
/* https://theweeklychallenge.org/blog/perl-weekly-challenge-171/
Task 2: First-class Function
-Submitted by: Mohammad S Anwar
-Create sub compose($f, $g) which takes in two parameters $f and $g as subroutine refs and returns subroutine ref i.e. compose($f, $g)->($x) = $f->($g->($x))
+Submitted by: [52]Mohammad S Anwar
+ __________________________________________________________________
-e.g.
+ Create sub compose($f, $g) which takes in two parameters $f and $g as
+ subroutine refs and returns subroutine ref i.e. compose($f, $g)->($x) =
+ $f->($g->($x))
+ e.g.
$f = (one or more parameters function)
$g = (one or more parameters function)
@@ -35,13 +38,17 @@ func main() {
fmt.Println("\n")
}
func Compose(f ...func([]uint) []uint) func([]uint) []uint {
- if len(f) < 1 {
+ switch len(f) {
+ case 0:
return func(s []uint) []uint {
return s
}
- }
- return func(s []uint) []uint {
- return f[0](Compose(f[1:]...)(s))
+ case 1:
+ return f[0]
+ default:
+ return func(s []uint) []uint {
+ return f[0](Compose(f[1:]...)(s))
+ }
}
}
func Square(s []uint) (r []uint) {