Faster way to create an array of numbers spanning a range in Swift [duplicate] - swift

This question already has answers here:
Is there a way to instantly generate an array filled with a range of values in Swift?
(4 answers)
Closed 6 years ago.
Is there a shorter way to create an array of numbers spanning a range in swift?
Right now, I'm using this:
var arrayOfInts = [UInt32]()
for number in 1...100 {
arrayOfInts.append(number)
}
Is there a one-line way of doing it?

var arrayOfInts = Array(1...100)
Playground Output

Is this short enough?
let array = Array(1...100)

Try like this
var z = [Int](1...100)
print(z)
DEMO

Related

How to create an array by multiplying another array in a functional way? [duplicate]

This question already has answers here:
Repeating array in Swift
(4 answers)
Closed 5 years ago.
I'd like to create an array that contains elements from another array multiplied by some Int value.
Example:
the following code
let arr = [1,2,3]
let multiplier = 3
print(function(arr, multiplier))
should return
[1,2,3,1,2,3,1,2,3]
I know how to make it using nested for loops, but I'm looking for some nifty functional way. I was thinking about map() function, but it iterates over each element of a given array, which is not my use case I suppose.
Main idea:
Create array of arrays,
flatMap to one-dimensional array.
Example:
let arr = [1, 2, 3]
let multiplayer = 3
print(Array(repeating: arr, count: multiplayer).flatMap({ $0 }))

How to quickly check if an Integer lies within a given range [duplicate]

This question already has answers here:
Can I use the range operator with if statement in Swift?
(6 answers)
Closed 6 years ago.
I have an integer x and I want to check if it lies between a given boundary / within a given range.
The straightforward approach would be
let contains = x > lowerBounds && x < higherBounds
Is there a more swifty approach to this?
You can create a range and check if it contains x:
let contains = (lowerBounds...upperBounds).contains(x)
e.g.:
let successful = (200..<300).contains(httpStatusCode)
Or you can use the pattern matching operator:
let contains = lowerBounds...uppperBounds ~= x

How to create an array with incremented values in Swift? [duplicate]

This question already has answers here:
Is there a way to instantly generate an array filled with a range of values in Swift?
(4 answers)
Closed 7 years ago.
I know that I can create an array with repeated values in Swift with:
var myArray = [Int](count: 5, repeatedValue: 0)
But is there a way to create an array with incremented values such as [0, 1, 2, 3, 4] other than to do a loop such as
var myArray = [Int]()
for i in 0 ... 4 {
myArray.append(i)
}
I know that code is pretty straightforward, readable, and bulletproof, but it feels like I should be able pass some function in some way to the array as it's created to provided the incremented values. It might not be worth the cost in readability or computationally more efficient, but I'm curious nonetheless.
Use the ... notation / operator:
let arr1 = 0...4
That gets you a Range, which you can easily turn into a "regular" Array:
let arr2 = Array(0...4)

How do I sort a list of Doubles in Scala? [duplicate]

This question already has answers here:
How do I sort an array in Scala?
(7 answers)
Closed 8 years ago.
How do I sort a simple list of Doubles in Scala?
var dubs = List(1.3,4.5,2.3,3.2)
I think my question may not have accurately reflected my specific problem, since I realize now that dubs.sorted will work just fine for the above. My problem is as follows, I have a string of doubles "2.3 32.4 54.2 1.33" that I'm parsing and adding to a list
var numsAsStrings = l.split("\\s");
var x = List(Double);
var i = 0;
for( i <- 0 until numsAsStrings.length) {
x :+ numsAsStrings(i).toDouble;
}
So, I would think that I could just call x.sorted on the above, but that doesn't work... I've been looking over the sortBy, sorted, and sortWith documentation and various posts, but I thought the solution should be simpler. I think I'm missing something basic, regardless.
Use the sorted method
dubs.sorted // List(1.3, 2.3, 3.2, 4.5)

Is there an easier way to get the lesser of two values in Swift [duplicate]

This question already has answers here:
Swift equivalent for MIN and MAX macros
(5 answers)
Closed 8 years ago.
I'd like to assign the lesser of two values to a variable. In Ruby I would do something like:
my_var = [value_one, value_two].min
In Swift, of course, I can do this:
var myVar = 0.0
if valueOne < valueTwo {
myVar = valueOne
} else {
myVar = valueTwo
}
But, I'm wondering if there is a cleaner, more succinct solution.
var myVar = min(valueOne, valueTwo)
min is a standard library function that takes the lesser of two (or least of several — it's variadic) Comparable values.