我想了解在clojure中对不同大小的集合进行操作的惯用方法。有没有办法告诉函数'map'用一些默认值填充集合的其余部分?
举个例子,假设我有3个向量:
(def x [1 2 3 4])
(def y [1 2 3 4 5])
(def z [1 2 3 4 5 6 7])
(map + x y z) ; yields (3 6 9 12)
在这种情况下,如何用零填充x和y并获得此收益:
(3 6 9 12 10 6 7)
答案 0 :(得分:12)
map
不会自行完成,但您可以使用concat
和repeat
的组合来获得所需的结果:
(def x [1 2 3 4])
(def y [1 2 3 4 5])
(def z [1 2 3 4 5 6 7])
(map +
(concat x (repeat 0))
(concat y (repeat 0))
z) ; => (3 6 9 12 10 6 7)
这里有一个草图,说明如何将它抽象出来,所以你不需要知道哪个集合最长。 (在上面的代码段中,如果concat
(repeat 0)
所有集合都有(defn map-longest
[f default & colls]
(lazy-seq
(when (some seq colls)
(cons
(apply f (map #(if (seq %) (first %) default) colls))
(apply map-longest f default (map rest colls))))))
(map-longest +
0
[1 2 3 4]
[1 2 3 4 5]
[1 2 3 4 5 6 7]) ; => (3 6 9 12 10 6 7)
,那么您将拥有无限序列。
{{1}}
您可以看到其他几种方法作为this previous question on Stack Overflow的答案。
答案 1 :(得分:0)
您可以通过以下功能合并长度可变的地图矢量:
constructor(props)
{
super(props)
this.state = {
show: false,
opticare: false
}
this.handleSwitch = this.handleSwitch.bind(this)
this.leave = this.leave.bind(this)
this.handleOpti = this.handleOpti.bind(this)
this.handleCare = this.handleCare.bind(this)
}
handleSwitch = () => {
this.setState({
show: !this.state.switch
})
}
leave = () => {
this.setState({
show: this.state.switch
})
}
handleOpti = () => {
this.setState({
opticare: !this.state.opticare
})
}
handleCare = () => {
this.setState({
opticare: this.state.opticare
})
}
render()
{
let className = 'reading-friends'
if (this.state.show) {
className = 'reading-friends visible'
} else if (!this.state.show) {
className = 'reading-friends invisible'
}
let addClass = 'opti-care'
if (this.state.opticare) {
addClass = 'opti-care visible'
} else if (!this.state.opticare) {
addClass = 'opti-care invisible'
}
return (
<div>
<div>
<div className="row ">
<div className="row ">
<div className={className} style={{ textAlign: 'center' }}>
<h1 className="heading" style={{
fontSize: '50px',
fontWeight: 'bold',
marginTop: '140px',
marginBottom: '200px',
fontFamily: 'catamaran,sans-serif'
}}>Reading Friends</h1>
<p className="parah">Reading Friends is designed to engage young children by
promoting interactive learning through games, puzzles,<br/>
and music. Appealing to children's instinctual inquisitiveness,
Reading Friends brings education to life with exciting graphics,<br/>
spirited sound and creative activities
that help them learn to read, while entertaining them through play.</p>
</div>
</div>
</div>
<div className="row d-flex justify-content-center">
<div style={{ textAlign: 'center' }} className={addClass}>
<h1 style={{
fontSize: '50px',
fontWeight: 'bold',
marginBottom: '200px',
fontFamily: 'catamaran,sans-serif'
}}>Opticare Solution</h1>
<p>OptiCare Solution is a complete mini ERP for opticians and optometrists.<br/>
We are the first to bring such an extensive solution in the field of Optometry,<br></br>
providing features that are robust and easy to use.</p>
</div>
</div>
<div className="row"></div>
</div>
<div style={{ marginTop: '270px' }} className="row ">
<div className="col-lg-3 col-sm-3 col-3 d-flex justify-content-center">
<a href="https://play.google.com/store/apps/details?id=com.planetreading.readingfriends">
<img onMouseOut={this.leave} onMouseOver={this.handleSwitch}
src="http://newstate.io/wp-content/uploads/2019/07/work-reading-friends-colored.png"
alt="" class="we-do-img we-work-img img-responsive grayscale"/>
</a>
</div>
<div className="col-lg-3 col-sm-3 col-3 d-flex justify-content-center">
<img onMouseOut={this.handleCare} onMouseOver={this.handleOpti}
src="http://newstate.io/wp-content/uploads/2019/07/work-opticare-colored.png"
alt="" class="we-do-img we-work-img img-responsive grayscale"/>
</div>
</div>
</div>
)
}
此功能使解决方案更加通用,可以采用任意长度的地图矢量。