我有一个update功能,采取postID和标题来更新我的帖子.
我想循环搜索帖子以查找帖子并更新其值.我试过用List.map但我不知道放在哪里.这是我想要的伪代码:
update action model =
case action of
UpdateTitle postID title ->
//something like this:
for post in model.posts :
if post.id == postID then
{ post | title = title }
( model.posts , Effects.none )
Run Code Online (Sandbox Code Playgroud) 以下是两个看似等效的函数版本,用于从数字列表中过滤掉素数.
版本1
def prime (mylist):
for i in range(2, 8):
return filter(lambda x: x == i or x % i, mylist)
Run Code Online (Sandbox Code Playgroud)
版本2
def prime2 (mylist):
nums = mylist
for i in range(2, 8):
nums = filter(lambda x: x == i or x % i, nums)
return nums
print prime([2,3,4,5,6,7,8,9,10,11,12,13,14,15])
>> [2, 3, 5, 7, 9, 11, 13, 15]
print prime2([2,3,4,5,6,7,8,9,10,11,12,13,14,15])
>> [2, 3, 5, 7, 11, 13]
Run Code Online (Sandbox Code Playgroud)
版本1返回错误的结果.为什么?
我使用过这样的东西:
struct Node
{
char name[50];
Node *left,*right;
};
int main()
{
char cmd[10];
Node* p=NULL;
scanf("%s",&cmd);
if (p == NULL)
{
// do something
// THIS NEVER GETS EXECUTED
// WHYYYYY????
//THIS IS STRANGE
}
}
Run Code Online (Sandbox Code Playgroud)
所以基本上,p在我读入cmd变量后,指针会改变它的值.我试图注释掉scanf代码,然后一切正常.很奇怪.
我正在尝试将以下Python语句转换为C ++:
some_array = [11, 22, 33, 44]
first, rest = some_array[0], some_array[1:]
Run Code Online (Sandbox Code Playgroud)
我到目前为止所拥有的是:
int array[4] = {11, 22, 33, 44};
vector<int> some_array (array, 4);
int first = some_array.front();
vector<int> rest = some_array;
rest.erase(rest.begin());
Run Code Online (Sandbox Code Playgroud)