Questions tagged [function-literal]
38 questions
2
votes
1 answer
Placeholder in Scala function doesnt work with mutable Maps
I have the following code in Scala which works:
var queryMap = Map("name" -> "tim")
age_list.foreach { age => queryMap += ("age" -> age.toString) }
If I include placeholders in the function, it breaks:
var queryMap = Map("name" ->…

Llama.new
- 357
- 8
- 23
2
votes
0 answers
Type error when chaining map with toSet and using function literal with underscore
Passing function literal with underscore as an argument to map chained with toSet on another collection (e.g. List) results in type error:
scala> List(1, 2, 3).toSet map (_.toString)
:12: error: missing parameter type for expanded function…

kirlich
- 7,998
- 2
- 19
- 10
2
votes
1 answer
Functions and function literals in scala
I am new to Scala. Please tell the difference between
def fun( t: Int => Int):Unit = {
and
def fun(t: =>Int):Unit {
and
def fun(t:=>Int):Unit { (without space b/w ":" and "=>"))

Surbhi Jain
- 369
- 1
- 11
2
votes
2 answers
Scala function literals and placeholder syntax on zipped collections
I'm currently learning Scala and have struggled with using placeholder syntax on zipped collections. For example, I want to filter the zipped array from items where l2[i] >= l1[i]. How can I do this using an explicit function literal or the…

g8524588
- 23
- 3
2
votes
1 answer
Default type-parametrized function literal class parameter
Is this an intended behavior or is it a bug? Consider the following trait (be it a class, doesn't matter):
trait P[T] {
class Inner(val f: T => Unit = _ => println("nope"))
}
This is what I would have expected:
scala> val p = new P[Int] {
…

Karel Smutný
- 1,100
- 1
- 9
- 17
2
votes
4 answers
`return` in a scala function literal
I'm trying to do something like this:
var fun : (Int,Int) => Double = (a,b) =>
{
// do something
return 1.0
}
However, my IDE complaints with Return statement outside method definition. So how do I explicitly give a return statement in a…

OneZero
- 11,556
- 15
- 55
- 92
1
vote
1 answer
Better approach nulling variables, objects in Javascript
I am building something for mobile and would like somehow to clear, null objects, variables to release a bit of memory. Here I have two quick examples, both are anonymous functions as I believe but what way is better or more valid approach? Sorry if…

devjs11
- 1,898
- 7
- 43
- 73
1
vote
1 answer
Difference between functions and function literals in ActionScript 3?
What is the difference between the following two function definitions in ActionScript 3?
f = function(arg) {
// body
}
and
function f(arg) {
// body
}

Tim
- 13,904
- 10
- 69
- 101
1
vote
1 answer
How to determine whether an object was created using an object literal or an Object constructor call?
More specifically, how would you determine if a certain object was created using a literal or not?
var s1 = new String();
var s2 = ""; // Literal
var o1 = new Object();
var o2 = {}; // Literal
var f1 = new Function();
var f2 = function(){}; //…

Shaz
- 15,637
- 3
- 41
- 59
1
vote
1 answer
scala: whether the following two are the same
Code piece 1
maps foreach { case (k, v) =>
// do something
}
code piece 2:
maps foreach {
case (k, v) => {
// do something
}
}
I am new to scala. Just wonder whether the above two pieces of codes are the same or not? which one is…

BAE
- 8,550
- 22
- 88
- 171
1
vote
2 answers
missing parameter type for expanded function for my function; not for another with same signature
Short form: I have a method with the same signature as Future.recover. Passing a partial function to Future's version works. Passing the same PF to my version results in a missing parameter type for expanded function. The argument types of an…

bwbecker
- 1,031
- 9
- 21
1
vote
2 answers
Scala compilation error- found : Int required: Int => Int
I am new to scala. I don't understand the compilation error for the below code:
def delayed( t:(Int)=> Int):Unit={
println("In delayed method")
var y=t;
println(y)
}
def time(x:Int):Int={
x*2
}
and when I call
delayed(time(8))
I…

Surbhi Jain
- 369
- 1
- 11
1
vote
1 answer
How can I pass an extern(C) function literal?
Say I am interfacing to C.
Here is an wrapping function to the interface.
@property extern(C) void onEvent(void function(InterfaceStruct*, int, int, int) nothrow callback)
{
interfaceSetCallback(handle, callback);
}
All…

hpm
- 1,202
- 13
- 34
1
vote
1 answer
Type inference with function literals
I am trying currently trying to solve some Scala problem set for getting to know with the language. I am stuck with problem 11 where my solution will not compile. My question is: Why is this illegal in Scala?
def countPackSingle[A](list: List[A]):…

Rafael Winterhalter
- 42,759
- 13
- 108
- 192
1
vote
2 answers
Scala class wrapping a partially applied constructor - how to use it to create API methods?
I'm trying to create a simple api for dealing with intervals of hours. (I'm aware of joda time, and I'm not trying to reinvent it. This is rather an exercise).
What I would like to achieve is this:
(1)
assert(from("20:30").to("20:50") ==…

teo
- 1,393
- 1
- 15
- 25