我有一个列表,例如:
["Hello", "Goodbye"]
Run Code Online (Sandbox Code Playgroud)
我想map在名单上使用;
我之前成功地使用了地图:
f = ("example" ++)
Run Code Online (Sandbox Code Playgroud)
那么:
map f ["Hello", "Goodbye"]
Run Code Online (Sandbox Code Playgroud)
列出清单:
["exampleHello", "exampleGoodbye"]
Run Code Online (Sandbox Code Playgroud)
但是如何在函数"f"中使用列表项呢?
例如,如果我想重复list元素,那么:
["Hello", "Goodbye"]
Run Code Online (Sandbox Code Playgroud)
会成为:
["HelloHello", "GoodbyeGoodbye"]
Run Code Online (Sandbox Code Playgroud)
我怎么能用map和函数'f'(和++)来做呢?
非常感谢,
例子:
for x in iterable1:
expression
Run Code Online (Sandbox Code Playgroud)
该map形式是:
map(lambda x: expression, iterable1)
Run Code Online (Sandbox Code Playgroud)
如何仅使用map和不使用列表推导式将其扩展到嵌套 for 循环?
例子:
for x in itr1:
for y in itr2:
expr
Run Code Online (Sandbox Code Playgroud) Javascript 有 anArray.prototype.map但似乎没有与集合等效的东西。在 Javascript 中在集合上运行地图的推荐方法是什么?
编辑:根据要求添加示例
users = new Set([1,2,3])
// throws an error:
// Uncaugh TypeError: users.map is not a function
users.map(n => n*n)
Run Code Online (Sandbox Code Playgroud) 我有 4 个按钮,每个按钮都有名称 id 和选定的布尔标志。
我想要实现的是,在单击按钮时,应该更改该特定按钮的布尔按钮标志。为此,我需要在 map 函数中为该特定按钮 Id 设置状态。
我的问题是我无法在地图函数中为那个特定的点击按钮设置状态,它的 btnSelected 应该改变
我的目标是创建一个多选取消选择按钮。它是用户感兴趣的选择,并基于此反映 UI 以及我的数组。这是我的代码。
感谢期待。
import React, { Component } from "react";
import { Redirect } from "react-router-dom";
export default class Test extends Component {
constructor(props, context) {
super(props, context);
this.handleChange = this.handleChange.bind(this);
this.state = {
value: "",
numbers: [1, 2, 3, 4, 5],
posts: [
{
id: 1,
topic: "Animal",
btnSelected: false
},
{
id: 2,
topic: "Food",
btnSelected: false
},
{
id: 3,
topic: "Planet",
btnSelected: …Run Code Online (Sandbox Code Playgroud) 我经历了理查德·伯德的"哈斯克尔功能的思考"一书,并且存在在那里他证明了滤波方法的属性我无法理解的部分.他证明的是:
filter p . map f = map f . filter (p . f)
Run Code Online (Sandbox Code Playgroud)
在本书的前面,他将过滤器定义为:
filter p = concat . map (test p)
test p x = if p x then [x] else []
Run Code Online (Sandbox Code Playgroud)
这就是他证明第一个等式的方法:
filter p . map f
= {second definition of filter} -- He's referring to the definition I gave above
concat . map (test p) . map f
= {functor property of map}
concat . map (test p . f)
= {since test p . f …Run Code Online (Sandbox Code Playgroud) 我想使用地图和/或过滤器功能编写此代码。它返回列表中项目的索引,前提是总和达到目标
我已经为此使用列表理解,但是看不到如何将第二个for循环获取到map / filter函数中。我不确定使用的语法如果我为map / filter函数的function参数定义自己的函数
num = [2,5,7,11,6,15,3,4]
tgt= 9
[num.index(x) for x in num for y in num if x + y == tgt]
Run Code Online (Sandbox Code Playgroud)
结果:
[0, 1, 2, 4, 6, 7]
Run Code Online (Sandbox Code Playgroud) data={data.map(({ ID,filePath,accountId,companyId,['First Name'], ...rest }) => rest)}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,名字是一个带空格的键,显然当按上述方式传递时会导致错误。如何处理这种情况?
使用列表Maybe a,如何过滤并仅获取列表中不是的元素Nothing?
-- input
pas = [Just 3, Just 1, Just 5, Just 9, Nothing, Just 10, Nothing] :: [Maybe Int]
-- expected output
new_pas = [3, 1, 5, 9, 10]
Run Code Online (Sandbox Code Playgroud)
我尝试了不同的使用方式map和查看方式,mapMaybe但找不到正确的组合。
我有以下代码给出[2,4,6]:
j :: [Int]
j = ((\f x -> map x) (\y -> y + 3) (\z -> 2*z)) [1,2,3]
Run Code Online (Sandbox Code Playgroud)
为什么?似乎只使用了“z 函数”,“y 函数”会发生什么?map在这种特殊情况下如何工作?
我是 Haskell 的新手,正在研究 Collatz 猜想问题。所需的输出是从给定整数变为 1 所需的步数。这是我第一次不得不使用该Maybe类型,这可能会导致我的困惑。
我有这个工作解决方案基于另一个我发现的相同问题的解决方案:
collatz :: Integer -> Maybe Integer
collatz n
| n <= 0 = Nothing
| n == 1 = Just 0
| even n = fmap succ . collatz $ n `div` 2
| otherwise = fmap succ . collatz $ 3 * n + 1
Run Code Online (Sandbox Code Playgroud)
我不清楚的是为什么有必要fmap succ在这种情况下使用。根据我目前的理解,我希望能够调用succ递归调用的输出collatz以增加它;但是,这会引发错误:
> No instance for (Enum (Maybe Integer))
arising from a use of `succ'
Run Code Online (Sandbox Code Playgroud)
它看起来像错误有事情做与调用succ一个 …