diff options
| author | Michael Manring <michael@manring> | 2022-07-04 15:42:25 +0700 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2022-07-04 15:42:25 +0700 |
| commit | 274c0106e82d4a18a070f154ca916f40dda869e3 (patch) | |
| tree | 63662d63e2f5456dd09e1ae5f352d5890234b54c | |
| parent | ff063088d69c2428b38e56e7aa6a4771d0df1b80 (diff) | |
| download | perlweeklychallenge-club-274c0106e82d4a18a070f154ca916f40dda869e3.tar.gz perlweeklychallenge-club-274c0106e82d4a18a070f154ca916f40dda869e3.tar.bz2 perlweeklychallenge-club-274c0106e82d4a18a070f154ca916f40dda869e3.zip | |
pwc171 in go - task#2 use switch to handle fuctions passed
| -rw-r--r-- | challenge-171/pokgopun/go/ch-2.go | 21 |
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) { |
