我有一个带有身体的宏:
(defmacro blah [& body] (dostuffwithbody))
Run Code Online (Sandbox Code Playgroud)
但是我也想为它添加一个可选的关键字参数,所以在调用它时可能看起来像这样:
(blah :specialthingy 0 body morebody lotsofbody)
(blah body morebody lotsofboy)
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?请注意,我正在使用Clojure 1.2,所以我也使用新的可选关键字参数解构.我天真地尝试这样做:
(defmacro blah [& {specialthingy :specialthingy} & body])
Run Code Online (Sandbox Code Playgroud)
但显然这并不好.我怎样才能完成这个或类似的东西?
val m: java.util.Map[String, Int] = ...
m.foreach { entry =>
val (key, value) = entry
// do stuff with key and value
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来解构Map.Entry?我尝试了以下,但它没有编译:
m.foreach { (key, value) =>
// do stuff with key and value
}
Run Code Online (Sandbox Code Playgroud) 我已经定义了一个采用地图的函数.我想用解构来访问这些值.但是,我还想检查是否有任何使用过的密钥.
所以,例如......
(defun func1 [{:keys [a b c] :rest rest}]
(println a b c)
(println rest))
(func1 {:a 1 :b 2 :c 3 :d 4})
Run Code Online (Sandbox Code Playgroud)
哪个会打印
1 2 3
4
Run Code Online (Sandbox Code Playgroud)
我想要这个的原因是,如果rest不为null,这可能是一个错误,我想发出信号.我知道:as,我可以使用.但后来我需要存储两次有效密钥列表.
我错过了什么吗?
菲尔
嵌套解构是否可能?
例如,我想要数组中的第一项,并在该数组中我想要该子数组的第一项.
鉴于:
let arr = [['foo'], ['bar']];
Run Code Online (Sandbox Code Playgroud)
有没有更简单的方法:
let firstItem = arr[0][0];
Run Code Online (Sandbox Code Playgroud) 在C#7中,显然不可能只用一个项目来构造一个元组.
ValueTuple<T1> 存在,所以不是因为那个.
向后兼容性意味着Deconstruct一个参数的方法也必须是合法的:
public void Deconstruct(out int i)
Run Code Online (Sandbox Code Playgroud)
那你为什么不写:
var (num) = foo;
Run Code Online (Sandbox Code Playgroud)
这简直就是没有合理的用例吗?
假设我在es6中调用了这个语法:
let a, b;
{a, b} = { a: 100, b: 300 };
Run Code Online (Sandbox Code Playgroud)
代码将无错误地运行;
但是让我们改写是这样的:
function fn() {
return { a: 100, b: 200 }
}
let a, b;
{ a, b } = fn();
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码时,它会显示意外的令牌"="; 我有点困惑,有什么区别?
我想为我的移动应用程序添加背景,但是当我使用“this.props.children”时,eslint 说我“必须使用解构道具分配”。为什么我可以解构这个道具?
这是我的代码,
export default class WallPaper extends Component {
render() {
return (
<ImageBackground
source={backgroundimg}
imageStyle={{ opacity: 0.9 }}
style={styles.container}
>
{this.props.children}
</ImageBackground>
);
}
}Run Code Online (Sandbox Code Playgroud)
当我使用此代码时
export default class WallPaper extends Component {
render() {
const { children } = this.props;
return (
<ImageBackground
source={backgroundimg}
imageStyle={{ opacity: 0.9 }}
style={styles.container}
>
{children}
</ImageBackground>
);
}
}Run Code Online (Sandbox Code Playgroud)
当我使用此代码时,
export default class WallPaper extends Component {
render() {
(this.props) => {
return (
<ImageBackground
source={backgroundimg}
imageStyle={{ opacity: 0.9 }} …Run Code Online (Sandbox Code Playgroud)在下面的函数中,我得到了带有属性的textarea对象current.
这里,嵌套的解构与Start和End变量一起工作.但current变量不起作用.
function someFunction({ current: { selectionStart: Start, selectionEnd: End } }, AppStateSetter) {
// do something with current, Start, and End
}
Run Code Online (Sandbox Code Playgroud) 这不一定是一个问题,更多的是通过ESLint错误产生的好奇心,这让我想知道是否有更好的方法只是禁用此行的ESLint.
请考虑下面的代码段.如果启用了反应/解构分配规则,则ESLint将给出错误
const { arrayToPrint } = myArrays 至 const arrayToPrint = myArrays[arrayName]
我的问题是,我无法找到任何参考,所以我猜不是,有没有办法移动[arrayName]到赋值的左侧进行destructure而不引用实际的对象属性?
const myArrays = {
arrayOne: ['one'],
arrayTwo: ['two'],
arrayThree: ['three'],
}
const arrayPrinter = function arrayPrinter(arrayName) {
const arrayToPrint = myArrays[arrayName]
return arrayToPrint
}
console.log(arrayPrinter('arrayTwo'))Run Code Online (Sandbox Code Playgroud)
我可以做一个赋值解构:
a, b = s.split(' ', 1)
Run Code Online (Sandbox Code Playgroud)
对于s包含多个单词的字符串。
我们如何使用 Python 3.8 中引入的最新赋值表达式(是否可能有多个目标)在 an ifor 中做同样的事情elif?
我试过:
if some_thing:
# some code.
elif (a, b := s.split(' ', 1)) and some_func(a) and some_func(b):
# some code probably using a and b as well.Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
elif (a, b := s.split(' ', 1)) and some_func(a) and some_func(b):
NameError: name 'a' is not defined
Run Code Online (Sandbox Code Playgroud)
我想要这个的原因是因为如果我的第一个条件得到满足,我不想不必要地拆分我的字符串。
destructuring ×10
javascript ×4
reactjs ×3
clojure ×2
c# ×1
c#-7.0 ×1
closures ×1
ecmascript-6 ×1
eslint ×1
macros ×1
node.js ×1
python ×1
python-3.8 ×1
python-3.x ×1
react-native ×1
react-ref ×1
scala ×1