Skip to content

Instantly share code, notes, and snippets.

@AlexeyRaga
Last active August 29, 2015 14:02
Show Gist options
  • Save AlexeyRaga/bc07a9ac731e054a35f8 to your computer and use it in GitHub Desktop.
Save AlexeyRaga/bc07a9ac731e054a35f8 to your computer and use it in GitHub Desktop.
Pipelines and fluent interfaces
//already defined in F# base library, but I wanted to show how simple it is
let (|>) a f = f a
let latestRequest =
user
|> login
|> clickMenu "Forms"
|> waitPageLoad
|> openFilter ["Communication"; "Request"]
|> results
|> Seq.head
class Program {
static void Main(string[] args) {
var latestRequest =
//.Take() is completely unnecessary, but it may make it look more "natural" (or not)
User.Take()
.Then(Login)
.Then(ClickMenu, "Forms")
.Then(WaitForPageLoad)
.Then(OpenFilter, new[] { "Communication", "Request" })
.Then(GetResults)
.First();
}
}
public static class Pipeline {
//an identity function, just syntaxic sugar for making pipelines more "natural"
public static A Take<A>(this A self) { return self; }
public static B Then<A, B>(this A self, Func<A, B> selector) {
return selector(self);
}
public static B Then<A, P1, B>(this A self, Func<P1, A, B> selector, P1 p1) {
return selector(p1, self);
}
public static B Then<A, P1, P2, B>(this A self, Func<P1, P2, A, B> selector, P1 p1, P2 p2) {
return selector(p1, p2, self);
}
public static B Then<A, P1, P2, P3, B>(this A self, Func<P1, P2, P3, A, B> selector, P1 p1, P2 p2, P3 p3) {
return selector(p1, p2, p3, self);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment