Skip to content

Commit

Permalink
Merge pull request #1251 from xushiwei/readme
Browse files Browse the repository at this point in the history
add: Data processing
  • Loading branch information
xushiwei committed Jun 6, 2022
2 parents aaa5ae9 + a1eb47b commit 5e37f69
Showing 1 changed file with 76 additions and 71 deletions.
147 changes: 76 additions & 71 deletions README.md
Expand Up @@ -81,19 +81,20 @@ Here is my `Hello world` program:
* [Statements & expressions](#statements--expressions)
* [If..else](#ifelse)
* [For loop](#for-loop)
* [List comprehension](#list-comprehension)
* [Select data from a collection](#select-data-from-a-collection)
* [Check if data exists in a collection](#check-if-data-exists-in-a-collection)

</td><td valign=top>

* [Error handling](#error-handling)
* [Functions](#functions)
* [Returning multiple values](#returning-multiple-values)
* [Variadic parameters](#variadic-parameters)
* [Higher order functions](#higher-order-functions)
* [Lambda expressions](#lambda-expressions)

</td><td valign=top>

* [Structs](#structs)
* [Error handling](#error-handling)
* [Data processing](#data-processing)
* [List comprehension](#list-comprehension)
* [Select data from a collection](#select-data-from-a-collection)
* [Check if data exists in a collection](#check-if-data-exists-in-a-collection)
* [Unix shebang](#unix-shebang)

</td></tr>
Expand Down Expand Up @@ -710,57 +711,57 @@ The condition can be omitted, resulting in an infinite loop. You can use `break`
<h5 align="right"><a href="#table-of-contents">⬆ back to toc</a></h5>


### List comprehension

```go
a := [x*x for x <- [1, 3, 5, 7, 11]]
b := [x*x for x <- [1, 3, 5, 7, 11] if x > 3]
c := [i+v for i, v <- [1, 3, 5, 7, 11] if i%2 == 1]
### Error handling

arr := [1, 2, 3, 4, 5, 6]
d := [[a, b] for a <- arr if a < b for b <- arr if b > 2]
We reinvent the error handling specification in Go+. We call them `ErrWrap expressions`:

x := {x: i for i, x <- [1, 3, 5, 7, 11]}
y := {x: i for i, x <- [1, 3, 5, 7, 11] if i%2 == 1}
z := {v: k for k, v <- {1: "Hello", 3: "Hi", 5: "xsw", 7: "Go+"} if k > 3}
```go
expr! // panic if err
expr? // return if err
expr?:defval // use defval if err
```

<h5 align="right"><a href="#table-of-contents">⬆ back to toc</a></h5>
How to use them? Here is an example:

```go
import (
"strconv"
)

### Select data from a collection
func add(x, y string) (int, error) {
return strconv.Atoi(x)? + strconv.Atoi(y)?, nil
}

```go
type student struct {
name string
score int
func addSafe(x, y string) int {
return strconv.Atoi(x)?:0 + strconv.Atoi(y)?:0
}

students := [student{"Ken", 90}, student{"Jason", 80}, student{"Lily", 85}]
println `add("100", "23"):`, add("100", "23")!

unknownScore, ok := {x.score for x <- students if x.name == "Unknown"}
jasonScore := {x.score for x <- students if x.name == "Jason"}
sum, err := add("10", "abc")
println `add("10", "abc"):`, sum, err

println unknownScore, ok // 0 false
println jasonScore // 80
println `addSafe("10", "abc"):`, addSafe("10", "abc")
```

<h5 align="right"><a href="#table-of-contents">⬆ back to toc</a></h5>
The output of this example is:

```
add("100", "23"): 123
add("10", "abc"): 0 strconv.Atoi: parsing "abc": invalid syntax
### Check if data exists in a collection
===> errors stack:
main.add("10", "abc")
/Users/xsw/tutorial/15-ErrWrap/err_wrap.gop:6 strconv.Atoi(y)?
```go
type student struct {
name string
score int
}
addSafe("10", "abc"): 10
```

students := [student{"Ken", 90}, student{"Jason", 80}, student{"Lily", 85}]
Compared to corresponding Go code, It is clear and more readable.

hasJason := {for x <- students if x.name == "Jason"} // is any student named Jason?
hasFailed := {for x <- students if x.score < 60} // is any student failed?
```
And the most interesting thing is, the return error contains the full error stack. When we got an error, it is very easy to position what the root cause is.

How these `ErrWrap expressions` work? See [Error Handling](https://github.com/goplus/gop/wiki/Error-Handling) for more information.

<h5 align="right"><a href="#table-of-contents">⬆ back to toc</a></h5>

Expand Down Expand Up @@ -1017,57 +1018,61 @@ println doc.any.funcDecl.name
In Go+, we introduce a concept named `auto property`. It is a `get property`, but is implemented automatically. If we have a method named `Bar()`, then we will have a `get property` named `bar` at the same time.


## Error handling
## Data processing

We reinvent the error handling specification in Go+. We call them `ErrWrap expressions`:
### List comprehension

```go
expr! // panic if err
expr? // return if err
expr?:defval // use defval if err
a := [x*x for x <- [1, 3, 5, 7, 11]]
b := [x*x for x <- [1, 3, 5, 7, 11] if x > 3]
c := [i+v for i, v <- [1, 3, 5, 7, 11] if i%2 == 1]

arr := [1, 2, 3, 4, 5, 6]
d := [[a, b] for a <- arr if a < b for b <- arr if b > 2]

x := {x: i for i, x <- [1, 3, 5, 7, 11]}
y := {x: i for i, x <- [1, 3, 5, 7, 11] if i%2 == 1}
z := {v: k for k, v <- {1: "Hello", 3: "Hi", 5: "xsw", 7: "Go+"} if k > 3}
```

How to use them? Here is an example:
<h5 align="right"><a href="#table-of-contents">⬆ back to toc</a></h5>

```go
import (
"strconv"
)

func add(x, y string) (int, error) {
return strconv.Atoi(x)? + strconv.Atoi(y)?, nil
}
### Select data from a collection

func addSafe(x, y string) int {
return strconv.Atoi(x)?:0 + strconv.Atoi(y)?:0
```go
type student struct {
name string
score int
}

println `add("100", "23"):`, add("100", "23")!
students := [student{"Ken", 90}, student{"Jason", 80}, student{"Lily", 85}]

sum, err := add("10", "abc")
println `add("10", "abc"):`, sum, err
unknownScore, ok := {x.score for x <- students if x.name == "Unknown"}
jasonScore := {x.score for x <- students if x.name == "Jason"}

println `addSafe("10", "abc"):`, addSafe("10", "abc")
println unknownScore, ok // 0 false
println jasonScore // 80
```

The output of this example is:
<h5 align="right"><a href="#table-of-contents">⬆ back to toc</a></h5>

```
add("100", "23"): 123
add("10", "abc"): 0 strconv.Atoi: parsing "abc": invalid syntax

===> errors stack:
main.add("10", "abc")
/Users/xsw/tutorial/15-ErrWrap/err_wrap.gop:6 strconv.Atoi(y)?
### Check if data exists in a collection

addSafe("10", "abc"): 10
```
```go
type student struct {
name string
score int
}

Compared to corresponding Go code, It is clear and more readable.
students := [student{"Ken", 90}, student{"Jason", 80}, student{"Lily", 85}]

And the most interesting thing is, the return error contains the full error stack. When we got an error, it is very easy to position what the root cause is.
hasJason := {for x <- students if x.name == "Jason"} // is any student named Jason?
hasFailed := {for x <- students if x.score < 60} // is any student failed?
```

How these `ErrWrap expressions` work? See [Error Handling](https://github.com/goplus/gop/wiki/Error-Handling) for more information.
<h5 align="right"><a href="#table-of-contents">⬆ back to toc</a></h5>


## Unix shebang
Expand Down

0 comments on commit 5e37f69

Please sign in to comment.