我有这个功能:
(defn movies [directors]
(->> directors
(mapcat :movies)
(group-by :genre)))
给了我这个输出:
{"Action" [{:title "Blade Runner 2049", :genre "Action", :year 2017}],
"Drama"
[{:title "Gladiator", :genre "Drama", :year 2000}
{:title "Apocalypse Now", :genre "Drama", :year 1979}
{:title "The Departed", :genre "Drama", :year 2006}
{:title "The Aviator", :genre "Drama", :year 2004}],
"Adventure" [{:title "Mars", :genre "Adventure", :year 2015}],
"Crime"
[{:title "American Gangster", :genre "Crime", :year 2007}
{:title "The Godfather", :genre "Crime", :year 1972}
{:title "GoodFellas", :genre "Crime", :year 1990}],
"Comedy"
[{:title "Jack", :genre "Comedy", :year 1996}
{:title "The Wolf", :genre "Comedy", :year 2013}]}
我想在相同功能“电影”中使用减少或计数来计算每个类别中的电影数量
谢谢, R上。
答案 0 :(得分:2)
(frequencies
(sequence (comp (mapcat :movies)
(map :genre))
directors))
答案 1 :(得分:1)
这只是计算每个:genre
组中的数字:
(map #((juxt key (comp count val)) %) your-current-output)
;; => (["Action" 1] ["Drama" 4] ["Adventure" 1] ["Crime" 3] ["Comedy" 2])
所以对你的功能:
(defn movies [directors]
(->> directors
(mapcat :movies)
(group-by :genre)
(map #((juxt key (comp count val)) %))))
答案 2 :(得分:0)
你真的需要在那里使用import UIKit
class Line {
var startPoint:CGPoint
var endPoint:CGPoint
init (start:CGPoint , end:CGPoint) {
startPoint = start
endPoint = end
}
}
class AnnotationView: UIView {
static internal let nibName = "AnnotationView"
@IBOutlet weak var imageView: UIImageView!
var lines :[Line] = []
var lastPoint:CGPoint!
let drawingLayer = CAShapeLayer()
override func awakeFromNib() {
super.awakeFromNib()
layer.addSublayer(drawingLayer)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
lastPoint = touches.first?.location(in: self)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first
{
let newPoint = touch.location(in:self)
lines.append(Line(start: lastPoint, end: newPoint))
lastPoint = newPoint
updateDrawingOverlay()
}
}
func updateDrawingOverlay() {
let path = CGMutablePath()
for line in lines {
path.move(to: line.startPoint)
path.addLine(to: line.endPoint)
}
drawingLayer.frame = imageView.frame
drawingLayer.path = path
drawingLayer.lineWidth = 5
drawingLayer.strokeColor = UIColor.black.cgColor
setNeedsDisplay()
}
}
吗?因为您可以使用reduce
和map
的组合来计算项目:
frequencies