I was looking at the code for how to remove duplicates from a list and I came upon some syntax I am unfamiliar with. What does $1 ++ $0 mean?
extension List where Element: Hashable {
func removeDuplicates() -> List {
var set = Set<Element>()
let list = reduce(List()) {
guard !set.contains($1) else { return $0 }
set.insert($1)
return $1 ++ $0
}
return list.reversed()
}
}
$0
is the first parameter passed into the closure.
$1
is the second parameter.
++
is a custom infix operator
infix operator ++
private func ++<Element>(element: Element, list: ListNode<Element>) -> ListNode<Element> {
return list.insert(element: element)
}
It will insert the element on the right to list on right of the operator
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.