Tutorial by Examples: c

Partial functions are often used to define a total function in parts: sealed trait SuperType case object A extends SuperType case object B extends SuperType case object C extends SuperType val pfA: PartialFunction[SuperType, Int] = { case A => 5 } val pfB: PartialFunction[SuperType,...
While partial function are often used as convenient syntax for total functions, by including a final wildcard match (case _), in some methods, their partiality is key. One very common example in idiomatic Scala is the collect method, defined in the Scala collections library. Here, partial functions ...
Scala has a special type of function called a partial function, which extends normal functions -- meaning that a PartialFunction instance can be used wherever Function1 is expected. Partial functions can be defined anonymously using case syntax also used in pattern matching: val pf: PartialFunction...
Partial functions are very common in idiomatic Scala. They are often used for their convenient case-based syntax to define total functions over traits: sealed trait SuperType // `sealed` modifier allows inheritance within current build-unit only case object A extends SuperType case object B exten...
Some JavaScript engines (for example, the current version of Node.js and older versions of Chrome before Ignition+turbofan) don't run the optimizer on functions that contain a try/catch block. If you need to handle exceptions in performance-critical code, it can be faster in some cases to keep the ...
To trim whitespace from the edges of a string, use String.prototype.trim: " some whitespaced string ".trim(); // "some whitespaced string" Many JavaScript engines, but not Internet Explorer, have implemented non-standard trimLeft and trimRight methods. There is a proposa...
Use .slice() to extract substrings given two indices: var s = "0123456789abcdefg"; s.slice(0, 5); // "01234" s.slice(5, 6); // "5" Given one index, it will take from that index to the end of the string: s.slice(10); // "abcdefg"
All JavaScript strings are unicode! var s = "some ∆≈ƒ unicode ¡™£¢¢¢"; s.charCodeAt(5); // 8710 There are no raw byte or binary strings in JavaScript. To effectively handle binary data, use Typed Arrays.
To detect whether a parameter is a primitive string, use typeof: var aString = "my string"; var anInt = 5; var anObj = {}; typeof aString === "string"; // true typeof anInt === "string"; // false typeof anObj === "string"; // false If you ev...
This demonstrates how to print each element of a Map val map = Map(1 -> "a", 2 -> "b") for (number <- map) println(number) // prints (1,a), (2,b) for ((key, value) <- map) println(value) // prints a, b This demonstrates how to print each element of a list val l...
var MyDict = new Dictionary<string,T>(StringComparison.InvariantCultureIgnoreCase)
The Scala compiler will automatically convert methods into function values for the purpose of passing them into higher-order functions. object MyObject { def mapMethod(input: Int): String = { int.toString } } Seq(1, 2, 3).map(MyObject.mapMethod) // Seq("1", "2", &...
using System.Runtime.InteropServices; class PInvokeExample { [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern uint MessageBox(IntPtr hWnd, String text, String caption, int options); public static void test() { MessageBox(IntPtr.Zero,...
If you want to check that a string contains only a certain set of characters, in this case a-z, A-Z and 0-9, you can do so like this, import re def is_allowed(string): characherRegex = re.compile(r'[^a-zA-Z0-9.]') string = characherRegex.search(string) return not bool(string) ...
In certain situations, data declarations can be performed inline. LOOP AT lt_sflight INTO DATA(ls_sflight). WRITE ls_sflight-carrid. ENDLOOP.
DATA begda TYPE sy-datum.
DATA: begda TYPE sy-datum, endda TYPE sy-datum.
A Map is a basic mapping of keys to values. Maps are different from objects in that their keys can be anything (primitive values as well as objects), not just strings and symbols. Iteration over Maps is also always done in the order the items were inserted into the Map, whereas the order is undefine...
The ToDictionary() LINQ method can be used to generate a Dictionary<TKey, TElement> collection based on a given IEnumerable<T> source. IEnumerable<User> users = GetUsers(); Dictionary<int, User> usersById = users.ToDictionary(x => x.Id); In this example, the single ar...
Grand Central Dispatch works on the concept of "Dispatch Queues". A dispatch queue executes tasks you designate in the order which they are passed. There are three types of dispatch queues: Serial Dispatch Queues (aka private dispatch queues) execute one task at a time, in order. They a...

Page 124 of 826