Mass-replace tabs with 4-width spacing

This seems to be the predominant choice, and matches the last commit I
just made, so I went ahead and converted them all, and changed any, -
for example, 2-space indents.

Let me know if this is undesired.

To understand why I chose to do this, please refer to the previous
commit's message.
pull/99/head
terminalforlife 4 years ago
parent 4df5219c66
commit ade78aaafb

@ -1,29 +1,28 @@
// ### Functions
// a simple function
// ### Functions
// a simple function
void functionName() {}
// function with parameters of integers
void functionName(int param1, int param2) {}
// return type declaration
int functionName() {
// return type declaration
int functionName() {
return 420;
}
// Return multiple values at once
// Return multiple values at once
// (C++ uses pairs [ 2 values ] and tuple [ more than 2 values ])
// Please refer pairs and tuple respectively for more information
std::tuple<double, double> functionName() {
return std::make_tuple( double1, double2 );
return std::make_tuple( double1, double2 );
}
pair<double,double> result = functionName();
std::cout << result.first << std::endl;
std::cout << result.second << std::endl;
// Pass by reference (the caller and the callee use the same variable for the parameter)
// Pass by reference (the caller and the callee use the same variable for the parameter)
int functionName(int &referenceName) {}
// Pass by value (the caller and callee have two independent variables with the same value)
int functionName(int valueName) {}

@ -1,8 +1,15 @@
int *int_Ptr; // Declare a pointer variable called iPtr pointing to an int (an int pointer)
// It contains an address. That address holds an int value.
double *double_Ptr; // Declare a pointer of type double.
// Declare a pointer variable called iPtr pointing to an int (an int pointer)
// It contains an address. That address holds an int value.
int *int_Ptr;
int a = 5; // Initializes a to the integer value 5
int *int_Ptr = &a // Set int_Ptr which is an int pointer to the address of a
std::cout << int_Ptr; // Returns the address of a
std::cout << *int_Ptr; // Returns 5 because it dereference the pointer to retrieve the value of a.
// Declare a pointer of type double.
double *double_Ptr;
// Initializes a to the integer value 5
int a = 5;
// Set int_Ptr which is an int pointer to the address of `a`.
int *int_Ptr = &a
// Returns the address of `a`.
std::cout << int_Ptr;
// Returns 5 because it dereference the pointer to retrieve the value of `a`.
std::cout << *int_Ptr;

@ -2,12 +2,12 @@
# Go to http://elm-lang.org/install
# and download the appropriate installer for your system.
# To create a project:
# mkdir hello
# cd hello
# elm package install elm-lang/html
# mkdir hello
# cd hello
# elm package install elm-lang/html
# after that create Hello.elm in this directory
# To start:
# elm reactor
# elm reactor
# Read more here:
# https://www.elm-tutorial.org/en/01-foundations/01-hello.html
module Hello exposing (..)

@ -34,5 +34,5 @@ func (v *Vertex) add(n float64) {
// **Anonymous structs:**
// Cheaper and safer than using `map[string]interface{}`.
point := struct {
X, Y int
X, Y int
}{1, 2}

@ -66,19 +66,19 @@ func outer() (func() int, int) {
// ### Variadic Functions
func main() {
fmt.Println(adder(1, 2, 3)) // 6
fmt.Println(adder(9, 9)) // 18
fmt.Println(adder(1, 2, 3)) // 6
fmt.Println(adder(9, 9)) // 18
nums := []int{10, 20, 30}
fmt.Println(adder(nums...)) // 60
nums := []int{10, 20, 30}
fmt.Println(adder(nums...)) // 60
}
// By using ... before the type name of the last parameter you can indicate that it takes zero or more of those parameters.
// The function is invoked like any other function except we can pass as many arguments as we want.
func adder(args ...int) int {
total := 0
for _, v := range args { // Iterates over the arguments whatever the number.
total += v
}
return total
total := 0
for _, v := range args { // Iterates over the arguments whatever the number.
total += v
}
return total
}

@ -1,23 +1,23 @@
func main() {
// Basic one
if x > 0 {
return x
} else {
return -x
}
// You can put one statement before the condition
if a := b + c; a < 42 {
return a
} else {
return a - 42
}
// Basic one
if x > 0 {
return x
} else {
return -x
}
// You can put one statement before the condition
if a := b + c; a < 42 {
return a
} else {
return a - 42
}
// Type assertion inside if
var val interface{}
val = "foo"
if str, ok := val.(string); ok {
fmt.Println(str)
}
// Type assertion inside if
var val interface{}
val = "foo"
if str, ok := val.(string); ok {
fmt.Println(str)
}
}

@ -1,18 +1,28 @@
var a []int // declare a slice - similar to an array, but length is unspecified
var a = []int {1, 2, 3, 4} // declare and initialize a slice (backed by the array given implicitly)
a := []int{1, 2, 3, 4} // shorthand
chars := []string{0:"a", 2:"c", 1: "b"} // ["a", "b", "c"]
// declare a slice - similar to an array, but length is unspecified
var a []int
// declare and initialize a slice (backed by the array given implicitly)
var a = []int {1, 2, 3, 4}
// shorthand
a := []int{1, 2, 3, 4}
// ["a", "b", "c"]
chars := []string{0:"a", 2:"c", 1: "b"}
var b = a[lo:hi] // creates a slice (view of the array) from index lo to hi-1
var b = a[1:4] // slice from index 1 to 3
var b = a[:3] // missing low index implies 0
var b = a[3:] // missing high index implies len(a)
a = append(a,17,3) // append items to slice a
c := append(a,b...) // concatenate slices a and b
// creates a slice (view of the array) from index lo to hi-1
var b = a[lo:hi]
// slice from index 1 to 3
var b = a[1:4]
// missing low index implies 0
var b = a[:3]
// missing high index implies len(a)
var b = a[3:]
// append items to slice a
a = append(a,17,3)
// concatenate slices a and b
c := append(a,b...)
// create a slice with make
a = make([]byte, 5, 5) // first arg length, second capacity
a = make([]byte, 5) // capacity is optional
a = make([]byte, 5, 5) // first arg length, second capacity
a = make([]byte, 5) // capacity is optional
// create a slice from an array
x := [3]string{"Лайка", "Белка", "Стрелка"}

@ -1,40 +1,40 @@
# Simple function
def functionName():
return True
return True
# Function with parameters
def functionName(a, b):
if a < b:
return a
else:
return b
def functionName(a, b):
if a < b:
return a
else:
return b
# Return multiple values
def functionName(a, b, c):
return a, b, c # Returns a tuple
return {'return_a':a, 'return_b':b ,'return_c':c } # Returns a dictionary
def functionName(a, b, c):
return a, b, c # Returns a tuple
return {'return_a':a, 'return_b':b ,'return_c':c } # Returns a dictionary
# Function with default parameters
def functionName(a=0, b=1):
print(a, b)
functionName() # 0 1
functionName(3) # 3 1
functionName(3, 4) # 3 4
def functionName(a=0, b=1):
print(a, b)
functionName() # 0 1
functionName(3) # 3 1
functionName(3, 4) # 3 4
# Calling parameters by name
def functionName(a, b, c):
print(a, b, c)
functionName(0, 1, 2) # 0 1 2
functionName(a=2, c=3, b=4) # 2 4 3
functionName(2, 3, c=4) # 2 3 4
def functionName(a, b, c):
print(a, b, c)
functionName(0, 1, 2) # 0 1 2
functionName(a=2, c=3, b=4) # 2 4 3
functionName(2, 3, c=4) # 2 3 4
# Arbitrary number of parameters
def functionName(*args):
...
functionName(*[1, 2]) # Equivalent of functionName(1, 2)
functionName(*[1, 2, 3]) # Equivalent of functionName(1, 2, 3)
...
functionName(*[1, 2]) # Equivalent of functionName(1, 2)
functionName(*[1, 2, 3]) # Equivalent of functionName(1, 2, 3)
# Arbitrary number of parameters with arbitrary name
def functionName(**kwargs):
...
functionName(**{'a' : 3, 'b' : 4}) # Equivalent of functionName(a=3, b=4)
...
functionName(**{'a' : 3, 'b' : 4}) # Equivalent of functionName(a=3, b=4)

@ -1,33 +1,33 @@
# Lambda are anonymous functions in Python by using the keyword lambda
# Therefore they are not bound to a name
# Therefore they are not bound to a name
# Simple Lambda Function
# Simple Lambda Function
a = lambda parameter: parameter + 40
#
print (a(2)) # Outputs 42
# Lambda Functions Inside Real Functions
def subtract_func(n) :
def subtract_func(n) :
return lambda x: x - n
#
a = subtract_func(1) # Sets n to 1 for a
b = subtract_func(2) # Sets n to 2 for b
b = subtract_func(2) # Sets n to 2 for b
#
print(a(-4)) # Outputs -5 ( -5 = -4 - 1 )
print(b(-2)) # Outputs -4 ( -4 = -2 - 2 )
# Lambda Function with Multiple Parameters
f = lambda x, y : x + y
# Lambda Function with Multiple Parameters
f = lambda x, y : x + y
#
print( f(1,1) ) # Outputs 2 ( 1 + 1 )
# map() + lambda functions
a = [1, 2, 3, 4, 5]
b = [1, 2, 3, 4, 5]
a = [1, 2, 3, 4, 5]
b = [1, 2, 3, 4, 5]
#
r = map(lambda x,y : x+y, a,b) # map() will return an iterator
r_list = list(r) # listify r
print(r_list) # prints [2, 4, 6, 8, 10]
r = map(lambda x,y : x+y, a,b) # map() will return an iterator
r_list = list(r) # listify r
print(r_list) # prints [2, 4, 6, 8, 10]
# filter() + lambda functions
# Program to filter out only the even items from a list
@ -35,4 +35,4 @@ my_list = [1, 5, 4, 6, 8, 11, 3, 12]
#
new_list = list(filter(lambda x: (x%2 == 0) , my_list))
#
print(new_list) # Output: [4, 6, 8, 12]
print(new_list) # Output: [4, 6, 8, 12]

@ -1,12 +1,12 @@
# Basic conditional:
if x > 0:
print(x)
print(x)
# 'if else'
if x > 0:
print(x)
else:
print(-x)
if x > 0:
print(x)
else:
print(-x)
#ternary operator
parity = 'even' if x % 2 == 0 else 'odd'
@ -14,69 +14,69 @@ parity = 'even' if x % 2 == 0 else 'odd'
'''
# Equivalent of:
if x % 2 == 0:
parity = 'even'
parity = 'even'
else:
parity = 'odd'
parity = 'odd'
'''
# multiple conditions:
if x > 0:
print(x)
print(x)
elif x == 0:
print(420)
print(420)
elif x == 1:
print(421)
print(421)
else:
print(-x)
print(-x)
# Basic 'for' loop:
for i in range(6):
print(i) #prints 0, 1, 2, 3, 4, 5
print(i) #prints 0, 1, 2, 3, 4, 5
#
for i in range(2, 6):
print(i) #prints 2, 3, 4, 5
print(i) #prints 2, 3, 4, 5
#
for i in range(3, 10, 2):
print(i) #prints 3, 5, 7, 9
print(i) #prints 3, 5, 7, 9
# Iterating through collections:
for i in [0, 1, 1, 2, 3, 5]:
print(i) #prints 0, 1, 1, 2, 3, 5
print(i) #prints 0, 1, 1, 2, 3, 5
#
for i in 'qwerty':
print(i) #prints q, w, e, r, t, y
print(i) #prints q, w, e, r, t, y
# 'for else':
for i in x:
if i == 0:
break
if i == 0:
break
else:
print('not found')
print('not found')
'''
# Equivalent of:
flag = False
for i in x:
if i == 0:
flag = True
break
if i == 0:
flag = True
break
if not flag:
print('not found')
print('not found')
'''
# Basic 'while' loop:
x = 0
while x < 6:
print(i)
x += 2
print(i)
x += 2
# prints 0, 2, 4
# No 'do while' loop in Python.
# Equivalent with 'while' loop:
x = 4
while True:
print(x)
x += 1
if x >= 4:
break
print(x)
x += 1
if x >= 4:
break
# prints 4

@ -11,8 +11,8 @@ l map (_ * 2) //returns List(2,4,6,8,10)
// allows the ability to provide different map functions for different discrete cases
// this example will increment odd numbers by one, but double even numbers
l map { //note: the curly brackets allow us to make the map multi-line and use 'case' statements (see PatternMatching)
case num if num % 2 == 0 => num * 2
case other => other + 1
case num if num % 2 == 0 => num * 2
case other => other + 1
} //returns List(2,4,4,8,6)
// filter
@ -26,7 +26,7 @@ l filter (_ % 2 == 0) //returns List(1,3,5)
// this is like a combination of filter and map
// this example shows that collect essentially filters by "i < 3" then maps with "_ + 20"
l collect { //note: collect requires a partial function, so we have to use curly brackets and 'case' statements
case i if i < 3 => i + 20
case i if i < 3 => i + 20
} //returns List(21, 22)
// find
@ -66,12 +66,12 @@ List(List(1,2,3), List(4,5)).flatten //returns List(1,2,3,4,5)
// maps then flattens in one function
// in this example, we will map and flatMap a List[Int] using a function that turns each Int into a List[Int]
List(1,2,3) map {
num => List(num, num)
num => List(num, num)
}
// for this example, mapping returns List(List(1,1),List(2,2),List(3,3))
// for this example, mapping returns List(List(1,1),List(2,2),List(3,3))
//vs
List(1,2,3) flatMap {
num => List(num, num)
num => List(num, num)
} // returns List(1,1,2,2,3,3)
// isEmpty
@ -89,12 +89,12 @@ oneToSix diff (fourToNine) //returns List(1,2,3)
fourToNine diff (oneToSix) //returns List(7,8,9)
// intersect
// returns the "intersection" of two collections, these are the elements that exist in both collections
// returns the "intersection" of two collections, these are the elements that exist in both collections
val oneToSix = List(1, 2, 3, 4, 5, 6)
val fourToNine = List(4, 5, 6, 7, 8, 9)
oneToSix intersect (fourToNine) //returns List(4,5,6)
fourToNine intersect (oneToSix) //returns List(4,5,6)
// union
// returns the concatenation of the two lists
val oneToSix = List(1, 2, 3, 4, 5, 6)
@ -199,4 +199,4 @@ List(1,2,3).tail //returns List(2,3), same as
List(1,2,3).drop(1)
// last
List(1,2,3).last //returns 3
List(1,2,3).last //returns 3

@ -1,21 +1,21 @@
// use case in function args for pattern matching.
(xs zip ys) map { case (x,y) => x*y } // GOOD
(xs zip ys) map( (x,y) => x*y ) // BAD
(xs zip ys) map { case (x,y) => x*y } // GOOD
(xs zip ys) map( (x,y) => x*y ) // BAD
// "v42" is interpreted as a name matching any Int value, and "42" is printed.
// BAD
val v42 = 42
Some(3) match {
case Some(v42) => println("42")
case _ => println("Not 42")
case Some(v42) => println("42")
case _ => println("Not 42")
}
// "`v42`" with backticks is interpreted as the existing val v42, and “Not 42” is printed.
// GOOD
val v42 = 42
Some(3) match {
case Some(`v42`) => println("42")
case _ => println("Not 42")
case Some(`v42`) => println("42")
case _ => println("Not 42")
}
// UppercaseVal is treated as an existing val, rather than a new pattern variable, because it starts with an uppercase letter.
@ -23,24 +23,24 @@ Some(3) match {
// GOOD
val UppercaseVal = 42
Some(3) match {
case Some(UppercaseVal) => println("42")
case _ => println("Not 42")
case Some(UppercaseVal) => println("42")
case _ => println("Not 42")
}
// Creating an alias for a match
// This will maintain the original value passed into the match function, using the '@' symbol, and print "Matched Some(3)"
Some(3) match {
case foundSome @ Some(_) => println("Matched " + foundSome)
case _ => println("Matched nothing")
case foundSome @ Some(_) => println("Matched " + foundSome)
case _ => println("Matched nothing")
}
// Case Classes
// This method allows you to match on any combination of properties of a case class
case class Example(a: Int, b: String, c: Boolean)
Example(1, "word", true) match {
case Example(3, _, _) => println("Matches any Example where a = 3")
case Example(_, "foo", _) => println("Matches any Example where b = foo")
case Example(_, _, false) => println("Matches any Example where c = false")
case Example(1, "word", true) => println("Matches our Example")
case Example(_, _, _) => println("Matches any other Example")
case Example(3, _, _) => println("Matches any Example where a = 3")
case Example(_, "foo", _) => println("Matches any Example where b = foo")
case Example(_, _, false) => println("Matches any Example where c = false")
case Example(1, "word", true) => println("Matches our Example")
case Example(_, _, _) => println("Matches any other Example")
}

Loading…
Cancel
Save