Skip to content

Commit

Permalink
Merge pull request #26 from Delta456/patch-1
Browse files Browse the repository at this point in the history
[WIP] Follow #25 and more progress
  • Loading branch information
vbrazo committed Oct 24, 2019
2 parents 6fd6884 + 7271403 commit 0ed373b
Show file tree
Hide file tree
Showing 11 changed files with 334 additions and 91 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Discord server: https://discord.gg/d3Qk65J

- [Arrays](examples/arrays.md)
- [Conditional Statements](examples/conditional_statements/conditional_statements.md)
- [Loop](examples/loops/loops.md)
- [Functions](examples/functions.md)
- [Keywords](examples/keywords.md)
- [Operator](examples/operator.md)
Expand Down
6 changes: 3 additions & 3 deletions examples/arrays.md → examples/arrays/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ println(ages)
println(ages[1])
>>> 25

mut github_users := ['vbrazo', 'donnisnoni95', 'Delta456']
mut users := ['vbrazo', 'donnisnoni95', 'Delta456']

println(github_users)
println(users)
>> ['vbrazo', 'donnisnoni95', 'Delta456']

println(github_users[0])
Expand All @@ -25,7 +25,7 @@ println(github_users[0])
Note: All elements must have the same type. `['vbrazo', 'donnisnoni95', 'Delta456', 0]` will not compile.

```bash
>>> mut github_users := ['vbrazo', 'donnisnoni95', 'Delta456', 0]
>>> mut users := ['vbrazo', 'donnisnoni95', 'Delta456', 0]
/user/vlang/v_by_example/.vrepl_temp.v:2:43: bad array element type `int` instead of `string`
```

Expand Down
108 changes: 108 additions & 0 deletions examples/arrays/functions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Array Functions

## `repeat`

Syntax

```go
Type[element].repeat(number int)
```

Makes an array with the given element number of times.

```go
foo := int[1].repeat(7)
println(foo)
```
Output

```go
[1, 1, 1, 1, 1, 1, 1]
```

## insert

Syntax

```go
array.insert(num int, data T)
```

Inserts the data at a given position and shifts the elements in the array

```go
names := ['Samuel', 'John', 'Peter']
names.insert(2,'Tom')
println(names)
```

Output

```go
['Samuel', 'John', 'Tom', 'Peter']
```

## `delete`

Syntax

```go
array.delete(element T)
```

Deletes the element present in the array

```go
even_numbers = [2, 4, 6, 8, 10]
even_numbers.delete(8)
println(even_number)
```

Output

```go
[2, 4, 6, 10]
```

## `reverse`

Syntax

```go
array.reverse()
```

Reverses the array

```go
float_num := [1.1, 1.3, 1.25, 1.4]
float_num.reverse()
```

Output

```go
[1.4, 1.25, 1.3, 1.1]
```

## `clone`

Syntax

```go
array.clone()
```

Clones and returns a new array

```go
foo := [1, 2, 4, 5, 4, 6]
foo1 := foo.clone()
println(foo1)
```

Output

```go
[1, 2, 4, 5, 4, 6]
```
118 changes: 38 additions & 80 deletions examples/conditional_statements/conditional_statements.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Conditional Statements

## If statement
## `if` statement

An `if` statement is a programming conditional statement that, if proved true, performs a function or displays information. Below is a general example of an if statement in V:
An `if` statement is a programming conditional statement that, if proved true, executes the code given in the block. Below is a general example of an if statement in V:

```go
john_height := 100
Expand All @@ -15,6 +15,28 @@ if john_height < maria_height {
}
```

In the above code, `fn println()` will only execute when the condition is true else no statement would be printed.

## `else` statement

An `else` statement is a programming conditional statement in which when `if` evaluates to false then the code in else block executes.

```go
joey_age := 12
kevin_age := 15

if joey_age > kevin_age {
println("Joey is older")
} else {
println("Kevin is older")
}
```

In this example the block in else will execute because the condition in `if` evaluates to false.


## `else if` statement

The `if...else` statement executes two different codes depending upon whether the test expression is `true` or `false`. Sometimes, a choice has to be made from more than 2 possibilities. The `if...else if...else` ladder allows you to check between multiple test expressions and execute different statements.

