由买买提看人间百态

topics

全部话题 - 话题: myfunction
1 (共1页)
d****n
发帖数: 1637
1
我觉得楼主的写法没有意义。要保持global var x,有很多种不confuse 写法。
1 non-closure, since it is in web browser
var x = 0;
var myFunction = function(y){
x = x+y; return x;
};
//caller
myFunction(123)
2. closure
var makeFunction = function(x){
return function (y){ x=x+y; return x;};
}
//usage:
var myFunction = makeFunction(0);
myFunction(123);
3. oop
var myFunctionClass = function(){
this.x = 0;
this.add_y = function(){ this.x += y; return this.x};
};
//usage:
var myFunction = new myFunctionClass();
myFunction.... 阅读全帖
r****y
发帖数: 26819
2
之所以有这个想法,是因为没看懂代码。
myFunction是定义为调用一个匿名函数的结果:
var myFunction = function() {...}();
而不是定义为这个匿名函数。
myFunction(1)调用的不是myFunction的定义里的匿名函数,因为myFunction本身是
一个函数,所以myFunction(1)是在调用inner function,所以理所当然有一个参数。

should
w******p
发帖数: 166
3
myFunction encloses var x, which gets updated each time myFunction is called
this is called closure, quite popular these days with lots of programming
languages supporting it
myFunction is the result of calling the anonymous "function() { var x .. }",
therefore myFunction is indeed the inner function(y). When function(y) is
defined, it references x which is not defined within itself, but rather in
the "environment" of function(y), so it "encloses" it -- taking a snapshot
of it as {name: value} a... 阅读全帖
W***o
发帖数: 6519
4
I don't think there's anything wrong with calling myFunction(1), because OP
writes the code a little bit off (but still works), which should be an
immediately executed function like this:
var myFunction = (function() {
var x = 0;
return function(y) {
x = x + y;
return x;
}
}());
myFunction will receive the returned anonymous inner function, so you can
call myFunction(
1)

should
h*****g
发帖数: 9
5
When I used gdb debug C++ program, I encountered the follow problem.
Source code: myprogram.cc
executable code: myprogram
I used CC compiler to compile.
****************************
> gdb myprogram
> break myprogram.cc:myfunction
(Here, myfunction is a function in myprogram.cc)
Function "myfunction" not defined.
****************************
So the problem is that gdb always gives me "Function "myfunction" not defined"
message when I try to set a breakpoint at that function.
Is there anyone can
C****0
发帖数: 53
6
最近需要做一个多线程的unit test,但是总是有问题,所以不是很清楚是我的方法有问
题还是gmock不支持多线程。
大体是这样的
1.原来的class是一个QThread的subclass:
class aSubClassOfQthread: public QThread{
...
...
public:
int myFunction();
}
2.然后我用gmock mock一个aSubClassOfQthread
3.测试
TEST(SubClassOfQthread,test1){
aSubClassOfQthread thread1;
EXPECT_CALL(thread1.myFunction()).Times(1); // 期望Call 一次
int check1 = thread1.myfunction(); // 看下返回值
EXPECT_EQ(1, check1); // 看下返回值和预期是否一样
}
问题是 EXPECT_EQ(1, check1) 报错,说返回的是int function的 default value 0.
就算我在my... 阅读全帖
d*****u
发帖数: 17243
7
来自主题: Programming版 - C++ 什么时候用 "new" ?
myObj->myFunction()实际上是*(myObj).myFunction()的缩写
如果你定义了myClass类型的变量,不能用->访问成员
只能用 myObj.myFunction()
G*****9
发帖数: 3225
8
来自主题: Programming版 - Urgent question: AIX, C++11, Shared Object
I am using AIX. I cannot generate .so file
Code:
#include
using namespace std;
#ifdef __cplusplus
extern "C" { // only need to export C interface if
// used by C++ source code
#endif
bool Myfunction( );
#ifdef __cplusplus
}
#endif
bool Myfunction( )
{
std::uniform_real_distribution U(0.0, 1.0);
std::mt19937_64 rnd;
rnd.seed(1);
return true;
}
command: g++ -std=c++0x -maix64 -c -Wall -ansi -g -fPIC test.cc
Error:
test.cc: In function 'bool Myfunctio... 阅读全帖
n******1
发帖数: 3756
9
这里可以吗?

第一次输出1,第二次输出2
有人解释一下吗?
Y**G
发帖数: 1089
10
检查一下alert("getX() is called");应该只显示一次。
h*****g
发帖数: 9
11

