Questions tagged [function-literal]

38 questions
41
votes
3 answers

The argument types of an anonymous function must be fully known. (SLS 8.5)

I have a function literal {case QualifiedType(preds, ty) => t.ty = ty ; Some((emptyEqualityConstraintSet,preds)) } Which results in an error message missing parameter type for expanded function The argument types of an…
Theodore Norvell
  • 11,939
  • 6
  • 27
  • 42
17
votes
2 answers

Multiline function literal as arguments in Scala

I always wondered why sometimes with function literals we can ignore the curly brace even for multiple statements. To illustrate this, the syntax for a multiline function literal is to enclose the statements with curly braces. Like so, val fl = (x:…
altcoder
  • 230
  • 1
  • 2
  • 7
16
votes
16 answers

Which languages support *recursive* function literals / anonymous functions?

It seems quite a few mainstream languages support function literals these days. They are also called anonymous functions, but I don't care if they have a name. The important thing is that a function literal is an expression which yields a function…
13
votes
6 answers

Exact meaning of Function literal in JavaScript

In JavaScript there are both Object literals and function literals. Object literal: myObject = {myprop:"myValue"} Function literal: myFunction = function() { alert("hello world"); } What is the significance of the word literal? Can we say Java…
dublintech
  • 14,457
  • 27
  • 77
  • 110
8
votes
3 answers

JavaScript Object literal method: Recursive call

Is it possible to call recursively a method from an object literal? For example: (function () { 'use strict'; var abc = ['A', 'B', 'C'], obj = { f: function () { if (abc.length) { …
7
votes
1 answer

How should I avoid unintentionally capturing the local scope in function literals?

I'll ask this with a Scala example, but it may well be that this affects other languages which allow hybrid imperative and functional styles. Here's a short example (UPDATED, see below): def method: Iterator[Int] { // construct some large…
Scott Morrison
  • 3,090
  • 21
  • 39
5
votes
1 answer

Scala underscore use to simplify syntax of function literals

I have the following code: var x = Array(1,3,4,4,1,1,3) var m = Int.MaxValue x.foreach((x)=>(m = m min x)) I tried to simplify last sentence to: x.foreach((m = _ min m)) But the interpreter says: scala> x.foreach((m = _ min m)) :8:…
5
votes
5 answers

how to simplify scala's function literal like this?

I'm new to scala and trying to write a function literal that check whether a given integer is odd or not. my first attempt is: val isOdd = (x:Int) => (x & 1) == 1 it works great, and, since the parameter x only appears once within this function…
hind_d
  • 355
  • 2
  • 6
4
votes
2 answers

Scala Currying and function literals

I was reading the-neophytes-guide-to-scala-part-10 where I came across following code. type EmailFilter = Email => Boolean val minimumSize: Int => EmailFilter = n => email => email.text.size >= n I understood the first line where type alias…
Kumar Waghmode
  • 409
  • 2
  • 14
4
votes
2 answers

Named functions in Javascript accessible before declaration, but function literals aren't

I'm trying to figure out how this works. When I reference a named Javascript function that hasn't been declared yet, in some circumstances, it works. But if I use a function literal, it doesn't, but it also doesn't fail with a…
David Ehrmann
  • 6,655
  • 1
  • 23
  • 33
3
votes
2 answers

Questions about placeholders in Scala

Consider the following definition in Scala: val f = ((_: Int) + 1).toString() The code assigns to f the string representation of the function literal _ + 1, which is quite natural, except that this is not i want. i intended to define a function…
weidi
  • 832
  • 7
  • 20
3
votes
2 answers

How to define a function that takes a function literal (with an implicit parameter) as an argument?

I want to be able to do something on these lines (won't compile): def logScope(logger:Logger)(operation: (implicit l:Logger) => Unit) {/* code */ operation(logger) /* code */} def operationOne(implicit logger:Logger) {/**/} def…
3
votes
1 answer

Function Literal referencing by val and def

I'm trying to understand the crucial difference between these two approaches of referencing / defining Function Literal (reference to anonymous function): By val scala> val v2 = new Function[Int, Int] { | def apply(a: Int): Int = a + 1 |…
ses
  • 12,339
  • 25
  • 106
  • 203
2
votes
0 answers

How To Obtain An Annotation Of A Function Literal At Runtime

package _z_additional import org.junit.jupiter.api.Test import org.junit.jupiter.api.TestInstance import kotlin.reflect.KFunction import kotlin.reflect.full.declaredMemberFunctions import…
Sero
  • 1,572
  • 1
  • 15
  • 29
2
votes
1 answer

What is the scala notation written as _:type?

I'm following the scala tutorial. In function literal, it has a following notation: (_ : *type*) => println("pressed") For example, (_ : Int) => println("pressed") In this notation, I couldn't understand what (_ : type) means.
user7159879
  • 427
  • 1
  • 5
  • 12
1
2 3