Plus not working in Scala interpreter

I am trying to sum a list using fold in the Scala interpreter, but it keeps giving me a strange error. When I type this:

val list = List(1,2,3)
(list :\ 0)(+)

I expect to get 6. However, the interpreter says

error: illegal start of simple expression
(list :\ 0)(+)
             ^

If I define my own function

def plus(a: Int, b: Int) = a+b

and call

(list :\ 0)(plus)

I do in fact get 6.

I'm sure I'm missing something really simple here, but I can't figure it out, so any help is much appreciated.

Answers


The plus operator by itself is not a function it is a symbol and has no type. What you are looking for is the following

val list = List(1,2,3)
(list :\ 0)(_+_)

The _+_ is shorthand for an anonymous function that takes two parameters and calls the + method on the first parameter passing in the second.


Need Your Help

Devise: current_user == nil?

ruby-on-rails ruby devise

My goal is to display the 'edit' and 'delete' buttons only to the user who created the listing. However, for some reason, current_user is returning nil. Any idea why this is happening?

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.