Monday, February 27, 2023
HomeSoftware EngineeringThe right way to carry out Perform Iteration in Golang

The right way to carry out Perform Iteration in Golang


The problem

The aim of this problem is to put in writing a higher-order operate returning a brand new operate that iterates on a specified operate a given variety of occasions. This new operate takes in an argument as a seed to start out the computation.

For example, take into account the operateĀ getDouble. When run twice on worthĀ 3, yieldsĀ 12Ā as proven beneath.

getDouble(3) => 6
getDouble(6) => 12

Allow us to identify the brand new operateĀ createIteratorĀ and we should always be capable of acquire the identical outcome utilizingĀ createIteratorĀ as proven beneath:

var doubleIterator = createIterator(getDouble, 2); // Runs *getDouble* twice
doubleIterator(3) => 12

For the sake of simplicity, all operate inputs toĀ createIteratorĀ would operate returning a small quantity and the variety of iterations would at all times be integers.

The answer in Golang

Choice 1:

bundle resolution
func CreateIterator(fn func(int) int,n int) func(int) int {
  return func (i int) int {
    for j := 0; j < n; j++ { i = fn(i) }
    return i
  }
}

Choice 2:

bundle resolution
func CreateIterator(fn func(int) int,n int) func(int) int {
  if n == 0 {
    return func(x int) int { return x } // id
  } else {
    return func(x int) int { return CreateIterator(fn, n-1)(fn(x)) }
  }
}

Choice 3:

bundle resolution
func CreateIterator(fn func(int) int,n int) func(int) int {
    if n < 1 {
      return nil
    }
    var retFunc = func(r int) int {
      for i := n ; i>0 ; i-- {
        r = fn(r)
      }
      return r
    }
    return retFunc
}

Check instances to validate our resolution

bundle solution_test
import (
    . "github.com/onsi/ginkgo"
    . "github.com/onsi/gomega"
)
var _ = Describe("Iterator for 'getDouble' operate",func() {
    var getDouble = func(n int) int {return 2*n}
    It("Operating the iterator as soon as",func() {
        doubleIterator := CreateIterator(getDouble,1)
        Anticipate(doubleIterator(3)).To(Equal(6))
        Anticipate(doubleIterator(5)).To(Equal(10))
    })
    It("Operating the iterator twice",func() {
        getQuadruple := CreateIterator(getDouble,2)
        Anticipate(getQuadruple(2)).To(Equal(8))
        Anticipate(getQuadruple(5)).To(Equal(20))
    })
})
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments

situs slot gacor provider terbaik agen toto slot terpercaya 2023 agen toto togel terpercaya 2023 situs toto togel pasaran resmi terbaik bandar toto macau pasaran resmi toto togel bandar toto slot gacor 4d 2023 bo togel online pasaran terlengkap sepanjang masa bo toto slot terlengkap sepanjang masa situs toto togel 2023 bet 100 perak daftar toto slot dan toto togel 2023 bermain toto togel dengan bet hanya 100 perak daftar toto slot bonus new member terpercaya bermain toto slot pelayanan 24 jam nonstop agen slot gacor 4d hadiah terbesar bandar toto slot provider terbaik toto slot gacor 4d hingga toto togel toto togel pasaran resmi terpercaya bo togel online terbaik 2023 agen togel online terbesar 2023 situs togel online terpercaya 2023 bo togel online paling resmi 2023 toto togel pasaran togel hongkong resmi situs slot online pasti gacor agen slot online anti rungkad bo slot online deposit tanpa potongan situs toto togel dan toto slot bonus new member situs toto slot gacor 4d bo toto slot gacor 4d bo toto slot gacor dari toto togel 4d bo toto slot 4d terpercaya bo toto slot terpercaya toto macau resmi dari toto togel 4d agen togel terbesar dan situs toto slot terpercaya bandar toto togel dan slot online 2023 bo slot gacor terbaik sepanjang masa winsortoto winsortoto bo toto togel situs toto situs toto togel terpercaya situs toto slot terpercaya situs slot gacor 4d terbaik sepanjang masa agen toto togel dan situs toto slot terpercaya situs toto togel dan agen toto slot terpercaya bandar toto togel tersedia pasaran toto macau resmi agen toto togel bet 100 perak deposit 10rb ltdtoto