1
\$\begingroup\$

Please, review my implementation of the F# pipeline operator in Scala

class PipelineContainer[F](value: F) {
 def |>[G] (f: F => G) = f(value)
}
implicit def pipelineEnrichment[T](xs: T) = new PipelineContainer(xs)
 test("Find the last but one element in the list") {
 // example: penultimate(List(1,2,3,3,4,5))
 // result: 4
 def reverse[T](list: List[T]) = list.reverse
 def head[T](list: List[T]) = list.head
 def tail[T](list: List[T]) = list.tail
 def lastButOne[T](list: List[T]) = list |> reverse |> tail |> head
 }
asked May 28, 2013 at 14:15
\$\endgroup\$

1 Answer 1

8
\$\begingroup\$

There isn't much that can be reviewed. If you use Scala 2.10 then you should use an implicit class instead of an implicit conversion. Even better: use an implicit value class:

implicit class PipelineContainer[F](val value: F) extends AnyVal {
 def |>[G] (f: F => G) = f(value)
}

Furthermore, in Scala, type parameter are enumerated starting with an A, but this is more a style issue.

answered May 28, 2013 at 14:58
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.