Skip to content

Commit

Permalink
Uses closures
Browse files Browse the repository at this point in the history
  • Loading branch information
elefevre committed Dec 1, 2011
1 parent 040ef33 commit 4c6665e
Showing 1 changed file with 7 additions and 20 deletions.
27 changes: 7 additions & 20 deletions FooBarQixFantom/fan/FooBarQix.fan
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,24 @@ class FooBarQix
{
static Void main(Str[] args) {
fooBarQix := FooBarQix {}
for (index := 1; index <= 100; ++index)
{
echo(fooBarQix.asText(index))
}
(1..100).each |Int c| { echo(fooBarQix.asText(c)) }
}

const Map digitsToStrings := [3:"Foo", 5:"Bar", 7:"Qix"]
Str asText(Int value)
{
result := asTextFromMultiple(value, 3, "Foo")
+ asTextFromMultiple(value, 5, "Bar")
+ asTextFromMultiple(value, 7, "Qix")
+ asTextFromDigit(value)

result := asTextFromMultiple(value) + asTextFromDigit(value)
return result == "" ? "" + value : result
}

private Str asTextFromMultiple(Int number, Int value, Str text)
private Str asTextFromMultiple(Int number)
{
return number % value == 0 ? text : ""
return digitsToStrings.keys.map |Int c -> Str?| { return number % c == 0 ? digitsToStrings[c] : "" }.join;
}

private Str asTextFromDigit(Int number)
{
text := ("" + number).chars
Str result := ""
for (index := 0; index < text.size; ++index)
{
if (text[index] == '3') result = result + "Foo"
if (text[index] == '5') result = result + "Bar"
if (text[index] == '7') result = result + "Qix"
}
return result
list := number.toStr.chars.map (|Int c -> Obj?| { return digitsToStrings[c.fromDigit] } );
return list.join("") |Obj? c -> Str| { c == null ? "": c };
}
}

0 comments on commit 4c6665e

Please sign in to comment.