dry-python / dry-python/returns

Adding some utility methods to Maybe/Result

Đang mở
#1,114 20 bình luận 7 reaction 0 người được giao Xem trên GitHub
enhancement
Ngôn ngữ chính
Python
Star
4.4k
Fork
155
Merge trung bình
2 giờ 27 phút
Pull request đã merge (30 ngày)
22

Mô tả

Have you considered adding some handy utility functions to `Maybe` and `Result`?
In using `returns` I'm missing many helpful methods you can find in Rust, for example.
Adding a few of these could increase the usability of the containers of `returns`.

Related: #1091

Here they are, in my subjective order of usefulness:

## Setdefault

Set a value if one is not yet present.
The name `setdefault` would be consistent with the python dict method.
(Although it might be good to choose another name to make clear that
the method doesn't mutate.)

```python
>>> Some(6).setdefault(7)
Some(6)
>>> Nothing.setdefault(3)
Some(3)
```

## Zip

Combine the values from two containers into a tuple, if both are successful.

```python
>>> Some(8).zip(Some(2))
Some((8, 2))
>>> Some(7).zip(Nothing)
Nothing
>>> Nothing.zip(Some(3))
Nothing
>>> Nothing.zip(Nothing)
Nothing
```

## Filter

Keep a value based on a predicate

```python
>>> Some("9").filter(str.isdigit)
Some("9")
>>> Some("foo3").filter(str.isdigit)
Nothing
>>> Nothing.filter(str.isdigit)
Nothing
```

## And

`AND` logic operation

```python
>>> Some(5).and_(Some(9))
Some(9)
>>> Some(9).and_(Nothing)
Nothing
>>> Nothing.and_(Some(8))
Nothing
```

## Or

`OR` logic operation

```python
>>> Some(5).or_(Some(9))
Some(5)
>>> Some(9).or_(Nothing)
Some(9)
>>> Nothing.or_(Some(8))
Some(8)
>>> Nothing.or_(Nothing)
Nothing
```

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.