Tutorial by Examples: c

Inject and reduce are different names for the same thing. In other languages these functions are often called folds (like foldl or foldr). These methods are available on every Enumerable object. Inject takes a two argument function and applies that to all of the pairs of elements in the Array. For...
Discriminated unions in F# offer a a way to define types which may hold any number of different data types. Their functionality is similar to C++ unions or VB variants, but with the additional benefit of being type safe. // define a discriminated union that can hold either a float or a string type...
To apply a function to every item in an array, use array_map(). This will return a new array. $array = array(1,2,3,4,5); //each array item is iterated over and gets stored in the function parameter. $newArray = array_map(function($item) { return $item + 1; }, $array); $newArray now is a...
You can access the elements of an array by their indices. Array index numbering starts at 0. %w(a b c)[0] # => 'a' %w(a b c)[1] # => 'b' You can crop an array using range %w(a b c d)[1..2] # => ['b', 'c'] (indices from 1 to 2, including the 2) %w(a b c d)[1...2] # => ['b'] (indic...
Here is an example of a service that greets people by the given name, and keeps track of how many users it encountered. See usage below. %% greeter.erl %% Greets people and counts number of times it did so. -module(greeter). -behaviour(gen_server). %% Export API Functions -export([start_link/0...
Standard Allocation The C dynamic memory allocation functions are defined in the <stdlib.h> header. If one wishes to allocate memory space for an object dynamically, the following code can be used: int *p = malloc(10 * sizeof *p); if (p == NULL) { perror("malloc() failed");...
If the variable contains a value of an immutable type (e.g. a string) then it is okay to assign a default value like this class Rectangle(object): def __init__(self, width, height, color='blue'): self.width = width self.height = height self.color = color d...
Run-length encoding captures the lengths of runs of consecutive elements in a vector. Consider an example vector: dat <- c(1, 2, 2, 2, 3, 1, 4, 4, 1, 1) The rle function extracts each run and its length: r <- rle(dat) r # Run Length Encoding # lengths: int [1:6] 1 3 1 1 2 2 # valu...
One of the first questions people have when they begin to use PowerShell for scripting is how to manipulate the output from a cmdlet to perform another action. The pipeline symbol | is used at the end of a cmdlet to take the data it exports and feed it to the next cmdlet. A simple example is using...
There are two main ways way to install Ansible on OS X, either using the Homebrew or Pip package manager. If you have homebrew, the latest Ansible can be installed using the following command: brew install ansible To install Ansible 1.9.X branch use following command: brew install homebrew/ver...
The OR (||) operator returns true if one of its two operands evaluates to true, otherwise it returns false. For example, the following code evaluates to true because at least one of the expressions either side of the OR operator is true: if (10 < 20) || (20 < 10) { print("Expression...
type person = {Name: string; Age: int} with // Defines person record member this.print() = printfn "%s, %i" this.Name this.Age let user = {Name = "John Doe"; Age = 27} // creates a new person user.print() // John Doe, 27
type person = {Name: string; Age: int} // Defines person record let user1 = {Name = "John Doe"; Age = 27} // creates a new person let user2 = {user1 with Age = 28} // creates a copy, with different Age let user3 = {user1 with Name = "Jane Doe"; Age = 29} //creates ...
When Fortran was originally developed memory was at a premium. Variables and procedure names could have a maximum of 6 characters, and variables were often implicitly typed. This means that the first letter of the variable name determines its type. variables beginning with i, j, ..., n are intege...
The ForEach-Object cmdlet works similarly to the foreach statement, but takes its input from the pipeline. Basic usage $object | ForEach-Object { code_block } Example: $names = @("Any","Bob","Celine","David") $names | ForEach-Object { "H...
MKLocalSearch allows users to search for location using natural language strings like "gym". Once the search get completed, the class returns a list of locations within a specified region that match the search string. Search results are in form of MKMapItem within MKLocalSearchResponse ob...
The filter or map functions should often be replaced by list comprehensions. Guido Van Rossum describes this well in an open letter in 2005: filter(P, S) is almost always written clearer as [x for x in S if P(x)], and this has the huge advantage that the most common usages involve predicates tha...
You may wish to use Model Factories within your seeds. This will create 3 new users. use App\Models\User; class UserTableSeeder extends Illuminate\Database\Seeder{ public function run(){ factory(User::class)->times(3)->create(); } } You may also want to define ...
Artisan is a utility that can help you do specific repetitive tasks with bash commands. It covers many tasks, including: working with database migrations and seeding, clearing cache, creating necessary files for Authentication setup, making new controllers, models, event classes, and a lot more. ...
The following is the original "Hello, World!" program from the book The C Programming Language by Brian Kernighan and Dennis Ritchie (Ritchie was the original developer of the C programming language at Bell Labs), referred to as "K&R": K&R #include <stdio.h> ma...

Page 84 of 826