Skip to content

Commit

Permalink
SelectMany
Browse files Browse the repository at this point in the history
  • Loading branch information
mausch committed Sep 25, 2011
1 parent fb7356d commit 9b64dd4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
17 changes: 17 additions & 0 deletions Formlets.CSharp.Tests/Usage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,22 @@ join age in e.Int() on 1 equals 1
Assert.Equal(42, r.Value.Value.age);
}

[Fact]
public void LINQ_SelectMany() {
var e = new FormElements();
var f = from name in e.Text()
from _ in Formlet.Raw(X.E("br"))
from age in e.Int()
where age == 42
select new { name, age };
var r = f.Run(new Dictionary<string, string> {
{"f0", "John"},
{"f1", "42"},
});
Assert.True(r.Value.HasValue());
Assert.Equal("John", r.Value.Value.name);
Assert.Equal(42, r.Value.Value.age);
}

}
}
13 changes: 12 additions & 1 deletion Formlets.CSharp/FormletT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,18 @@ public Formlet<T> Where(Func<T,bool> pred) {
}

public Formlet<R> Join<I, K, R>(Formlet<I> inner, Func<T, K> outerKeySelector, Func<I, K> innerKeySelector, Func<T, I, R> resultSelector) {
return Formlet.Tuple2<T, I>().Ap(this).Ap(inner).Select(t => resultSelector(t.Item1, t.Item2));
return Formlet.Tuple2<T, I>()
.Ap(this)
.Ap(inner)
.Select(t => resultSelector(t.Item1, t.Item2));
}

public Formlet<R> SelectMany<R>(Func<Formlet<T>, Formlet<R>> collector) {
return collector(this);
}

public Formlet<R> SelectMany<U, R>(Func<Formlet<T>, Formlet<U>> collector, Func<T, U, R> selector) {
return this.Join<U,object,R>(collector(this), x => x, x => x, selector);
}

public Formlet<T> Satisfies(Func<T,bool> pred, Func<T, List<XNode>, IEnumerable<XNode>> error, Func<T, IEnumerable<string>> errorMsg) {
Expand Down

0 comments on commit 9b64dd4

Please sign in to comment.