Tutorial by Examples: c

The dot function can also be used to compute vector dot products between two one-dimensional numpy arrays. >>> v = np.array([1,2]) >>> w = np.array([1,2]) >>> v.dot(w) 5 >>> np.dot(w,v) 5 >>> np.dot(v,w) 5
Since functions are ordinary values, there is a convenient syntax for creating functions without names: List.map (fun x -> x * x) [1; 2; 3; 4] (* - : int list = [1; 4; 9; 16] *) This is handy, as we would otherwise have to name the function first (see let) to be able to use it: let square x...
class Plane { enum Emergency: ErrorType { case NoFuel case EngineFailure(reason: String) case DamagedWing } var fuelInKilograms: Int //... init and other methods not shown func fly() throws { // ... if fuelInKilograms ...
const int a = 0; /* This variable is "unmodifiable", the compiler should throw an error when this variable is changed */ int b = 0; /* This variable is modifiable */ b += 10; /* Changes the value of 'b' */ a += 10; /* Throws a compiler error */ The const qualif...
If while working you realize you're on wrong branch and you haven't created any commits yet, you can easily move your work to correct branch using stashing: git stash git checkout correct-branch git stash pop Remember git stash pop will apply the last stash and delete it from the stash list. T...
process.argv is an array containing the command line arguments. The first element will be node, the second element will be the name of the JavaScript file. The next elements will be any additional command line arguments. Code Example: Output sum of all command line arguments index.js var sum = 0...
Pointer assignments do not copy strings You can use the = operator to copy integers, but you cannot use the = operator to copy strings in C. Strings in C are represented as arrays of characters with a terminating null-character, so using the = operator will only save the address (pointer) of a str...
Here we make a simple echo websocket using asyncio. We define coroutines for connecting to a server and sending/receiving messages. The communcations of the websocket are run in a main coroutine, which is run by an event loop. This example is modified from a prior post. import asyncio import ai...
Install cx_Freeze from here Unzip the folder and run these commands from that directory: python setup.py build sudo python setup.py install Create a new directory for your python script and create a "setup.py" file in the same directory with the following content: application_title ...
A singleton is a pattern that restricts the instantiation of a class to one instance/object. Using a decorator, we can define a class as a singleton by forcing the class to either return an existing instance of the class or create a new instance (if it doesn't exist). def singleton(cls): i...
Using vanilla mathematics: var from:Point = new Point(300, 10); var to:Point = new Point(75, 40); var a:Number = to.x - from.x; var b:Number = to.y - from.y; var distance:Number = Math.sqrt(a * a + b * b); Using inbuilt functionality of Point: var distance:Number = to.subtract(from).len...
var degrees:Number = radians * 180 / Math.PI;
var radians:Number = degrees / 180 * Math.PI;
A whole circle is 360 degrees or Math.PI * 2 radians. Half of those values follows to be 180 degrees or Math.PI radians. A quarter is then 90 degrees or Math.PI / 2 radians. To get a segment as a percentage of a whole circle in radians: function getSegment(percent:Number):Number { retur...
You can test whether a point is inside a rectangle using Rectangle.containsPoint(): var point:Point = new Point(5, 5); var rectangle:Rectangle = new Rectangle(0, 0, 10, 10); var contains:Boolean = rectangle.containsPoint(point); // true
In addition to predicates functioning as specs, you can register a spec globally using clojure.spec/def. def requires that a spec being registered is named by a namespace-qualified keyword: (clojure.spec/def ::odd-nums odd?) ;;=> :user/odd-nums (clojure.spec/valid? ::odd-nums 1) ;;=> tru...
clojure.spec/and & clojure.spec/or can be used to create more complex specs, using multiple specs or predicates: (clojure.spec/def ::pos-odd (clojure.spec/and odd? pos?)) (clojure.spec/valid? ::pos-odd 1) ;;=> true (clojure.spec/valid? ::pos-odd -3) ;;=> false or works similarl...
You can spec a record as follows: (clojure.spec/def ::name string?) (clojure.spec/def ::age pos-int?) (clojure.spec/def ::occupation string?) (defrecord Person [name age occupation]) (clojure.spec/def ::person (clojure.spec/keys :req-un [::name ::age ::occupation])) (clojure.spec/valid? ...
You can spec a map by specifying which keys should be present in the map: (clojure.spec/def ::name string?) (clojure.spec/def ::age pos-int?) (clojure.spec/def ::occupation string?) (clojure.spec/def ::person (clojure.spec/keys :req [::name ::age ::occupation])) (clojure.spec/valid? ::perso...
You can spec collections in a number of ways. coll-of allows you to spec collections and provide some additional constraints. Here's a simple example: (clojure.spec/valid? (clojure.spec/coll-of int?) [1 2 3]) ;; => true (clojure.spec/valid? (clojure.spec/coll-of int?) '(1 2 3)) ;; => tru...

Page 258 of 826