ComputerScientist
174 subscribers
14 photos
3 files
206 links
▜ The Inventor

Stuff that inspire you to create.

See also: ▙ @LitMind
Download Telegram
“you make prototype objects, and then … make new instances. Objects are mutable in JavaScript, so we can augment the new instances, giving them new fields and methods. These can then act as prototypes for even newer objects. We don't need classes to make lots of similar objects… Objects inherit from objects. What could be more object oriented than that?”
Prototypal inheritance in JavaScript, Douglas Crockford (#JavaScript)
Object-oriented programming in Lua
Although Lua does not have a built-in concept of classes, object-oriented programming can be achieved using two language features: first-class functions and tables. By placing functions and related data into a table, an object is formed. Inheritance (both single and multiple) can be implemented using the metatable mechanism, telling the object to look up nonexistent methods and fields in parent object(s).

There is no such concept as "class" with these techniques; rather, prototypes are used, similar to Self or #JavaScript. New objects are created either with a factory method (that constructs new objects from scratch) or by cloning an existing object.

Lua provides some syntactic sugar to facilitate object orientation. To declare member functions inside a prototype table, one can use function table:func(args), which is equivalent to function table.func(self, args). Calling class methods also makes use of the colon: object:func(args) is equivalent to object.func(object, args).

#Lua
Data layout
- is how multidimensional arrays are stored in a linear storage such as RAM. It is critical for:
• correctly passing arrays between programs written in different programming languages
• performance when traversing an array because modern CPUs, due to caching, process sequential data more efficiently than non-sequential data
• contiguous access makes it possible to use SIMD instructions that operate on vectors of data

Row-major versus column-major order
The difference between the orders lies in which elements of an array are contiguous in memory. In a row-major order, the consecutive elements of a row reside next to each other, whereas the same holds true for consecutive elements of a column in a column-major order. While the terms allude to the rows and columns of a two-dimensional array, the orders can be generalized to arrays of any dimension.

Transposition
As exchanging the indices of an array is the essence of array transposition, an array stored as row-major but read as column-major (or vice versa) will appear transposed. As actually performing this rearrangement in memory is typically an expensive operation, some systems provide options to specify individual matrices as being stored transposed.

Languages support
• Row-major: #C/C++/Objective-C (for C-style arrays), PL/I, #Pascal, Speakeasy, SAS, and Rasdaman
• Column-major: #Fortran, #MATLAB, GNU Octave, S-Plus, #R, #Julia, and Scilab.
• Neither (for less dense arrays):
• Iliffe vectors: #Java, #Scala, #Swift. #Ruby, #Perl, #PHP, #JavaScript, Visual Basic .NET
• Lists of lists: #Python, Wolfram Language of Wolfram Mathematica
• Tables of tables: #Lua
#regex
Regex Cheatsheet
All the rules apply to all of the three languages: #Python, #Perl (PCRE) and #JavaScript, unless stated otherwise.
Source: debuggex.com

Basics
.: Any character except newline
a: The character a
ab: The string ab
a|b: a or b
a*: 0 or more a's
\: Escapes a special character

Quantifiers
*: 0 or more
+: 1 or more
?: 0 or 1
{2}: Exactly 2
{2, 5}: Between 2 and 5
{2,}: 2 or more
(,5}: Up to 5 (Python only)
⚠️ Default is greedy. Append ? for reluctant.

Groups
(...): Capturing group
(?:...): Non-capturing group
\Y: Match the Y'th captured group
Python and PCRE:
(?P<Y>...): Capturing group named Y
(?P=Y): Match the named group Y
(?#...): Comment
PCRE only:
(?>...): Atomic group
(?|...): Duplicate group numbers
(?R): Recurse into entire pattern
(?Y): Recurse into numbered group Y
(?&Y): Recurse into named group Y
\g{Y}: Match the named or numbered group Y
\g<Y>: Recurse into named or numbered group Y

Character Classes
[ab-d]: One character of: a, b, c, d
[^ab-d]: One character except: a, b, c, d
[\b]: Backspace character
\d: One digit
\D: One non-digit
\s: One whitespace
\S: One non-whitespace
\w: One word character
\W: One non-word character

Assertions
^: Start of string
$: End of string
\b: Word boundary
\B: Non-word boundary
(?=...): Positive lookahead
(?!...): Negative lookahead
Python and PCRE:
\A: Start of string, ignores m flag
\Z: End of string, ignores m flag
(?<=...): Positive lookbehind
(?<!...): Negative lookbehind
(?()|): Conditional
PCRE:
\G: Start of match

Flags
i: Ignore case
m: ^ and $ match start and end of line
Python and PCRE:
s: . matches newline as well
x: Allow spaces and comments
L: Locale character classes
u: Unicode character classes
(?iLmsux): Set flags within regex
JavaScript only:
g: Global Match

Special Characters
\n: Newline
\r: Carriage return
\t: Tab
\YYY: Octal character YYY
\xYY: Hexadecimal character YY
JavaScript and PCRE:
\0: Null character
\cY: Control character Y
Hexadecimal character YY (\uYY for JavaScript and \x{YY} for PCRE)

Replacement
Python only:
\g<0>: Insert entire match
\g<Y>: Insert match Y (name or number)
\Y: Insert group numbered Y
JavaScript only:
$$: Inserts $
$&: Insert entire match
$`: Insert preceding string
$': Insert following string
$Y: Insert Y'th captured group

Escapes
PCRE only:
\Q..\E: Remove special meaning

POSIX Classes
PCRE only:
[:alnum:]: Letters and digits
[:alpha:]: Letters
[:ascii:]: Ascii codes 0 - 127
[:blank:]: Space or tab only
[:cntrl:]: Control characters
[:digit:]: Decimal digits
[:graph:]: Visible characters, except space
[:lower:]: Lowercase letters
[:print:]: Visible characters
[:punct:]: Visible punctuation characters
[:space:]: Whitespace
[:upper:]: Uppercase letters
[:word:]: Word characters
[:xdigit:]: Hexadecimal digits