5 kyu

Lazy Repeater

5,685 of 8,853nklein

Description:

The makeLooper() function (or make_looper in your language) takes a string (of non-zero length) as an argument. It returns a function. The function it returns will return successive characters of the string on successive invocations. It will start back at the beginning of the string once it reaches the end.

For example:

const abc = makeLooper('abc');
abc(); // should return 'a' on this first call
abc(); // should return 'b' on this second call
abc(); // should return 'c' on this third call
abc(); // should return 'a' again on this fourth call
abc = makeLooper 'abc'
abc() # should return 'a' on this first call
abc() # should return 'b' on this second call
abc() # should return 'c' on this third call
abc() # should return 'a' again on this fourth call
Func<char> abc = Kata.MakeLooper("abc");
abc(); // should return 'a' on this first call
abc(); // should return 'b' on this second call
abc(); // should return 'c' on this third call
abc(); // should return 'a' again on this fourth call
abc = make_looper('abc')
abc() # should return 'a' on this first call
abc() # should return 'b' on this second call
abc() # should return 'c' on this third call
abc() # should return 'a' again on this fourth call
let mut abc = make_looper("abc");
abc(); // should return 'a' on this first call
abc(); // should return 'b' on this second call
abc(); // should return 'c' on this third call
abc(); // should return 'a' again on this fourth call
auto abc = makeLooper("abc");
abc(); // should return 'a' on this first call
abc(); // should return 'b' on this second call
abc(); // should return 'c' on this third call
abc(); // should return 'a' again on this fourth call
local abc = make_looper "abc"
abc() -- should return "a" on this first call
abc() -- should return "b" on this second call
abc() -- should return "c" on this third call
abc() -- should return "a" again on this fourth call
>>> abc <- makeLooper "abc"
>>> abc
'a'
>>> abc
'b'
>>> abc
'c'
>>> abc
'a'
Supplier<Character> abc = Solution.makeLooper("abc");
abc.get(); // 'a'
abc.get(); // 'b'
abc.get(); // 'c'
abc.get(); // 'a' (loops back to the first character after reaching the end)
let abc = make_looper "abc"
abc () (* 'a' *)
abc () (* 'b' *)
abc () (* 'c' *)
abc () (* 'a' (loops back to the first character after reaching the end) *)

Different loopers should not affect each other, so be wary of unmanaged global state.

Iterators
Algorithms

Similar Kata:

Stats:

CreatedAug 2, 2013
PublishedAug 3, 2013
Warriors Trained15027
Total Skips1068
Total Code Submissions27487
Total Times Completed8853
JavaScript Completions5685
CoffeeScript Completions35
C# Completions541
Python Completions1634
Rust Completions181
Scala Completions59
TypeScript Completions600
C++ Completions322
Lua Completions14
Java Completions46
Haskell Completions6
OCaml Completions4
Total Stars208
% of votes with a positive feedback rating93% of 648
Total "Very Satisfied" Votes574
Total "Somewhat Satisfied" Votes61
Total "Not Satisfied" Votes13
Ad
Contributors
  • nklein Avatar
  • jhoffner Avatar
  • SagePtr Avatar
  • iamphill Avatar
  • suic Avatar
  • myjinxin2015 Avatar
  • JohanWiltink Avatar
  • Souzooka Avatar
  • FArekkusu Avatar
  • Awesome A.D. Avatar
  • hobovsky Avatar
  • cplir-c Avatar
  • trashy_incel Avatar
  • tobeannouncd Avatar
  • farhanaditya Avatar
  • yLaWy Avatar
  • o2001 Avatar
  • KayleighWasTaken Avatar
  • metatable Avatar
  • SummerTheCoder Avatar
Ad