Yeah, I did.
I guess it might be because I used template when I declared the function.
********************
template
void myfunction(Itemtype& first, Itemtype& second);
********************
After I took off the template, gdb can find the "myfunction" when the program
is compiled by g++. But if I use CC to compile, gdb gave the same message
"Funciton myfunction not defined"
So I don't know how to make it work for template.
Also, I don't understand why CC doesn't work for this.
T
b******t
发帖数: 965
12
来自主题: JobHunting版 - leetcode 这题insert interval怎么做?
写个C++的
bool myfunction(Interval a, Interval b) {
return (a.start < b.start);
}
class Solution {
public:
vector insert(vector &intervals, Interval
newInterval) {
// Start typing your C/C++ solution below
// DO NOT write int main() function

intervals.insert(lower_bound(intervals.begin(),intervals.end(),
newInterval,myfunction),newInterval);
vector result;
if(intervals.size()>0)
result.push_back(inter... 阅读全帖
b******t
发帖数: 965
13
来自主题: JobHunting版 - leetcode 这题insert interval怎么做?
写个C++的
bool myfunction(Interval a, Interval b) {
return (a.start < b.start);
}
class Solution {
public:
vector insert(vector &intervals, Interval
newInterval) {
// Start typing your C/C++ solution below
// DO NOT write int main() function

intervals.insert(lower_bound(intervals.begin(),intervals.end(),
newInterval,myfunction),newInterval);
vector result;
if(intervals.size()>0)
result.push_back(inter... 阅读全帖
E*******0
发帖数: 465
14
来自主题: JobHunting版 - 实现next_permutation
// NextPermutation.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
using namespace std;
#include
bool myfunction (int i,int j) { return (i>j); }
bool nextPermutation(vector &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
//If vector size is 1, permutation is its self.
if (1==num.size())
return true;
// rit is the iterator indicates current ite... 阅读全帖
s***a
发帖数: 34
15
来自主题: Immigration版 - 探讨eb1b文章引用问题 (转载)
【 以下文字转载自 I140 讨论区 】
发信人: myfunction (myfunction), 信区: I140
标 题: 探讨eb1b文章引用问题
发信站: BBS 未名空间站 (Thu Jul 24 05:19:51 2014, 美东)
我看了看eb1b的要求,只需要申明2项:
5. 在国际发行的期刊上发表过学术文章,或曾著有学术书籍;
6. 作为评委或裁判,参与评审过他人作品,或担任过学术期刊的评委或审稿人;
不知道我的理解是否正确,由于文章引用并不多(最近两年刚发),且感觉引用不是自
己努力就可以实现的,自己可以控制的是文章的发表,和审稿。 文章发表可以多写嘛
,多搞就出来了,审稿更是要脸皮厚就行,不知道我这理解对不对? 只要我多写几篇
文章,多审稿子,eb1b就能通过?
看板上申请eb1b的人不多,大多是申请eb1a的,为什么?这样看来eb1b更容易呀。
大家给评评?
l****t
发帖数: 36289
16
来自主题: Joke版 - 也问一个父母的问题 (转载)
【 以下文字转载自 Family 讨论区 】
发信人: myfunction (myfunction), 信区: Family
标 题: 也问一个父母的问题
发信站: BBS 未名空间站 (Tue Jul 22 07:18:52 2014, 美东)
情况介绍:
母亲60多了,年级也不小,又有高血压,家里有2个姐姐和我。 按照四川的风俗来说呢
,一般是男孩养父母,但是现在母亲一个人在国内,再加上我们小孩也出生了,本想打
算让她过来看看孩子,可是麻烦事情也很多呢。
母亲是那种农村妇女,没有受过什么教育,再加上年级大了,毛病很多。 脾气很坏
,根本不太会为孩子考虑(也许想,但是不得法),就拿昨天的事情来说吧,打算让她
过来了,票也买好了,这个月过来,叫她带点东西,跟她和姐姐说了多次,东西比较多
,去买个纸箱子,用透明胶带一缠就好了,她和姐姐搞了2天,然后告诉我说,搞不定
,非说要用个熟料口袋装,这大老远的,这么点事情我就不相信有那么难吗?后来还是
我在网上买上一个纸箱邮寄到她那里。 关键老婆也不理解,老婆不喜欢我妈妈,为了
母亲这个问题,家里都吵了无数回了,我在中间真的很为难。
... 阅读全帖
l********a
发帖数: 1154
17
传入外部变量指针即可
char *testvar;
void myfunction(char *gVar)
{
char *temp;
temp = "a";
gVar = temp;
}
调用
myfunction(testvar);
s***1
发帖数: 49
18
来自主题: Programming版 - C++ 什么时候用 "new" ?
Yea, I meant to say MyClass *myObj = new MyClass(12);
I realized the difference, stack VS heap.
If I declare:
MyClass *myObj = new MyClass(12);
I can access a member function by: myObj->myFunction().
But if I declare
MyClass myObj (12);
What is the syntax for accessing the member function? Is it still myObj->
MyFunction() ?
x******a
发帖数: 6336
19
来自主题: Programming版 - 请教for_each
为什么下面这个程序输出的时候多一个1出来:
enter the number of element
5
4 0 3 0 4 1
the maximum element is 4
0 0 3 4 4
程序在这
#include
#include
#include
using namespace std;
void myfunction(int i)
{
cout << i<<" ";
}
int main()
{
int N;
cout<< "enter the number of element" < cin>> N;
int a[N];
srand((unsigned) time(0));
for(int i=0; i a[i]=rand()%N;
cout< cout<< endl;
cout<<"the maximum element is " << *max_element(a, a+N)... 阅读全帖
n******1
发帖数: 3756
20
谢谢
The shared environment is created in the body of an anonymous function,
which is executed as soon as it has been defined.
http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Dept
http://www.yuiblog.com/blog/2007/06/12/module-pattern/
写成这样可能比较好,虽然我还是很迷惑
myFunction = (function() {
var x = 0;
return function(y) {
x = x + y;
return x;
}
})();

called
",
code
myFunction)
w*****e
发帖数: 721
21
It is a new "fashion", but I think it will be hard to maintain after some
years for someone else.
Try to keep the code clean and easy to understand is also important.
As for LZ, the anonymous myFunction does not take an argument, so you should
not call it as myFunction(1)
f***a
发帖数: 329
22
来自主题: Statistics版 - 【欢迎进来讨论】for loop in R
照例,还是我先胡说几句,:-)
在R里面能不用for loop就不应该用,尽量用vectorize的方式搞定一切。
对matrix/data.frame的row or col做运算,就用apply;(btw, same for array)
要对list, data.frame(essentially it is a list), vector的element做运算就用
lapply, sapply;
对不同id做运算,用tapply
下面是我的问题。
1)
# Way I:
for(i in 1:n){
res[i] <- myfunction(a[i], b[i], c[i])
}
# Way II:
res <- apply(cbind(a,b,c), 1, function(t)
myfunction(t[1], t[2], t[3])
)
这两种方法equivalent还是way II好一些呢?
2)
# Way I:
for(i in 1:n){
input <- i
...... # some heavy calculation
res[i] <- output
}
... 阅读全帖
c*****t
发帖数: 1879
23
来自主题: BuildingWeb版 - javascript Q
I think the following may work

The key is to cancel event bubble to disable default event (here, return
false).
n****t
发帖数: 27
24
char *testvar;
void myfunction(char fname[])
{
char *temp;
temp = testvar;
temp = "a";
}
我想实现的是在这个函数里给temp赋一个值,然后传到testvar(定义在函数之外)。但
是我发现在这个函数里我改变不了testvar的值。有人遇到个这种问题吗?谢谢。
a**********3
发帖数: 88
25
我在perl monks上面发帖没人理我,只好来这里碰碰运气,不知道哪位大牛可以帮忙解
答一下,跪谢。。。。
我想从perl里面call一个c subroutine,比如一个叫do_nothing(double *)的函数,
想对perl里面一个array做些计算。
目前我的c code是这样:
double do_nothing(double *para){
return para[0];
}
我的xs是这么写的:
double do_nothing(x)
SV * x
然后用过make之后,我的test fail掉了。我的test file:
my @array = (1.1,1.2);
use Test::More tests => 1;
is(&myfunction::do_nothing(\@array),1.1);
拿到的结果是:
got '4.56882265335234e-317', expected '1.1'.
我是哪里弄错了?
m**c
发帖数: 90
26

The communication between JSP and JS is one-way. You can only access JSP
information in JS, but not the other way:

There are two ways to solve your problem:
1. Use "processSel()" function to submit a form so that page can be
regenerated. If you don't want use to see the form is submitted, you can use
s*****i
发帖数: 355
27
来自主题: Java版 - 再问一个今天的面试题
关于java reflection的
Suppose class ClassName immplements runnable interface. Write a function
which returns a thread that attached with this class, taking the class name
string as argument.
我这么写的:
public Thread myFunction (String s) {
Thread t=null;

try {
Object o = Class.forName(s).newInstance();
t = new Thread((Runnable)o);
}
catch(Exception e ){
t*********2
发帖数: 32
28
no, my case is:
An exception specification with an empty throw, as in
void MyFunction(int i) throw(){
}
tells the compiler that the function does not throw any exceptions.
y***d
发帖数: 2330
29
来自主题: Programming版 - 请教for_each
cout< ^^^^
Y**G
发帖数: 1089
30
of course

var x = 0;
return function(y) {
x = x + y;
return x;
}

is only called once to compute myFunction, so "var x = 0" is only executed
once.
n*****t
发帖数: 22014
31
改成 function myFunction 再试试
k**n
发帖数: 3989
32
来自主题: Programming版 - 这个代码啥意思c#
用interface与继承时会出现这种需要cast的情况。
大概就下面这意思
1)
MyFunction(IMoveable aMoveableObject) {
if (aMoveableObject is ICar)
ICar myCar = (ICar)aMoveableObject;
...
}
也有人爱写成
2)
ICar myCar= aMoveableObject as ICar
if(myCar!=null){
..
}
代码1)比2)在各方面都好一点。
g***l
发帖数: 2753
33
来自主题: Programming版 - 问个node.js的问题
不是这个问题。我把第一个form改成
http://10.4.4.222:6789" onsubmit="myFunction()">
"jasmineStarted">


然后JScode改成下面的还是发不出去。form的值是被更新了,console.log也触发了,
就是提交不了。
jasmineStarted: function(suiteInfo) {
console.log('Running suite with ' + suit... 阅读全帖
1 (共1页)