Skip to content

Commit

Permalink
Merge branch 'main' into iterator-find-map
Browse files Browse the repository at this point in the history
  • Loading branch information
darky authored Apr 30, 2024
2 parents 2cc18ee + c91845d commit ae8ef41
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- The `list` module gains the `wrap` function.
- The `iterator.find_map` has been added.

## v0.37.0 - 2024-04-19
Expand Down
20 changes: 20 additions & 0 deletions src/gleam/list.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,26 @@ pub fn new() -> List(a) {
[]
}

/// Returns the given item wrapped in a list.
///
/// ## Examples
///
/// ```gleam
/// wrap(1)
/// // -> [1]
///
/// wrap(["a", "b", "c"])
/// // -> [["a", "b", "c"]]
///
/// wrap([[]])
/// // -> [[[]]]
/// ```
///
///
pub fn wrap(item: a) -> List(a) {
[item]
}

/// Joins one list onto the end of another.
///
/// This function runs in linear time, and it traverses and copies the first
Expand Down
20 changes: 20 additions & 0 deletions test/gleam/list_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,26 @@ pub fn new_test() {
|> should.equal([])
}

pub fn wrap_test() {
list.wrap([])
|> should.equal([[]])

list.wrap([[]])
|> should.equal([[[]]])

list.wrap(Nil)
|> should.equal([Nil])

list.wrap(1)
|> should.equal([1])

list.wrap([1, 2])
|> should.equal([[1, 2]])

list.wrap([[1, 2, 3]])
|> should.equal([[[1, 2, 3]]])
}

pub fn append_test() {
list.append([1], [2, 3])
|> should.equal([1, 2, 3])
Expand Down

0 comments on commit ae8ef41

Please sign in to comment.