summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--bash.html.markdown4
-rw-r--r--kotlin.html.markdown73
-rw-r--r--pascal.html.markdown44
3 files changed, 117 insertions, 4 deletions
diff --git a/bash.html.markdown b/bash.html.markdown
index be879538..cda877ad 100644
--- a/bash.html.markdown
+++ b/bash.html.markdown
@@ -228,10 +228,13 @@ mv s0urc3.txt dst.txt # sorry, l33t hackers...
# Since bash works in the context of a current directory, you might want to
# run your command in some other directory. We have cd for changing location:
cd ~ # change to home directory
+cd # also goes to home directory
cd .. # go up one directory
# (^^say, from /home/username/Downloads to /home/username)
cd /home/username/Documents # change to specified directory
cd ~/Documents/.. # still in home directory..isn't it??
+cd - # change to last directory
+# => /home/username/Documents
# Use subshells to work across directories
(echo "First, I'm here: $PWD") && (cd someDir; echo "Then, I'm here: $PWD")
@@ -256,6 +259,7 @@ print("#stderr", file=sys.stderr)
for line in sys.stdin:
print(line, file=sys.stdout)
EOF
+# Variables will be expanded if the first "EOF" is not quoted
# Run the hello.py Python script with various stdin, stdout, and
# stderr redirections:
diff --git a/kotlin.html.markdown b/kotlin.html.markdown
index 86d1baa0..6ff7c1dc 100644
--- a/kotlin.html.markdown
+++ b/kotlin.html.markdown
@@ -65,7 +65,7 @@ fun helloWorld(val name : String) {
A template expression starts with a dollar sign ($).
*/
val fooTemplateString = "$fooString has ${fooString.length} characters"
- println(fooTemplateString) // => My String Is Here! has 18 characters
+ println(fooTemplateString) // => My String Is Here! has 18 characters
/*
For a variable to hold null it must be explicitly specified as nullable.
@@ -175,12 +175,12 @@ fun helloWorld(val name : String) {
// Objects can be destructured into multiple variables.
val (a, b, c) = fooCopy
println("$a $b $c") // => 1 100 4
-
+
// destructuring in "for" loop
for ((a, b, c) in listOf(fooData)) {
println("$a $b $c") // => 1 100 4
}
-
+
val mapData = mapOf("a" to 1, "b" to 2)
// Map.Entry is destructurable as well
for ((key, value) in mapData) {
@@ -347,6 +347,8 @@ fun helloWorld(val name : String) {
println(EnumExample.A) // => A
println(ObjectExample.hello()) // => hello
+
+ testOperator()
}
// Enum classes are similar to Java enum types.
@@ -376,6 +378,71 @@ throws an exception if the value is null.
*/
var b: String? = "abc"
val l = b!!.length
+
+/* You can add many custom operations using symbol like +, to particular instance
+by overloading the built-in kotlin operator, using "operator" keyword
+
+below is the sample class to add some operator, and the most basic example
+*/
+data class SomeClass(var savedValue: Int = 0)
+
+// instance += valueToAdd
+operator fun SomeClass.plusAssign(valueToAdd: Int) {
+ this.savedValue += valueToAdd
+}
+
+// -instance
+operator fun SomeClass.unaryMinus() = SomeClass(-this.savedValue)
+
+// ++instance or instance++
+operator fun SomeClass.inc() = SomeClass(this.savedValue + 1)
+
+// instance * other
+operator fun SomeClass.times(other: SomeClass) =
+ SomeClass(this.savedValue * other.savedValue)
+
+// an overload for multiply
+operator fun SomeClass.times(value: Int) = SomeClass(this.savedValue * value)
+
+// other in instance
+operator fun SomeClass.contains(other: SomeClass) =
+ other.savedValue == this.savedValue
+
+// instance[dummyIndex] = valueToSet
+operator fun SomeClass.set(dummyIndex: Int, valueToSet: Int) {
+ this.savedValue = valueToSet + dummyIndex
+}
+
+// instance()
+operator fun SomeClass.invoke() {
+ println("instance invoked by invoker")
+}
+
+/* return type must be Integer,
+so that, it can be translated to "returned value" compareTo 0
+
+for equality (==,!=) using operator will violates overloading equals function,
+since it is already defined in Any class
+*/
+operator fun SomeClass.compareTo(other: SomeClass) =
+ this.savedValue - other.savedValue
+
+fun testOperator() {
+ var x = SomeClass(4)
+
+ println(x) // => "SomeClass(savedValue=4)"
+ x += 10
+ println(x) // => "SomeClass(savedValue=14)"
+ println(-x) // => "SomeClass(savedValue=-14)"
+ println(++x) // => "SomeClass(savedValue=15)"
+ println(x * SomeClass(3)) // => "SomeClass(savedValue=45)"
+ println(x * 2) // => "SomeClass(savedValue=30)"
+ println(SomeClass(15) in x) // => true
+ x[2] = 10
+ println(x) // => "SomeClass(savedValue=12)"
+ x() // => "instance invoked by invoker"
+ println(x >= 15) // => false
+}
```
### Further Reading
diff --git a/pascal.html.markdown b/pascal.html.markdown
index 6877afef..4144f900 100644
--- a/pascal.html.markdown
+++ b/pascal.html.markdown
@@ -3,6 +3,7 @@ language: Pascal
filename: learnpascal.pas
contributors:
- ["Ganesha Danu", "http://github.com/blinfoldking"]
+ - ["Keith Miyake", "https://github.com/kaymmm"]
---
@@ -92,5 +93,46 @@ End.
```
```pascal
+program Functional_Programming;
-``` \ No newline at end of file
+Var
+ i, dummy : integer;
+
+function factorial_recursion(const a: integer) : integer;
+{ recursively calculates the factorial of integer parameter a }
+
+// Declare local variables within the function
+// e.g.:
+// Var
+// local_a : integer;
+
+Begin
+ If a >= 1 Then
+ // return values from functions by assigning a value to the function name
+ factorial_recursion := a * factorial_recursion(a-1)
+ Else
+ factorial_recursion := 1;
+End; // terminate a function using a semicolon after the End statement.
+
+procedure get_integer(var i : integer; dummy : integer);
+{ get user input and store it in the integer parameter i.
+ parameters prefaced with 'var' are variable, meaning their value can change
+ outside of the parameter. Value parameters (without 'var') like 'dummy' are
+ static and changes made within the scope of the function/procedure do not
+ affect the variable passed as a parameter }
+
+Begin
+ write('Enter an integer: ');
+ readln(i);
+ dummy := 4; // dummy will not change value outside of the procedure
+End;
+
+Begin // main program block
+ dummy := 3;
+ get_integer(i, dummy);
+ writeln(i, '! = ', factorial_recursion(i));
+ // outputs i!
+ writeln('dummy = ', dummy); // always outputs '3' since dummy is unchanged.
+End.
+
+```