Tutorial by Examples: code

The most straightforward approach to optimizing is by executing less code. This approach usually gives a fixed speed-up without changing the time complexity of the code. Even though this approach gives you a clear speedup, this will only give noticable improvements when the code is called a lot. R...
As Rebar3 is free, open source and written in Erlang, it's possible to simply clone and build it from the source code. $ git clone https://github.com/erlang/rebar3.git $ cd rebar3 $ ./bootstrap This will create the rebar3 script, which you can put on your PATH or link to /usr/local/bin as expl...
Knitr is an R package that allows us to intermingle R code with LaTeX code. One way to achieve this is inline code chunks. This apporach is demonstrated below. # r-noweb-file.Rnw \documentclass{article} \begin{document} This is an Rnw file (R noweb). It contains a combination of LateX and ...
Knitr is an R package that allows us to intermingle R code with LaTeX code. One way to achieve this is internal code chunks. This apporach is demonstrated below. # r-noweb-file.Rnw \documentclass{article} \begin{document} This is an Rnw file (R noweb). It contains a combination of LateX a...
If you want to extract something from a webpage (or any representation/programming language), a regex is the wrong tool for the task. You should instead use your language's libraries to achieve the task. If you want to read HTML, or XML, or JSON, just use the library that parses it properly and ser...
XSD schema (schema.xsd) The following xml schema (xsd) defines a list of users with attributes name and reputation. <?xml version="1.0"?> <xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns="http://www...
public function actionFormSubmission() { $security = new Security(); $string = Yii::$app->request->post('string'); $stringHash = ''; if (!is_null($string)) { $stringHash = $security->generatePasswordHash($string); } return $this->render('form-submi...
The utf8 pragma indicates that the source code will be interpreted as UTF-8. Of course, this will only work if your text editor is also saving the source as UTF-8 encoded. Now, string literals can contain arbitrary Unicode characters; identifiers can also contain Unicode but only word-like characte...
Android Studio's Live templates can offer quite a few shortcuts for quick logging. To use Live templates, all you need to do is to start typing the template name, and hit TAB or enter to insert the statement. Examples: logi → turns into → android.util.Log.i(TAG, "$METHOD_NAME$: $content$&q...
using System; using System.Runtime.InteropServices; namespace ComLibrary { [ComVisible(true)] public interface IMainType { int GetInt(); void StartTime(); int StopTime(); } [ComVisible(true)] [ClassInterface(ClassInterfaceType.N...
In the Xcode, you have three separate areas of working - Navigators (in red), Debug area(in green) and Utilities(in blue). The workspace window always includes the editor area. When you select a file in your project, its contents appear in the editor area, where Xcode opens the file in an appropri...
In build.sbt, make sure you include (here for Mysql and PostGreSQL): "mysql" % "mysql-connector-java" % "5.1.20", "org.postgresql" % "postgresql" % "9.3-1100-jdbc4", "com.typesafe.slick" %% "slick" % "3.1.1&...
git ls-tree -r HEAD | sed -Ee 's/^.{53}//' | \ while read filename; do file "$filename"; done | \ grep -E ': .*text' | sed -E -e 's/: .*//' | \ while read filename; do git blame --line-porcelain "$filename"; done | \ sed -n 's/^author //p' | \ sort | uniq -c | sort -rn
Shortcode is a small piece of code that can be added into the WordPress editor and will output something different once the page is published or previewed. Frequently, shortcodes are added to the theme functions.php file, but that's not a good practice as shortcodes are expected to keep working aft...
The example in Visitor Pattern provides a compelling use-case for CRTP: struct IShape { virtual ~IShape() = default; virtual void accept(IShapeVisitor&) const = 0; }; struct Circle : IShape { // ... // Each shape has to implement this method the same way ...
GetHashCode has major performance effects on Dictionary<> and HashTable. Good GetHashCode Methods should have an even distribution every integer should have a roughly equal chance of returning for a random instance if your method returns the same integer (e.g. the constant '999') for e...
var syntaxTree = CSharpSyntaxTree.ParseText( @"using System; using System.Collections; using System.Linq; using System.Text; namespace HelloWorldApplication { class Program { static void Main(string[] args) { Console.WriteLine(""Hello World""); } } }"); ...
Obfuscation is often considered as a magic solution for code protection, by making your code harder to understand if it ever gets de-compiled by hackers. But if you're thinking that removing the Log.x(..) actually removes the information the hackers need, you'll have a nasty surprise. Removing al...
X-Macros can be used for code generation, by writing repetitive code: iterate over a list to do some tasks, or to declare a set of constants, objects or functions. Here we use X-macros to declare an enum containing 4 commands and a map of their names as strings Then we can print the string values ...
Consider this example function to check if a host is up: is_alive() { ping -c1 "$1" &> /dev/null } This function sends a single ping to the host specified by the first function parameter. The output and error output of ping are both redirected to /dev/null, so the functi...

Page 8 of 21