Is it possible to match N elements with a coffeescript splat? - coffeescript

Is it possible to specify how many elements a splat should match? Something like:
foo = [1, 2, 3, 4, 5, 6]
[firstThree...(3), fourth, rest...] = foo
console.log firstThree // [1, 2, 3]
console.log forth // 4
console.log rest // [5, 6]

As far as I know there is no way of adding a limit to the amount of arguments a splat can take.
But you can use ranges(search for range in the Loops and Comprehensions Docs) to get a similar syntax in your destructuring assignment:
foo = [1, 2, 3, 4, 5, 6]
[firstThree, fourth, rest] = [foo[0..2], foo[3], foo[4..-1]]
firstThree
# => [1, 2, 3]
fourth
# => 4
rest
# => [5, 6]

Related

Why isn't there a formSubtracting() method in Swift for Sets

In Swift there is a Form... equivalent for the Sets methods intersection(), symmetricDifference() and union(), i.e. formIntersection(), formSymmetricDifference() and formUnion().
But for the method subtracting() there is no method called formSubtracting. Does anyone know why this is so, because it seams I now have to use something like mySet = mySet.subtracting(anotherSet)
subtract(_:) is what you are looking for:
Removes the elements of the given set from this set.
Example:
var mySet: Set = [1, 2, 3, 4, 5]
let anotherSet : Set = [2, 4, 6, 8]
mySet.subtract(anotherSet)
print(mySet) // [3, 1, 5]
There is also a variant which takes another sequence (of the same element type) as the argument, e.g. an array:
var mySet: Set = [1, 2, 3, 4, 5]
let anotherSequence = [2, 4, 6, 8]
mySet.subtract(anotherSequence)
print(mySet) // [3, 1, 5]

RxSwift - How to concatenate observables of collection

I'm trying to concatenate two observables of collection with concat() operator, however, it's not working as expected.
I've two observables:
let first = Observable<[Int]>.create { observer in
observer.onNext([1, 2])
observer.onCompleted()
return Disposables.create()
}
let second = PublishSubject<[Int]>()
Using concat():
let items = Observable.concat([first, second])
items.subscribe(onNext: {
print($0)
})
second.onNext([3, 4, 5])
Output:
[1, 2]
[3, 4, 5]
What I want:
[1, 2]
[1, 2, 3, 4, 5]
So you don't just want to concat two observables, you also want to concat the arrays that are in the events that are produced by the two observables. You aren't going far enough to get what you want.
Imagine you had two Array<[Int]> instead of two Observable<[Int]>. Concat-ing the two (as in arr1 + arr2) would not produce [[1, 2], [1, 2, 3, 4, 5]], instead it would produce [[1, 2], [3, 4, 5]]. Your Observables are behaving the same way.
To both concat and combine them, you need scan, as in:
let items = Observable.concat([first, second])
.scan([], accumulator: +)
Which will produce two events:
[1, 2]
[1, 2, 3, 4, 5]
FYI, with arrays, we don't have a scan operator, but we can approximate it with reduce. For arrays, it would be:
let arr1: Array<[Int]> = [[1, 2]]
let arr2: Array<[Int]> = [[3, 4, 5]]
let itemsArr = (arr1 + arr2).reduce([], { result, element in
return result + [(result.last ?? []) + element]
})
print(itemsArr)

Adding lists by element in pyspark

I'd like to take a RDD of integer lists and reduce it down to one list. For example...
[1, 2, 3, 4]
[2, 3, 4, 5]
to
[3, 5, 7, 9]
I can do this in python using the zip function but not sure how to replicate it in spark besides doing collect on the object but I want to keep the data in the rdd.
If all elements in rdd are of the same length, you can use reduce with zip:
rdd = sc.parallelize([[1,2,3,4],[2,3,4,5]])
rdd.reduce(lambda x, y: [i+j for i, j in zip(x, y)])
# [3, 5, 7, 9]

Remove list element at index?

There is a serious lack of methods available to lists in Swift. It is really disappointing, coming from a Python background. For example, I want to remove the first element, something like this would work in Python:
mylist = mylist[1:]
How do I remove an element from a list (preferably by index, but I can do whatever method is easiest)?
Use removeAtIndex
var arr = [1, 2, 3]
arr.removeAtIndex(1)
If you want to remove a range of values, you can use removeRange:
var x = [1, 2, 3, 4, 5]
x.removeRange(1...2) // result is [1, 4, 5]
var x = [1, 2, 3, 4, 5]
x.removeRange(1..<2) // result is [1, 3, 4, 5]
Note that this method doesn't check for range bounds, so if you specify a range outside the array size, it will throw a runtime exception.

Object range with conditions

In groovy I can write
def n = 10
print 1..<n
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Are there other language that allow to specify range with conditions?
examples
def n = 10
print 1<=..n
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def n = -2
print 1<=..n
Output: [1]
def n = -2
print 1..n
Output: [1, 0, -1, -2]
Python has the range() method which does a similar thing. While it does not use operators for the condition you can specify a start value, stop value and step value. It then creates a list containing all values starting with the start value, then start+step, ... until it reaches the end value (which is not included).