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

Stuff that inspire you to create.

See also: ▙ @LitMind
Download Telegram
#Programming_paradigms : #Array_programming (also vector or multidimensional)

Generalizes operations on scalars to apply transparently to vectors, matrices, and higher-dimensional arrays.

Is used in scientific and engineering settings.

e.g. APL, J, Fortran, #Ada, #MATLAB, #Perl Data Language (PDL) and the NumPy extension to #Python.

Vectorized operation
Operations applied at once to an entire set of values like arrays; regardless of whether it is executed on a vector processor or not.

Function rank
Analogous to tensor rank in mathematics
Functions that operate on data may be classified by the number of dimensions they act on.

• Ordinary multiplication, for example, is a scalar ranked function because it operates on zero-dimensional data (individual numbers).

• The cross product operation is an example of a vector rank function because it operates on vectors, not scalars.

• Matrix multiplication is an example of a 2-rank function, because it operates on 2-dimensional objects (matrices).

Collapse operators reduce the dimensionality of an input data array by one or more dimensions. For example, summing over elements collapses the input array by 1 dimension.
#Python
Attribute versus Property
An attribute with a __get__, __set__, or __delete__ method is a property.

Properties
Are created by putting the @property decorator above a method defenition. This means that when the instance attribute with the same name as the method is accessed, the method will be called instead.
#Python
Classes
A class is a new type of object which can have instances.

An instance has:
• Attributes — for maintaining its state, defined by its constructor
• Methods — for modifying its state, defined by its class

🔥 Methods are actually attributes. More specifically, class attributes.

Definition
class ClassName:
classAttribute = value
def __init__(self, a):
self.a = a


⚠️ All methods must have self as their first parameter.
⚠️ Instances inherit class attributes upon construction.

Instantiation
x = ClassName(a)

⚠️ Classes are created at runtime and can be modified after creation.

Terminology
• base class = parent class
• derived class = child class
• derive = inherit
• attribute = data member
• method = function member


Inheritance
• Multiple base classes are allowed.
• The derived class can override any methods of its base classes. i.e. all member functions are virtual and can be overridden.

class Pizza(Food):

Polymorphism
• A method can call the method of a base class with the same name.

Incapsulation
• members are normally public except Private Variables
GoTo
- is a statement that performs a one-way transfer of control to another point of code; in contrast to a function call which returns control.

Its use has declined significantly since the advent of structured programming in the 1960s. Structured programming languages like #Pascal introduced control structures such as: subroutines, loops and multiway branch for clarity and efficiency. These replace equivalent flows written using gotos and ifs.

Language support:
#C
#CSharp: also makes case and default statements labels, whose scope is the enclosing switch statement; goto case or goto default is often used to replace explicit "fall-through", which C# disallows.
#Perl
#PHP there was no native support for goto until version 5.3
#Java: goto is a reserved word, but is unusable.
#Python: does not support it but there are several joke modules that provide it.

The structured program theorem proves that goto is not necessary to write programs.
#Python
How to install a module

python -m pip install SomePackage

Normally, if a suitable module is already installed, attempting to install it again will have no effect. Upgrading existing modules must be requested explicitly:

python -m pip install --upgrade SomePackage
#toRead

📚 Controlling the Keyboard and Mouse with GUI Automation
#Python

https://automatetheboringstuff.com/chapter18/
#Python
The Zen of Python

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
#Python
Syntax

Definition:
def f(args):

Conditional
if expression:
⚠️ Use == to check for equality, not is.

Iteration:
for i in iterable:

Inside a function definition, use the following syntax to include the global variables in its scope:
def f(args):
global names
stuff
#Python
How to send GET or POST requests

import requests
r = requests.get(url)
r = requests.post(url, data=data)

r = r.json()
Exceptions
Events that occur during the execution of a program and disrupt the normal flow. An exception is an object that represents an error.

When raised, exceptions must be handled immediately, otherwise they would terminate the execution. If you have some suspicious code that may raise an exception, you can defend your program by placing it in a try block, followed by a except block which handles the problem as elegantly as possible.

try:
You do your operations here;
......................
except ExceptionI:
If there is ExceptionI, then execute this block.
except ExceptionII:
If there is ExceptionII, then execute this block.
......................
else:
If there is no exception then execute this block.


The list of all #Python exceptions:
https://docs.python.org/3/library/exceptions.html
The diamond problem
- is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If there is a method in A that B and C have overridden, and D does not override it, then which version of the method does D inherit: that of B, or that of C?

For example, in the context of GUI software development, a class Button may inherit from both classes Rectangle (for appearance) and Clickable (for functionality/input handling), and classes Rectangle and Clickable both inherit from the Object class. Now if the equals method is called for a Button object and there is no such method in the Button class but there is an overridden equals method in Rectangle or Clickable (or both), which method should be eventually called?

