Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: ChrisMarinos/FSharpKoans
base: ae6c2bd8ff
...
head fork: ChrisMarinos/FSharpKoans
compare: ad071fe8ab
  • 3 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
View
7 FSharpKoans.Core/KoanResult.fs
@@ -10,3 +10,10 @@ with
match this with
| Success x -> x
| Failure (x, _) -> x
+
+[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
+module KoanResult =
+ let map f (x:KoanResult) =
+ match x with
+ | Success m -> Success <| f m
+ | Failure (m, e) -> Failure (f m, e)
View
57 FSharpKoans.Core/KoanRunner.fs
@@ -1,35 +1,40 @@
namespace FSharpKoans.Core
open System
+open System.Collections.Generic
-//TODO: refactor this filth
type KoanRunner(containers) =
- let combine joinMessages message (next:KoanResult) =
- let message = joinMessages message next.Message
-
- match next with
- | Success _ -> Success(message)
- | Failure (_, ex) -> Failure(message, ex)
-
- let combineUnlessFailed joinMessages state next =
- match state with
- | Success s -> combine joinMessages s next
- | Failure _ -> state
-
- let executeContainer container =
+ let findFailureOrLastResult (values:seq<KoanResult>) =
+ let e = values.GetEnumerator()
+ let rec helper() =
+ let last = e.Current
+ if e.MoveNext() then
+ match e.Current with
+ | Success _ -> helper()
+ | Failure _ -> e.Current
+ else
+ last
+ helper()
+
+ let buildKoanResult builderString (current:KoanResult) (next:KoanResult) =
+ next
+ |> KoanResult.map (sprintf builderString current.Message)
+
+ let getContainerResult container =
let name = container.GetType().Name.ToString()
let lineOne = sprintf "When contemplating %s:" name
-
- let joinLines x y = String.Concat(x, System.Environment.NewLine,
- " ", y)
-
+
container
|> KoanContainer.runKoans
- |> Seq.fold (combineUnlessFailed joinLines) (Success lineOne)
-
+ |> Seq.scan (buildKoanResult "%s\n %s") (Success lineOne)
+ |> findFailureOrLastResult
+
member this.ExecuteKoans =
- let joinContainers x y = String.Concat(x, System.Environment.NewLine,
- System.Environment.NewLine, y)
-
- containers
- |> Seq.map executeContainer
- |> Seq.fold (combineUnlessFailed joinContainers) (Success "")
+ let result =
+ containers
+ |> Seq.map getContainerResult
+ |> Seq.scan (buildKoanResult "%s\n\n%s") (Success "")
+ |> findFailureOrLastResult
+
+ match result with
+ | Success m -> Success (m.Replace("\n", Environment.NewLine))
+ | Failure (m, e) -> Failure (m.Replace("\n", Environment.NewLine), e)
View
24 FSharpKoans.Test/KoanResults.fs
@@ -0,0 +1,24 @@
+module KoansRunner.Test.KoanResults
+open FSharpKoans.Core
+open NUnit.Framework
+
+[<Test>]
+let ``map lets you project a message when success``() =
+ let result = Success "sample message"
+
+ let mappedResult =
+ result
+ |> KoanResult.map (fun x -> x + " expanded")
+
+ Assert.AreEqual(Success "sample message expanded", mappedResult)
+
+[<Test>]
+let ``map lets you project a message when failure``() =
+ let ex = new System.Exception("abcd")
+ let result = Failure ("sample message", ex)
+
+ let mappedResult =
+ result
+ |> KoanResult.map (fun x -> x + " expanded")
+
+ Assert.AreEqual(Failure ("sample message expanded", ex), mappedResult)
View
26 ideaboard.txt
@@ -1,26 +0,0 @@
-Test asserts - Message, bool, etc.
-Tuples - syntax
-Pattern Matching
-Functions returning functions
-Functions taking functions
-if expressions
-for comprehensions
-immutability/side effects
-state identity lifetime
-collections array, list
-map, reduce, filter
-classes
-lists / cons
-memoization
-lazy sequences
-recursion
-traits / mixins
-recrusive list processing
-lambda
-currying / pfa
-null option types
-mutation
-type signatures
-reflection
-scala properties - set date / use earlier date
-

No commit comments for this range

Something went wrong with that request. Please try again.