```go
Expand All @@ -38,13 +60,15 @@ ashia_age := 38

if tom_age < ashia_age {
if tom_age < 18 {
println('tom_age < 18 and younger than Ashia.')
} else {
println('tom_age > 18 and older than Ashia.')
}
} else {
println('$tom_age == $ashia_age')
println('Tom and Ashia have the same age.')
println('tom_age < 18 and younger than Ashia.')
} else {
println('tom_age >= 18 and younger than Ashia.')
}
}
else if tom_age > ashia_age {
println('$tom_age > $ashia_age')
} else {
println('$tom_age == $ashia_age')
}
```

Expand All @@ -63,7 +87,11 @@ s := if tom_age < ashia_age {
}

print(s)
>> Tom is the youngest
```

Output
```bash
Tom is the youngest
```

## Exercises
Expand All @@ -72,73 +100,3 @@ print(s)
2. Write a V program to check whether a given number is even or odd.
3. Write a V program to check whether a given number is positive or negative.
4. Write a V program to find whether a given year is a leap year or not.

## For

`for` loops offer a quick and easy way to do something repeatedly. They're handy, if you want to run the same code over and over again, each time with a different value. You can think of a loop as a computerized version of the game where you tell someone to take X steps in one direction then Y steps in another; for example, the idea "Go five steps to the east" could be expressed this way as a loop:

```go
for i := 0; i < 5; i++ {
println('Walking one step');
}
```

V has the `for` looping construct and the loop can be written in different ways:

1. `in` operator

```go
ages := [18, 25, 32, 43, 50]

for age in ages {
println(age)
}
```

Note, that the value is read-only.

2. `while` loop form

A `while` loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. There are no parentheses surrounding the condition, and the braces are always required.

```go
mut factorial := 1
mut counter := 1

for {
counter++
factorial=factorial*counter

if counter > 5 {
print(factorial)
break
}
}

println(counter)
>> 120
```

3. `for` with the traditional C style

```go
mut factorial := 1
mut counter := 1

for i := 0; i < 5; i++ {
factorial=factorial*counter
if i == 6 {
print(factorial)
continue
}
println(i)
}
```

## Exercises

1. Write a V program to display the first 10 natural numbers.
2. Write a V program to find the sum of first 10 natural numbers.
3. Write a V program to display n terms of natural number and their sum.
4. Write a V program to read 10 numbers from keyboard and find their sum and average.
5. Write a V program to display the cube of the number upto given an integer.
73 changes: 73 additions & 0 deletions examples/functions.md → examples/functions/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ A function is a block of organized, reusable code that is used to perform a sing

Ideally you should consider using the [single responsibility principle](https://en.wikipedia.org/wiki/Single_responsibility_principle) (SOLID) which states that every module or function should have responsibility over a single part of the functionality provided by the software to keep your code maintainable.

Like C and Go, functions cannot be overloaded.

```go
fn main() {
println(sum(77, 33))
Expand All @@ -26,6 +28,77 @@ fn full_name(first_name, last_name string) string {
}
```

## Variadic Functions

Functions can also be variadic i.e. accept infinite number of arguments. They are **not** arrays and cannot be returned.

```go
fn main() {
foo("V", "is", "the", "best", "lang" , "ever")
}

fn foo(test ...string) {
println(test)
}
```

Output

```bash
V
is
the
best
lang
ever
```

## Multi-Return Functions

Similar to Go, functions in V can also return multiple and with different type.

```go
fn main() {
name, age := student("Tom", 15)
println(name, age)

}

fn student(name string, age int) string, int {
return name, age
}
```

Output

```bash
Tom , 15
```

## High Order Functions

Functions can also take in another function which is usually needed to sort, map, fitler etc.

```go
fn square(num int) int {
return num * num
}

fn run(value int, op fn(int) int) int {
return op(value)
}

fn main() {
println(run(10, square))
}
```

Output

```bash
100
```

## Exercises

1. Write a V program to find the square of any number using the function.
Expand Down
12 changes: 12 additions & 0 deletions examples/hello_world/comment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## Comments

V supports single line comments `//` and multi-line comments `/* */`. They should be used for documenting the code for letting the other users know how the code works. It can also be used for temporarily commenting the code which has to be used later on.

```go
// This is a single line comment

/* This is a
* multi-line comment
* /* This could be nested as well*/
*/
```
2 changes: 0 additions & 2 deletions examples/hello_world/hello.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Hello V

## Formatted Print

Printing is handled by various I/O stream functions. One should know where to use them accordingly.
Expand Down
Loading

0 comments on commit 0ed373b

Please sign in to comment.