How different languages deal with it:
• Common #Lisp: by order "...in the order in which parent classes are named in the subclass definition"
• Curl: "and the secondary constructor will be invoked for all other subclasses."
• Eiffel: "Eiffel will automatically join features together, if they have the same name and implementation."
• Go: compile-time error
#Java: compile-time error
#OCaml: by order "...are inherited in the same order, with each newly inherited method overriding any existing methods."
#Perl: by order "...from as an ordered list. The compiler uses the first method it finds..."
#Python: by order
#Ruby: by order "...as rightmost depth first resolution."
#Scala: by order "allows multiple instantiation of traits, which allows for multiple inheritance by adding a distinction between the class hierarchy and the trait hierarchy. A class can only inherit from a single class, but can mix-in as many traits as desired." This approach is the most similar to #Nile's; traits being qualifications.
• Tcl: by order "the order of specification in the class declaration affects the name resolution for members..."

Languages that allow only single inheritance, where a class can only derive from one base class, do not have the diamond problem.

Moreover, languages such as #Ada, Objective-C, #CSharp, #Delphi/Free #Pascal, Java, #Swift and PHP allow multiple-inheritance of interfaces (called protocols in Objective-C and Swift). Interfaces are like abstract base classes that specify method signatures without implementing any behavior.

When several interfaces declare the same method signature, as soon as that method is implemented (defined) anywhere in the inheritance chain, it overrides any implementation of that method in the chain above it (in its superclasses). Hence, at any given level in the inheritance chain, there can be at most one implementation of any method. Thus, single-inheritance method implementation does not exhibit the Diamond Problem even with multiple-inheritance of interfaces.
#TalkingSemicolon

To use all/any, you need to construct a new iterator. You can override their behavior:

def __and__(self, other): return False

which also affects all.

#Python
#TalkingSemicolon
Tools for natural language generation
Either Keras/TensorFlow if going with #Python or TorchNN if going with #Lua.
Nothing else really works.
Blocks
- are lexical structures that allow many statements to be treated as one. A language that allows blocks and nested blocks, is called block-structured. Blocks are fundamental to #structured programming.

As scopes
Depending on the language, certain distinguished blocks may be treated as lexical scopes; otherwise, identifiers assigned in outer blocks are visible inside inner blocks, unless shadowed.

Syntax
• Free-form
Whitespace only delimits tokens and has no other significance
begin ... end: #ALGOL, #Pascal
{ ... }: #C, #Perl, #JS, #Nile
( keyword ... ): #Lisp

• Off-side rule
Indentation groups blocks of code
e.g. #Python, #Haskell, #Cobra, #CoffeeScript

Limitations
In some languages blocks do not fully support all declarations; for instance many C-derived languages do not permit nested functions.
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
#Python
Iterables versus iterators
An iterable is something with an __iter__ method;
An iterator is the result of calling iter() on an iterable.
All iterators can only be iterated over once, not just those produced by generator functions.

Generators
When you call a function that contains a yield statement, you get a generator object, but no code runs. Then each time you extract an object from the generator, Python executes code in the function until it comes to a yield statement, then pauses and delivers the object. When you extract another object, your code will continue from where it left off i.e. it resumes just after the yield and continues until it reaches another yield (often the same one, but one iteration later). This continues until the function runs off the end, at which point the generator is deemed exhausted.
#Python
The filter function takes an iterable and an predicate, i.e. a function that returns a boolean, and removes items that don't match that predicate i.e. return False.
#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
Quine
- is a non-empty computer program which produces a copy of its own source code as its only output, and cannot receive any form of input, including reading a file, which means a quine is considered to be "cheating" if it looks at its own source code. Quines are possible in any Turing complete programming language, as a direct consequence of Kleene's recursion theorem.

A quine in #Python:
s = 's = %r\nprint(s%%s)'
print(s%s)
#Java
The JVM
- is the detailed, formal specification for a 32-bit abstract machine. Abstract implies hardware and OS-independent and thus is interoperable across platform-dependent implementations. It operates on abstracte data types rather the native data types of any ISA:
• 32-bits types: integers, floats, and references are called primitives
• 64-bits types: long and double are supported but consume two units of storage
• smaller types: boolean, byte, short and char are extended to 32-bit ints

It loads code, verifies it, and executes it. It has instructions for:
• Load and store
• Arithmetic
• Type conversion
• Object creation and manipulation
• Operand stack management (push & pop)
• Control transfer (branching)
• Method invocation and return
• Throwing exceptions
• Monitor-based concurrency

The specification includes various dynamic features such as GC and thread management.

JDK
- is an software development kit for Java and consists of:
• a private stand-alone implementation of the JVM such as JRE's HotSpot
• the Java standard library, JCL
• an interpreter/loader (java)
• a compiler (javac)
• an archiver (jar)
• a documentation generator (Javadoc)

HotSpot
- relies on JIT; and divides the memory into generations:
• young generation: heap for short-lived objects
• old generation: heap for long-lived objects
• permanent generation: used for class definitions and associated metadata

JVM languages
Any language that can express a valid class file. The existing languages includes ports from other languages:
• JRuby (#Ruby)
• Jython (#Python)
And entirely new languages that compile to Java bytecode:
• Clojure
• Apache Groovy
#Scala
#Kotlin

JVM languages are compatible with each other i.e. libraries of one can be used with programs of another. A new feature will supports dynamically typed languages in the JVM, and is developed within the Da Vinci Machine project whose mission is to extend the JVM so that it supports languages other than Java.