Given any value of type Int or Long to render a human readable string:
fun Long.humanReadable(): String {
if (this <= 0) return "0"
val units = arrayOf("B", "KB", "MB", "GB", "TB", "EB")
val digitGroups = (Mat...
A common use case for extension methods is to improve an existing API. Here are examples of adding exist, notExists and deleteRecursively to the Java 7+ Path class:
fun Path.exists(): Boolean = Files.exists(this)
fun Path.notExists(): Boolean = !this.exists()
fun Path.deleteRecursively(): Bool...
With this declaration:
fun Temporal.toIsoString(): String = DateTimeFormatter.ISO_INSTANT.format(this)
You can now simply:
val dateAsString = someInstant.toIsoString()
In the simple example, we simply set the width of the rectangle to that of it's parent. Let's consider a more complicated example:
ApplicationWindow {
visible: true
width: 400
height: 640
Rectangle{
id: rect
anchors.centerIn: parent
height: 100
...
When creating Random instances with the same seed, the same numbers will be generated.
int seed = 5;
for (int i = 0; i < 2; i++)
{
Console.WriteLine("Random instance " + i);
Random rnd = new Random(seed);
for (int j = 0; j < 5; j++)
{
Console.Write(rnd.Next(...
Analog to get a collection for a Stream by collect() an array can be obtained by the Stream.toArray() method:
List<String> fruits = Arrays.asList("apple", "banana", "pear", "kiwi", "orange");
String[] filteredFruits = fruits.stream()
....
Table file with header, footer, row names, and index column:
file: table.txt
This is a header that discusses the table file
to show space in a generic table file
index name occupation
1 Alice Salesman
2 Bob Engineer
3 Charlie Janitor
This is a footer becaus...
In the following, we are creating an example of an Aurelia Custom Element which will allow you to display Youtube videos via their video ID.
An Aurelia Custom Element can be defined in two different ways:
the first one is by creating a viewmodel and accompanying view, the second one is by just cre...
A basic custom element is created in Aurelia based on naming conventions, by simply adding the suffix CustomElement to the name of a class. This suffix will automatically be stripped out by Aurelia. The remaining part of the class name will be lowercased and separated using a hyphen and can then be ...
Implicit parameters can be useful if a parameter of a type should be defined once in the scope and then applied to all functions that use a value of that type.
A normal function call looks something like this:
// import the duration methods
import scala.concurrent.duration._
// a normal method...
Similar to int but accepts only positive integers (useful for pagination when there is a page parameter.
Define:
module.config(['$urlMatcherFactoryProvider', function($urlMatcherFactory) {
$urlMatcherFactory.type('page', {
decode: function(val) { return +val; },
encode: function(val) ...
Python has only limited support for parsing ISO 8601 timestamps. For strptime you need to know exactly what format it is in. As a complication the stringification of a datetime is an ISO 8601 timestamp, with space as a separator and 6 digit fraction:
str(datetime.datetime(2016, 7, 22, 9, 25, 59, 55...