Questions tagged [thunk]

A parameterless closure (functional programming) or a function generated by a compiler to aid runtime linking with a dynamic library function.

  • In functional programming, a thunk is an anonymous and parameterless function (closure), used to pass lazily evaluated expressions.
  • In C++, a thunk is a compiler generated function which optimizes virtual function calls.
  • On systems which support dynamic linking, a thunk is often automatically inserted to provide a place for the runtime linker to insert code for invoking the actual implementation found in a library.
184 questions
3
votes
2 answers

Scala lazy val caching

In the following example: def maybeTwice2(b: Boolean, i: => Int) = { lazy val j = i if (b) j+j else 0 } Why is hi not printed twice when I call it like: maybeTwice2(true, { println("hi"); 1+41 }) This example is actually from the book…
joesan
  • 13,963
  • 27
  • 95
  • 232
3
votes
1 answer

Have Haskell expand certain thunks at compile time?

Is there a way to have Haskell expand certain thunks at run time. For example, say I have --Purposely inefficient code for demonstration fib 0=0 fib 1=1 fib n=fib n=fib (n-1) + fib (n-2) goldRatio=fib 100 / fib 101 How could I have it evaluate…
PyRulez
  • 10,513
  • 10
  • 42
  • 87
3
votes
2 answers

More succinct delayed evaluation than function(){return x}?

I'm porting some Python code that relies heavily on delayed evaluation. This is accomplished by via thunks. More specifically, any Python expression for which delayed evaluation is desired gets enclosed within a Python "lambda expression",…
kjo
  • 33,683
  • 52
  • 148
  • 265
3
votes
3 answers

Making Scala choose less specific overloaded method in presence of argument of type Nothing

If come across an interesting case with thunks versus functions in the presence of type Nothing: object Test { def apply(thunk: => Any ): String => Any = _ => thunk def apply(fun: String => Any): String => Any = fun } val res0 = Test {…
0__
  • 66,707
  • 21
  • 171
  • 266
3
votes
2 answers

Using v-table thunks to chain procedure calls

I was reading some articles on net regarding Vtable thunks and I read somewhere that thunks can be used to hook /chain procedures calls. Is it achievable? Does anyone know how that works , also I am not able to find good resource explaining…
anand
  • 11,071
  • 28
  • 101
  • 159
2
votes
1 answer

The value of ESP was not properly saved.... and C/C++ calling conventions

I am writing an application using the OpenCV libraries, the Boost libraries and a pieve of code that I have downloaded from this LINK. I have created a project under the same solution with Thunk32 and I have the following…
2
votes
2 answers

Is the ATL incompatibility with DEP fixable?

ATL uses thunks to manage callbacks for windows, and apparently it needs to allow for data execution. Microsoft says: Note that system DEP policy can override, and having DEP AlwaysOn will disable ATL thunk emulation, regardless of the…
user541686
  • 205,094
  • 128
  • 528
  • 886
2
votes
4 answers

Can we implement c++ thunk in linux?

I want to use class member functions as callbacks, I don't use libsigc, because it's slow. In ATL, we can use member function for C-style callback(http://www.codeproject.com/KB/cpp/SoloGenericCallBack.aspx), so can we implement c++ thunk in…
xufan
  • 392
  • 1
  • 5
  • 18
2
votes
1 answer

Uncaught TypeError: Cannot read properties of undefined (reading 'then')?

saveProfile() does not return promise, although the person who has exactly the same code, the same function returns promise return async (dispatch, getState) => { const authUserId = getState().auth.id let data = await…
2
votes
1 answer

Can a getter return a thunk or value depending on the type call call (virt. property or function)?

I know this a weird question, but I'll ask it anyway. The pseudo code function below provides a virtual property getter. What it should do is this: > const calc = Calculator(3) > calc.multiple // returns getter result directly 6 >…
LongHike
  • 4,016
  • 4
  • 37
  • 76
2
votes
2 answers

Error: Expected the root reducer to be a function. Instead, received: 'undefined'

I keep running into this error! Error: Expected the root reducer to be a function. Instead, received: 'undefined' I've tried every answer I could find to no avail, here is all the pertinent stuff! Root Reducer const createRootReducer = (history) =>…
Manny Ledoux
  • 23
  • 1
  • 1
  • 4
2
votes
0 answers

Problem with Jest and dispatch with async functions

I am working with React + Redux (with thunk) and I am using Jest for testing. I have the next code: export const startUploading = (file) => { return async(dispatch, getState) => { const {active:activeNote} = getState().notes; …
2
votes
2 answers

useEffect() cause re-renders and multiple requests to api

Problem Functional component causes multiple re-renders causing multiple ajax requests. What is the solution for this? This is my code. export default function MyMenu() { let menu = useStoreState(state => state.menu.menu); const getMenu =…
2
votes
2 answers

Are thunk and function currying the same?

When I learn thunk, I think they are like function currying. Why is it called thunk? Thunk function add(x, y){ return x + y } function thunk() { return add(10, 20) } Function currying function multiply(a, b, c) { return a * b *…
2
votes
1 answer

Lazy evaluation, thunk and function closure in Scala

case class Test[A](elem: () => A) object Fun extends App { def test1(v: => Int): Test[Int] = Test(() => v) val a1 = test1({ println("hello"); 1 }) val a2 = a1.elem() //echoes hello val a3 = a1.elem() //echoes hello def test2(v: =>…
1 2
3
12 13