Code Monkey home page Code Monkey logo

Comments (8)

 avatar commented on June 4, 2024

Without thinking implementation yet, I am thinking something like the following could be an idea, and can be kept simple in implementation...

mtcars %>% Or(nrow, when = rowcount)

more generally,

x %>% Or(fun, when)

where fun is a function reference or a lambda, and when is a (single-valued) condition.

perhaps something like

Or <- function(value, fun, when) 
{
    if (when) fun(value) else value
}

from magrittr.

smbache avatar smbache commented on June 4, 2024

my comment above... wrong login...

from magrittr.

wch avatar wch commented on June 4, 2024

This sounds reasonable... I think that, as a name "Or" is too much like a logical OR. Maybe something like fif would be appropriate.

A couple more thoughts:

  • I like the idea of allowing an else function.
  • It makes more sense to me to have the condition first, before the function(s) to run.
fif <- function(x, cond, if_fun, else_fun = identity) {
  if(cond) if_fun(x)
  else else_fun(x)
}

# Some examples
mtcars %>% fif(TRUE, nrow)
mtcars %>% fif(FALSE, nrow)
mtcars %>% fif(TRUE, nrow, head)
mtcars %>% fif(FALSE, nrow, head)

from magrittr.

smbache avatar smbache commented on June 4, 2024

Right, "Or" is probably not the best -- I quite like when one can "read" the statements.
Another option could be

x %>% when(condition, otherwise = fun)

which can also be "read". Could also have the additional (optional/identity) argument

x %>% when(condition, do = fun1, otherwise = fun2)

although the way this is read is a little different than the first "sentence".

Just a thought.

You're suggestion is fine, although a little insider knowledge is needed to figure out what fif means...

from magrittr.

wch avatar wch commented on June 4, 2024

That makes sense.

I also realized that you'd probably want to capture the unevaluated function call, so that you could pass in additional arguments. Using your suggestion of function, it would be something like

x %>% when(cond, fun1(opt1, opt2))

So that when cond is TRUE, it would call fun1(x, opt1, opt2).

from magrittr.

smbache avatar smbache commented on June 4, 2024

Right, and we should consider whether the condition should be allowed to depend on x, e.g.

x %>% when(is.na %>% any, do = fun1(opt1, opt2), 
           otherwise = fun2(opt3, opt4))

I am thinking we could implement all this using the defer function we discussed previously.

Something like (this won't work directly, just to show the idea.)

when <- function(x, condition, do = identity, otherwise = NULL) 
{
    cond <- defer(condition)(x)
    if (cond) defer(do)(x) else defer(otherwise)(x)
}

This would be nice if condition depends on x but more annoying when it doesn't.
Maybe one could think of a syntax to distinguish.

from magrittr.

gaborcsardi avatar gaborcsardi commented on June 4, 2024

This is also my number one wanted feature. How about some syntax like this:

input %>%
  branch(is.data.fame() %>%
         as.matrix() %>% 
         ensure_symmetric() ) %>%
  graph.adjacency()

branch takes a bunch of chains, the first element of the chain is a logical expression. If all chains inside of branch are false, then nothing happens to the data, it skips the branch entirely.

Here is another example with a three-way branch:

input %>%
  branch(is.data.frame() %>% graph.data.frame(),
         is.matrix() %>% graph.adjacency(),
        ) %>%
  plot()

EDIT:

from magrittr.

smbache avatar smbache commented on June 4, 2024

This at all interesting anymore, as you can do

mtcars %>% {if(rowcount) nrow(.) else .}

to take the initial example? If so, you're welcome to re-open...

from magrittr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.