Passing a function to an argument in Scala

Is there any way to do something like argument.<keyword>(function) in Scala?

For example:

[1,2,3].supply(myFunc) yielding 6 if myFunc were the summation function.

It just seems easier to chain functions if I were able to do this, instead of calculating something and 'wrapping it' into an argument for a function call.

Answers


You can define it yourself if you want. It's frequently called the "pipe operator":

class AnyWrapper[A](wrapped: A) {
  def |>[B](f: A => B) = f(wrapped)
}
implicit def extendAny[A](wrapped: A): AnyWrapper[A] = new AnyWrapper(wrapped)

Then:

def plus1(i: Int) = i + 1
val fortyTwo = 41 |> plus1

Need Your Help

PHP Regex Parse query string containing un-encoded ampersands

php regex parsing query-string ampersand

I'm receiving a query string (from a terrible payment system whose name I do not wish to sully publicly) that contains un-encoded ampersands

Managing NSStoreType changes between debug and release builds

cocoa core-data debugging release

NSXMLTypeStore is used when starting Core Data backed application by default because it's good for debugging purposes. But practice dictates that developer should use either NSBinaryStoreType,

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.