Idiomatic Pluto flow control

I’d like some advice on how to idiomatically use flow control with Pluto.

Here is a minimal example in pseudo code. Let’s say I want to do conditional evaluation. Here’s bare pseudo code of what I’m after that I’d use outside of Pluto:

if isfile(parquet_file_path)
  df = dataframe_from_parquet_file(parquet_file_path)
  df = dataframe_from_csv(csv_file_path)

This is contrived, and kind of stupid as presented, but is suitable for the questions I have.

I could take the above code and wrap it in a begin end block and use it in Pluto.

Alternatively, I could create a series Pluto cells, like this:

parquet_df = isfile(parquet_file_path) && dataframe_from_parquet_file(parquet_file_path) 
csv_df = dataframe_from_csv(csv_file_path)
df = parquet_df || csv_df 

In both cases, I’d end up with a df variable with the desired dataframe. The latter trades readability for strict compliance with having a single expression per cell. The former is nicer to read and, since it’s arguably not important for that code to be responsive, maybe it’s okay.

Since either work, there isn’t an absolute answer to this. Which (if either) is idiomatic? Is there a third, better choice?


1 Like

The if statement is one expression, that’s different in Julia than in Python, for example. You can also write

df = if condition

The short form for this is the ternary operator

df = condition ? a : b