This question already has answers here:
Swift - Seeding arc4random_uniform? Or alternative?
(2 answers)
ios Swift: looking for a cross compatible method to Java's Random() PRNG that has identical output
(3 answers)
Closed 4 years ago.
I'm writing tests in Swift 4. I want to generate thousands of rows of test input data. I want the test input data to be random.
But I want my tests to be repeatable: the same data needs to be generated for each run of all tests.
How is this achieved in Swift? In Java I've always relied on the Apache commons RandomDataGenerator. I've explored arc4random and drand48, but these don't seem to provide the repeatability I require.
Swift has no native random number generation capabilities. You can call C library functions from Swift, though. Any page on C random number library functions will describe them to you.
For example, you could just seed srand48 with the same constant every time and call drand48 to get your numbers.
(1...3).forEach {_ in
srand48(100)
(1...3).forEach {_ in print(drand48())}
}
/*
// spot the repetition:
0.25105890266514
0.208948404851498
0.940927909958315
0.25105890266514
0.208948404851498
0.940927909958315
0.25105890266514
0.208948404851498
0.940927909958315
*/
Some other C library functions are unavailable from Swift so you'd have to write that part of the code in C. No big deal.
Related
This question already has answers here:
Large Switch statements: Bad OOP?
(14 answers)
Switch statements are bad? [closed]
(8 answers)
Closed 5 years ago.
In Clean Code, Uncle Bob states that switch statements almost always break Single Responsibility and Open/Closed Principles. So does this mean that I should rarely (the only case he proposes is when switch is buried in an Abstract Factory to create polymorphic objects) use Switch Statements and therefore Enumerations? This brings the question that are Switch Statements really necessary to have in an OO Language?
Swift language for instance seems to favour enumerations (they can carry data and so on). Minimising their usage would be a major decision. Swift being an OO Language, do the same rules apply?
This question already has answers here:
Correctly Parsing JSON in Swift 3
(10 answers)
Closed 6 years ago.
I recently converted my code to Swift 3.0 and a number of errors have shown up. I have viewed other related questions to this specific error, but I have not been able to gain an understanding of what is going on/ what is wrong. I am fairly new to Swift so any explanation is appreciated. Attached is a snippet of my code.
AnyObject has become Any. You probably want this method changed to return [String : Any]. Likely your cellDescriptors array should change to Any as well; this will have some cascading changes but should get you closer to compiling.
This question already has answers here:
Arduino stepper motor control with Simulink
(3 answers)
Closed 8 years ago.
Hi im quite new in this stuff. I am trying to get an arduino board to run with a stepper motor and Simulink model but when I try to run my Simulink model on the arduino i keep getting an error that says that a .tlc file is missing. ( I am using Matlab R2013a )
My question is can someone explain in a simple way what is this .tlc file where is it used and why ?
And do I need to write one for my program to run.
I have posted in more detail my problem with the arduino in this question.
TLC (Target Language Compiler) files are used to customize the code building process. They generally come in two varieties
System/model tlc files that help in the overall model conversion process
Block level tlc files which describe how individual blocks (typically S-Functions) get converted to C code.
But writing them is a very advanced maneuver, and describing them in a short answer difficult.
You should search the doc for the term "Introduction to the Target Language Compiler" and read the links from there to get a more detailed overview.
If you're getting an error about a missing tlc file from a package that you didn't author (i.e. Arduino) then you need to go back to the authors of that package and get it.
It's not something that you're going to be able to generate yourself.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
“eval” in Scala
Dr. Subramaniam in his presentation http://www.youtube.com/watch?v=LH75sJAR0hc at min 30 when he starts talking about functional style in Scala he gives this example
class Car {
def turn(direction: String) = {
println("turning " + direction)
}
}
val car = new car
car turn "left"
and then he says that the last line could be read from a data file and evaluated. So, data becomes code, code becomes data.
How does Scala supports this? Does it have an eval function?
Pretty much every language supports an eval function (even strongly, statically typed languages like Haskell). Many language runtimes built for languages that are primarily implemented via bytecode interpretation (such as Lisp-like languages, Erlang or Java) support the ability to insert new (byte)code at runtime.
Once you can insert new code dynamically, you can write eval.
Scala is an example of such a language, where the JVM is available at runtime.
Even in language implementations without specific support for full meta-programming, or even dynamic linking, there are often ways to dynamically generate code under programmer control, either via reflection mechanisms or code generation support libraries (such as LLVM).
Beyond just a simple one-stage eval, more generally, languages that support multi-stage computation allow for generation of programs from one stage to the next, for arbitrary numbers of stages, making it possible to safely, arbitrarily nest evals.
Background reading
McCarthy, John, History of LISP, SIGPLAN Not. 1978. -- introduces eval
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Placement of the asterisk in Objective-C
What is the differenct between the following:
(Assume we have a class calculator)
Calculator* calc;
Calculator *calc;
Also what is the need for pointers? Is it not possible to omit the star?
There is no difference between those two declarations, you could also do Calculator * calc if you're feeling adventurous.
As far as if you can omit the star, no, you cannot. It is a carry over from C and shows that calc is a pointer to a Calculator, and not a Calculator itself.
There is no difference between the 2 declaration is just an preference for typing.
Every thing on computers uses pointers. You need pointers java, c#, etc use pointers.
No is not posible to omit the star.