Skip to content

Commit

Permalink
Merge pull request #99 from PoCInnovation/zig
Browse files Browse the repository at this point in the history
Zig workshop
  • Loading branch information
Molaryy authored Aug 29, 2024
2 parents 4e611f5 + e47e71a commit 18d6edd
Show file tree
Hide file tree
Showing 3 changed files with 374 additions and 0 deletions.
304 changes: 304 additions & 0 deletions software/32.Zig/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
# Workshop 32 - Introduction to Zig

Welcome to this introductory workshop on Zig! Zig is a modern programming language that emphasizes robustness, performance and clarity. Today, you'll learn :
✔️ How to install Zig
✔️ Create your first project
✔️ Discover the language's key concepts by creating a few projects

## Introduction

Zig is a general-purpose programming language focused on robustness, performance, and simplicity. It offers manual memory management, safety features, built-in cross-compilation, and seamless C interoperability, making it ideal for system programming and high-performance applications.

## Step 0 - SETUP

All the required information to install dependencies can be found in [SETUP.md](./SETUP.md).

> 💡 We recommend you to follow the [Getting started](https://ziglang.org/learn/getting-started/) for this workshop.
## Step 1 - Hello World! in Zig

> ❗ We strongly advise you to use the documentation provided for this exercise.
📑 Description:

For the first exercise, we simply ask you to print `Hello world!` in your terminal when you run your program.

📌 Tasks:

Create a file `main.zig` in a folder called `src` with your logic to print the "hello world"

It should look like this 🔽

```sh
src
└── main.zig
```

📚 Documentation:

💡 After creating your main.zig file, you can include and use logic from other Zig files in your project. To do this, you need to integrate the modules from those files into your main.zig. You can achieve this by using the @import statement to include and access the functionality defined in different Zig files.
For more details on including Zig files, check out this guide : <https://stackoverflow.com/questions/71186556/how-do-i-include-one-zig-file-from-another-zig-file>

- [Build System](https://ziglang.org/learn/build-system/)
- [Doc.Zig](https://ziglang.org/documentation/master/)

✔️ Validation:

```sh
zig build-exe src/main.zig
./main
```

you should see the following :

```sh
Hello, World!
```

## Step 2 - Palindrome?

📑 Description:

For the second exercise, you have to create a function that takes as parameter a string named `word`.

📌 Tasks:

Create a file `palindrome.zig` for this new function.

```sh
src
└── main.zig
└── palindrome.zig
```

This function must return true if the word given in parameter is a palindrome and false in the opposite case.

📚 Documentation:

-[What is a palindrome ?](https://www.wikiwand.com/en/Palindrome)
-[New function](https://ziglang.org/documentation/master/#Functions)
-[The types in Zig](https://ziglang.org/documentation/master/#Primitive-Types)
-[Control_Structures](https://zig.guide/language-basics/while-loops)

✔️ Validation:

Here's a main example :

```zig
pub fn main() void {
const stdout = std.io.getStdOut().writer();
const test_word1 = "madam";
const test_word2 = "hello";
const result1 = is_palindrome(test_word1);
const result2 = is_palindrome(test_word2);
stdout.print("{} is palindrome: {}\n", .{test_word1, result1}) catch {};
stdout.print("{} is palindrome: {}\n", .{test_word2, result2}) catch {};
}
```

When you compile and run palindrome.zig, the output should be:

```sh
madam is palindrome: true
```

or

```sh
hello is palindrome: false
```

## Step 3 - A simple and useful `Calculator`

📑 Description:

Let's go further now!

The objective of this fifth exercise is to create a simplified calculator in the terimnal.
For this step you will create a interactive calculator that will take a first number, then the operation and finally the second number and once you have all the information, compute all that and print the result.

📌 Tasks:

- Create a file `calculator.zig` for this new function.

```sh
src
└── main.zig
└── calculator.zig
```

To do this, you have to make a calculator that can do:

- `Addition`
- `Multiplication`
- `Division`
- `Subtraction`

- between two values that must be retrieved one after the other from the user's input.

> 💡 Pay attention to your error handling !
- Display the result of the calculation in your terminal.
For this step you will create a interactive calculator that will take a first number, then the operation and finally the second number and once you have all the information, compute all that and print the result.
📚 Documentation:
-[Errors](https://ziglang.org/documentation/master/#Errors)
-[Memory](https://zig.guide/standard-library/allocators)
-[ArrayList](https://zig.guide/standard-library/arraylist)

✔️ Validation:

You should get something like this :

```sh
First number: 5
Operation: +
Second number: 3
8
```

Here's a typical error handeling example :

```sh
First number: abc
Operation: +
Second number: 3
Expected Output: Error message indicating invalid input.
```

## Step 4 - Pascal's Triangle Line

📑 Description:

Let's dive into a more advanced topic!

The objective of this exercise is to generate the n-th line of Pascal's Triangle. Pascal's Triangle is a triangular array of binomial coefficients. For this step, you will create a function that takes an int32 parameter n and returns a list of numbers corresponding to the n-th line of the triangle.

📌 Tasks:

Create a file pascals_triangle.zig for this new function.

Your project structure should look like this 🔽

```sh
src
└── main.zig
└── pascals_triangle.zig
```

Implement the function to compute the n-th line of Pascal's Triangle. You will need to:
Create a function that computes the binomial coefficients for the given line index n.
Use an efficient method to calculate these coefficients.
Return a list of coefficients that represents the n-th line of Pascal's Triangle.

Test your implementation by adding a main function that prints out the line of Pascal's Triangle for a given n.

📚 Documentation:

Factorials : <https://ziglang.org/documentation/master/#Factorial>
Binomial Coefficient: <https://en.wikipedia.org/wiki/Binomial_coefficient>
Array Manipulation <https://ziglang.org/documentation/master/#Array>

✔️ Validation:

To validate your exercise, compile and run your program. You should see output similar to the following:

```sh
zig build-exe src/pascals_triangle.zig
./pascals_triangle
```

Expected output:

```sh
Line 5 of Pascal's Triangle: 1, 5, 10, 10, 5, 1
```
Ensure your program correctly calculates and displays the n-th line of Pascal's Triangle. Check different values of n to verify that the output is accurate.

## Bonuses - TO DO List and C Code translation

### TO DO List

📑 Description:

Now that you're used to the basics, you will do a little project to apply what you've learnt so far.

📌 Tasks:

Create a command-line todo list application where you can:

- Add a new todo
- Delete a todo by ID
- View all todos

Each todo should have at least three fields:

- id (auto-incremented) which will be an integer
- name which will be a string
- description which will also be a string

> 💡 To easily test your functions during this workshop remember to check the testing tools mentioned above

📚 Documentation:

- [New function](https://ziglang.org/documentation/master/#Functions)
- [The types in Zig](https://ziglang.org/documentation/master/#Primitive-Types)

✔️ Validation:

```sh
./zig-out/bin/main
Choose an action:
1. Add Todo
2. Delete Todo
3. View Todos
4. Exit
> 1
Enter name: Buy groceries
Enter description: Milk, eggs, and bread
Todo added successfully.
```

```sh
Choose an action:
5. Add Todo
6. Delete Todo
7. View Todos
8. Exit
> 3
ID: 1, Name: Buy groceries, Description: Milk, eggs, and bread
Choose an action:
9. Add Todo
10. Delete Todo
11. View Todos
12. Exit
> 4
```

> 💡 Pay attention to your error handling !

### Use The C Translation tool

📑 Description:

Zig provides the command zig translate-c for automatic translation from C source code. You can try to read the documentation and have fun with the tool.

📚 Documentation:

-[Translate-C](https://zig.guide/working-with-c/translate-c)
-[cImport](https://zig.guide/working-with-c/c-import)
-[LinkLibC](https://zig.guide/working-with-c/linking-libc)

## Conclusion

Well done ! You've accomplished a lot with the Zig Workshop, and there is so much more to discover. Refer to the official documentation to deep-dive into it.
Hope you enjoyed the workshop!
## Authors
- Elie STROUN
- Pierre LISSOPE
> 🚀 Follow us on our different social networks, and put a star 🌟 on `PoC's` repositories.
70 changes: 70 additions & 0 deletions software/32.Zig/SETUP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Installing Zig

## For Fedora

Since most students use Fedora, installing Zig is very simple. Just open a terminal and run the following command:

```sh
sudo dnf install zig
```

### For other operating systems

- Visit the official Zig website: ziglang.org.

### to check installation

```sh
zig version
```

## Initializing a Zig Project

### Step 1: Create a new project

To create a new Zig project, open a terminal and navigate to the directory where you wish to create your project. Then run the following command:

```sh
zig init
```

This command will create a basic project structure with the necessary files.

### Step 2: Explore the project structure

Once the project has been created, you'll see the following structure:

```sh
.
├── build.zig
├── build.zig.zon
└── src
├── main.zig
└── root.zig
```

- build.zig: This file is a build script for your Zig project.
- src/main.zig: This is the entry point for your Zig program.
- src/root.zig: This is a sub-file of example (don't worry if you don't have it).

### Step 3: Compile and run the project

To compile your project, run the following command in your project directory:

```sh
zig build
```

After compilation, a binary will be generated in the zig-out/bin folder (it may be called something different depending on the name of your folder). To run this binary, use the following command:

```sh
./zig-out/bin/main
```

You should see the messages:
"All your codebase are belong to us.
Run `zig build test` to run the tests." are displayed, indicating that your project has been correctly configured and compiled.

### Next step

Now that your basic project is ready, let's explore the basic concepts of the Zig language and develop a palindrome detection program.
Empty file added software/32.Zig/gitkeep
Empty file.

0 comments on commit 18d6edd

Please sign in to comment.