当从matlab调用外部C库(DLL)时,似乎matlab将首先尝试filename_thunk_pcwinXX.dll
在临时目录中生成名为(其中XX依赖于OS的32或64)的thunk文件.
在matlab文档中,它提到该文件是外部DLL和Matlab之间的接口,因此它不包含原始外部DLL的任何其他数据.
问题是,我编写了一个库,在运行时可能会调用其他库,matlab总会给我一些奇怪的结果,甚至在库调用某些库时崩溃(不是全部,我的库和其他库都是64位) DLL).
当从其他C/C++程序而不是matlab调用时,我的所有库函数都可以正常工作.
我只是想知道,为了让matlab正常工作,我的库在运行时调用的其他库是否也需要得到某种thunk文件?我简直无法相信,但我不知道matlab如何处理外部库的确切方法.
所以我一直在阅读co库的用法,我在大多数博客文章中看到的一般设计模式是包含回调函数的函数.然后使用es6生成器将这些thunk产生到co
对象.像这样:
co(function *(){
var a = yield read(‘Readme.md’);
var b = yield read(‘package.json’);
console.log(a);
console.log(b);
});
function read(path) {
return function(done){
fs.readFile(path, ‘utf8', done);
}
}
Run Code Online (Sandbox Code Playgroud)
而且我可以理解,因为它带来了承诺的所有好处,例如更好的可读性和更好的错误处理.
但是co
如果你已经有了承诺,那么使用的重点是什么?
co(function* () {
var res = yield [
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3),
];
console.log(res); // => [1, 2, 3]
}).catch(onerror);
Run Code Online (Sandbox Code Playgroud)
为什么不喜欢
Promise.all([
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3),
]).then((res) => console.log(res)); // => [1, 2, 3]
}).catch(onerror);
Run Code Online (Sandbox Code Playgroud)
对我而言,与Promise版本相比,co使代码看起来更加混乱.
Can someone explain to me what a Thunk is?
and an ATL Thunk?
I know a thunk has something to do with the vtbl and execution of code to find the right function pointer. Am I right?
我正在移植一些很大程度上依赖于延迟评估的Python代码.这是通过thunks完成的.更具体地说,任何<expr>
需要延迟评估的Python表达式都包含在Python"lambda表达式"中,即lambda:<expr>
.
AFAIK,最接近的JavaScript等价物function(){return <expr>}
.
由于我正在使用的代码绝对充斥着这样的thunk,我想让它们的代码更加简洁,如果可能的话.这样做的原因不仅在于保存字符(当涉及到JS时不可忽略的考虑因素),而且还使代码更具可读性.要了解我的意思,请比较此标准JavaScript表单:
function(){return fetchx()}
Run Code Online (Sandbox Code Playgroud)
同
\fetchx()
Run Code Online (Sandbox Code Playgroud)
在第一种形式中,实质性信息,即表达fetchx()
,在印刷中被周围的function(){return
...... 掩盖}
.在第二种形式1中,只有一个(\
)字符用作"延迟评估标记".我认为这是最佳方法2.
AFAICT,此问题的解决方案将分为以下几类:
eval
模拟延迟评价.我对听到最后三个类别的回答特别感兴趣.
PS:我知道使用eval
(上面的选项1)在JS世界中被广泛弃用,但是,FWIW,下面我给出了这个选项的玩具插图.
我们的想法是定义一个私有包装类,其唯一目的是将纯字符串标记为用于延迟评估的JavaScript代码.然后使用具有短名称的工厂方法(例如C
,用于"CODE")来减少例如,
function(){return fetchx()}
Run Code Online (Sandbox Code Playgroud)
至
C('fetchx()')
Run Code Online (Sandbox Code Playgroud)
首先,工厂C
和辅助函数的定义maybe_eval
:
var C = (function () {
function _delayed_eval(code) { this.code = code; }
_delayed_eval.prototype.val = function () { return eval(this.code) };
return function (code) { …
Run Code Online (Sandbox Code Playgroud) 有没有办法让Haskell在运行时扩展某些thunk.例如,说我有
--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
Run Code Online (Sandbox Code Playgroud)
我怎么能goldRatio
在编译时评估它.例如,与
{-# EVALUATE goldRatio #-}
Run Code Online (Sandbox Code Playgroud)
它只需要弱头形状,因为Control.Deepseq.force
可以处理其余部分.我听说模板haskell可以做到这一点,但我不太清楚.
注意:我现在正在使用GHC.
在以下示例中:
def maybeTwice2(b: Boolean, i: => Int) = {
lazy val j = i
if (b) j+j else 0
}
Run Code Online (Sandbox Code Playgroud)
当我这样称呼它时,为什么 hi 没有打印两次:
maybeTwice2(true, { println("hi"); 1+41 })
Run Code Online (Sandbox Code Playgroud)
这个例子实际上来自“Scala 中的函数式编程”一书,给出的原因是为什么“嗨”没有被打印两次对我来说不够令人信服。所以只是想在这里问这个!
这个问题是关于虚函数调用的(可能的)实现(我相信它被使用gcc
)。
考虑以下场景:
f()
D 重写B 中声明的虚方法;实例化 F 类型的对象f()
D 重写B 中声明的虚方法;实例化 F 类型的对象(这两种场景唯一的区别是B类的继承方式)
在场景 1 中,在对象 B 的 vtable 中,在目标位置处f()
现在有一个(非虚拟)thunk 表示:
如果你想调用
f()
,首先this
用offset
(实际上是D把这个thunk放在那里)
在场景 2 中,在对象 B 的 vtable 中,在指定的位置f()
现在有一个(虚拟)thunk 表示:
如果要调用
f()
,请首先将this
指针更改为存储在的值addr
this
(D无法准确告诉B指针需要调整多少,因为它不知道B对象在F对象最终内存布局中的位置)
g++ -fdump-class-hierarchy
这些假设是通过结合查看的输出来做出的g++ -S
。它们正确吗? …
我尝试了 redux 的所有命令,但它不起作用:您认为解决方案如何。这些是我试过的命令
yarn add react-redux
yarn add reduxjs / Redux-thunk#master
npm install --save Redux react-redux
npm install redux -- save
npm i redux -- save
yarn add redux-thunk
Run Code Online (Sandbox Code Playgroud)
索引.jsx
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import {createStore, applyMiddleware,compose} from 'redux';
import rootReducer from './store/reducers/rootReducer';
import {Provider} from 'react-redux';
import thunk from 'redux-thunk';
import {reduxFirestore,getFirestore} from 'redux-firestore'
import {reactReduxFirebase,getFirebase} from 'react-redux-firebase'
import fbConfig from './config/fbConfig';
const store=createStore(rootReducer,
compose(
applyMiddleware(thunk.withExtraArgument({getFirebase,getFirestore})),
reduxFirestore(fbConfig),
reactReduxFirebase(fbConfig)
)); …
Run Code Online (Sandbox Code Playgroud) 我一直遇到这个错误!
错误:期望根减速器是一个函数。相反,收到:“未定义”
我已经尝试了所有我能找到的答案都无济于事,这里是所有相关的东西!
根减速机
const createRootReducer = (history) => {
combineReducers({
router: connectRouter(history),
createUser: signupReducer,
});
};
export default createRootReducer;
Run Code Online (Sandbox Code Playgroud)
根
const Root = ({ children, initialState = {} }) => {
const history = createBrowserHistory();
const middleware = [thunk, routerMiddleware(history)];
const store = createStore(rootReducer(history), initialState, applyMiddleware(...middleware));
return (
<Provider store={store}>
<ConnectedRouter history={history}>{ children }</ConnectedRouter>
</Provider>
);
};
export default Root;
Run Code Online (Sandbox Code Playgroud)
应用程序
function App() {
return (
<div className="App">
<Root>
<ToastContainer hideProgressBar={true} newestOnTop={true} />
<Navbar />
<Landing />
<PostList /> …
Run Code Online (Sandbox Code Playgroud) 我有一个thunk
名为logInline(改编自Co文档).
我注意到thunkified get
总是似乎是yield
一个数组.这是设计的吗?它是这样thunkify
做的,还是标准的一部分yield
?
var co = require('co'),
get = thunkify(request.get);
var logInline = co(function *(){
var google = yield get('http://google.com');
console.log(google[0].statusCode);
})
logInline()
Run Code Online (Sandbox Code Playgroud)
请注意,此处的变量'google'始终是一个数组.为什么?请注意,request.get
通常返回err, response
(即没有数组).
脚本BTW返回200
google.com返回的任何其他响应代码.
唉产量文件是相当稀疏ATM.
编辑: Thunk并不总是返回数组.例如,如果var readFile = thunkify(fs.readFile);
:
var fileContents = yield readFile('myfile', 'utf8');
log(fileContents);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,fileContents不会在数组中返回.那么为什么谷歌在一个阵列里? 在thunkify中似乎有一些东西可以控制thunk返回的内容
我的商店是这样的:
export default configureStore({
reducer: {
sequencer: sequencerReducer,
editMode: editModeReducer,
tone: toneReducer,
app: appReducer,
},
middleware: (getDefaultMiddleware) => {
getDefaultMiddleware({ immutableCheck: false });
},
});
Run Code Online (Sandbox Code Playgroud)
我有一个工作thunk,但我需要这个immutableCheck: false
配置。一旦设置,它似乎会覆盖默认的中间件,并且 thunk 不再工作。这是我的想法:
export const modCell = (step, noteOn) => (dispatch, getState) => {
const selectedSound = getState().editMode.selectedSound;
dispatch(sequencerSlice.actions.toggleCell({ step, selectedSound }));
};
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
Error: Actions must be plain objects. Use custom middleware for async actions.
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
thunk ×12
redux ×4
javascript ×3
c++ ×2
node.js ×2
reactjs ×2
redux-thunk ×2
async-await ×1
c ×1
co ×1
compilation ×1
dll ×1
eslint ×1
evaluation ×1
g++ ×1
haskell ×1
loadlibrary ×1
matlab ×1
promise ×1
react-redux ×1
scala ×1
vtable ×1
yield ×1