Skip to content

Commit

Permalink
fix flatten and compact
Browse files Browse the repository at this point in the history
  • Loading branch information
vituri committed May 9, 2024
1 parent 72687d6 commit 91bde74
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
20 changes: 14 additions & 6 deletions docs/src/apply.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ In base Julia there is already the `map` function, but

- The function is the first argument, and the collection is the second. This make it less "pipeable".

## `f` is a one-variable function
We will cover some common cases below.

## One variable, one collection

Given a collection `x` and a one-variable function `f`, we can apply `f` to each element of `x` as follows:

Expand Down Expand Up @@ -62,9 +64,15 @@ Every member of the apply family has a optional named argument `T` which is a fu
apply(x, f, T = string)
```

## `f` is a two-variable function and we have two collections
This is the same as

```@example 1
apply(x, string ∘ f)
```

## Two variables, two collections

We can also apply a two-variable function `f` to two collections `x` and `y` by applying `f` to each pair `(x_i, y_i)` where `x_i` is the `i-th` element of `x` and `y_i` the `i-th` element of `y`. If `x` and `y` have different sizes, we iterate until one of them ends.
We can apply a two-variable function `f` to two collections `x` and `y` by applying `f` to each pair `(x_i, y_i)` where `x_i` is the `i-th` element of `x` and `y_i` the `i-th` element of `y`. If `x` and `y` have different sizes, we iterate until one of them ends.

```@example 2
using TidierIteration;
Expand All @@ -87,9 +95,9 @@ d2 = Dict(i => i^2 for i in [3:9;])
apply2(d1, d2, f)
```

## `f` is a two-variable function and we have only one collection
## Two variables, one collection

In this case, we can use the index of each element of `x` as the first variable to be applied on `f, that is, we apply `f` on the pairs `(i, x_i)` for each index `i` of `x`.
In this case, we can use the index of each element of `x` as the first variable to be applied on `f`, that is, we apply `f` on the pairs `(i, x_i)` for each index `i` of `x`. It is important to note that `i` is the first argument to be passed to `f`.

```@example 3
using TidierIteration;
Expand All @@ -108,7 +116,7 @@ g(k, v) = k + v
iapply(d, g)
```

## Converting to dataframe
## One variable and one collection, dataframe output

When the output of `f` is a dataframe, we can bind all rows (or columns) quickly as follows:

Expand Down
2 changes: 1 addition & 1 deletion src/flatten.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,5 @@ to_json(x::Dict) = x |> json_string
to_json(x::Vector) = x |> json_string

function flatten_json(d)
map_asis(d, to_json)
apply(d, to_json)
end
3 changes: 3 additions & 0 deletions src/predicates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ function is_empty(x)
length(x) == 0
end

function is_empty(x::Nothing)
true
end

"""
is_non_empty(x)
Expand Down
4 changes: 4 additions & 0 deletions src/zzz tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ x
modify_if!(x, isodd, x -> x^3)
x


x = [1, nothing, [], "a", [2, 3]]
compact(x)

# predicates.jl
x = [1, [1, 2], [], Dict()]
compact(x)
Expand Down

0 comments on commit 91bde74

Please sign in to comment.