Yesterday I wrote a post called "My impressions from Øredev" on Dotway's blog. In summary, I had a great time there and I'm still very inspired from the experience.
Hope to see you there next year! :)
Some code, some agile, and some muda.
Yesterday I wrote a post called "My impressions from Øredev" on Dotway's blog. In summary, I had a great time there and I'm still very inspired from the experience.
Hope to see you there next year! :)
Earlier this week I realized that you are able to define functions in LINQ queries. So I made myself a sweet little FizzBuzz in LINQ.
It's not pretty code, but I had fun writing it. Though, the code would be a somewhat nicer without all the type declarations everywhere. Hehe, I guess that if your code reminds you of The Obfuscated Code Contest, it's a pretty good sign that your code stinks. Clearly muda!
[TestMethod]
public void FizzBuzz()
{
var lines = from x in Enumerable.Range(1, 100)
let divides = new Func<int, int, bool>((denominator, nom) => nom % denominator == 0)
let fizz = new Func<int, Tuple<int, string>>(
n => new Tuple<int, string>(n, divides(3, n) ? "Fizz" : ""))
let buzz = new Func<Tuple<int, string>, Tuple<int, string>>(
tuple => new Tuple<int, string>(tuple.Item1, tuple.Item2 +
(divides(5, tuple.Item1) ? "Buzz" : "")))
let returnNumberIfEmptyString = new Func<Tuple<int, string>, string>(
tuple => tuple.Item2 == "" ? tuple.Item1.ToString() : tuple.Item2)
select returnNumberIfEmptyString(buzz(fizz(x)));
foreach (var line in lines)
{
Debug.WriteLine(line);
}
}
(Oh, forgot to say that I use the .NET 4.0 tuples for this post)
[Test]
public void CreateListOfFoos()
{
var xs = new List<Foo>();
for (int i = 0; i < 10; i++)
{
xs.Add(new Foo());
}
Assert.AreEqual(10, xs.Count());
}
[Test]
public void ExtractMethod()
{
var times = 10;
Func<Foo>newFoo = () => new Foo();
var xs = CreateFooNumberOfTimes(times, newFoo);
Assert.AreEqual(10, xs.Count());
}
private IEnumerable<Foo> CreateFooNumberOfTimes(int times, Func<Foo> newFoo)
{
var xs = new List<Foo>();
for (int i = 0; i < times; i++)
{
xs.Add(newFoo.Invoke());
}
return xs;
}
[Test]
public void GeneralizeTypeForFunc()
{
var times = 10;
Func<Foo> newFoo = () => new Foo();
var xs = CreateTNumberOfTimes(times, newFoo);
Assert.AreEqual(10, xs.Count());
}
private IEnumerable<T> CreateTNumberOfTimes<T>(int times, Func<T> newFoo)
{
var xs = new List<T>();
for (int i = 0; i < times; i++)
{
xs.Add(newFoo.Invoke());
}
return xs;
}
[Test]
public void ExtractForLoopLogic()
{
Func<Foo> newFoo = () => new Foo();
int i = 0; // Will be modified lots of times
Func<bool> expr = () => i < 10;
Action inc = () => i++;
var xs = CreateTUntil(newFoo, expr, inc);
Assert.AreEqual(10, xs.Count());
}
private IEnumerable<T>CreateTUntil<T>(Func<T> newT, Func<bool> expr, Action inc)
{
var xs = new List<T>();
for (; expr.Invoke(); inc.Invoke())
{
xs.Add(newT.Invoke());
}
return xs;
}
[Test]
public void ForLoopToWhileLoop()
{
Func<Foo> newFoo = () => new Foo();
Func<int,bool> expr = a => a < 10;
Func<int,int> inc = i => i + 1;
var xs = CreateTUntilUsingWhile(newFoo, 0, expr, inc);
Assert.AreEqual(10, xs.Count());
}
// Now a pure method
private IEnumerable<T> CreateTUntilUsingWhile<T>
(Func<T> newT, int init, Func<int,bool> expr, Func<int,int> inc)
{
var xs = new List<T>();
int i = init;
while (expr.Invoke(i))
{
xs.Add(newT.Invoke());
i = inc.Invoke(i);
}
return xs;
}
[Test]
public void ArgumentForConstructor()
{
Func<int,Result<Foo,int>> gen = n => new Result<Foo,int>(new Foo(), n + 1);
Func<int,bool> expr = a => a < 10;
var xs = GeneralizedCreateTWithArgsUntilUsingWhile(gen, 0, expr);
Assert.AreEqual(10, xs.Count());
}
private IEnumerable<T> GeneralizedCreateTWithArgsUntilUsingWhile<T,A>
(Func<a,Result><T,A>> gen, A init, Func<A,bool> expr)
{
var xs = new List<T>();
var i = init;
while (expr.Invoke(i))
{
var result = gen.Invoke(i);
xs.Add(result.Value);
i = result.Accumulator;
}
return xs;
}
[Test]
public void WhileLoopToRec()
{
Func<int, Result<Foo, int>> gen = n => new Result<Foo, int>(new Foo(), n + 1);
Func<int, bool> expr = a => a < 10;
var xs = CreateTUntilUsingRec(gen, 0, expr);
Assert.AreEqual(10, xs.Count());
}
private IEnumerable<T> CreateTUntilUsingRec<T,A>
(Func<A, Result<T, A>> gen, A init, Func<A, bool> expr)
{
if (!expr.Invoke(init))
return new List<T>();
var result = gen.Invoke(init);
return (new List<T> {result.Value})
.Concat(CreateTUntilUsingRec(gen, result.Accumulator, expr));
}
[Test]
public void MergeOnceMore()
{
int? i = 0;
Func<int, Result<int?, int>> gen = n => new Result<int?, int>(n < 10 ? i : null, n + 1);
var xs = CreateTUntilUsingRecNullCheck(gen, 0);
Assert.AreEqual(10, xs.Count());
}
private IEnumerable<T> CreateTUntilUsingRecNullCheck<T, A>
(Func<A, Result<T, A>> gen, A init)
{
var result = gen.Invoke(init);
if (result.Value == null)
return new List<T>();
return (new List<T> { result.Value })
.Concat(CreateTUntilUsingRecNullCheck(gen, result.Accumulator));
}
let rec anamorphism n f =
match f n with
| option.None -> []
| option.Some (e,next) -> e::(anamorphism next f)
> anamorphism 0 (fun a -> if a < 5 then option.Some("Bar" + a.ToString(), a + 1) else option.None);;
val it : string list = ["Bar0"; "Bar1"; "Bar2"; "Bar3"; "Bar4"]
[<Fact>]
let tableLegsScenario =
given (ATableWith 4 Legs)
|> whens (ICutOf 1 Leg)
|> thens (ItHasOnly 3 LegsLeft)
#light
open FsStoryRunner
open MutatedTurtleMovesImpl
open Xunit
(*
In order to impress my friends
As a .NET programmer
I want to draw funny fractal pictures
*)
[<fact>]
let MoveTurtleToPosition() =
given ATurtle
|> andGiven IsRotated90DegreesToTheRight
|> whens (TurtleWalksSteps 9)
|> thens (TurtleIsLocatedAt (0,9))
|> endStory
#light
open Turtle
open FsxUnit.Syntax
let mutable turtle = new Turtle() // turtle must have type Turtle
let ATurtle () = turtle <- new Turtle() // For reuse in same story let MovesOneStepForward () = turtle.Go() let IsMovedOneStepForward () = turtle.Position.X |> should equal 1 let RotationIs angle () = turtle.Direction |> should equal 0.0
let ATurtle () = new TurtleImmutable()
let IsRotated90DegreesToTheRight = fun (turtle : TurtleImmutable) -> turtle.Left()
let TurtleWalksSteps steps = fun (turtle : TurtleImmutable) -> turtle.GoSteps(steps)
let TurtleIsLocatedAt (x,y) = fun (turtle : TurtleImmutable) -> turtle.Position |>
should equal (new Position(x,y)) ; turtle
[Story, Test]When this test is run with ReSharper, the test passes and outputs the story with indentation. Nice! (Except the "mockery has started" part of the story..)
public void GetMoneyWhenPassGO()
{
// Set up and initialize
var mocks = new MockRepository();
var container = new Rhino.Testing.AutoMocking.AutoMockingContainer(mocks);
container.Initialize();
// Resolve and obtain references
IPlayerTurn turn = container.Create<PlayerTurn>();
IBoard board = container.Resolve<IBoard>();
IPlayer player = container.Resolve<IPlayer>();
turn.AddPlayers(new List<Player>() {player});
// Story begins here
var story = new Story("Player recieves money when passes 'GO'");
story
.AsA("Player")
.IWant("to recieve money when I pass 'GO'")
.SoThat("I can buy things that generate money");
story
.WithScenario("Normal play scenario")
.Given("A board with 4 squares", () => Expect.Call(board.NumberOfSquares).Return(4))
.And("a player near 'GO'", () => Expect.Call(board.GetIndexForPlayer(player)).Return(2))
.And("mockery has started", () => mocks.ReplayAll())
.When("player passes 'GO'",() => turn.PlayerSequence(player, 3))
.Then("the player earns $4000", () => player.AssertWasCalled(x => x.Credit(4000)));
}