# Ocaml counterpart for \`continue\` and \`return\` in programming workflow

**URL:** <https://discuss.ocaml.org/t/ocaml-counterpart-for-continue-and-return-in-programming-workflow/12601>\
**Category:** Learning\
**Created:** [July 13, 2023, 2:27pm UTC](https://discuss.ocaml.org/t/ocaml-counterpart-for-continue-and-return-in-programming-workflow/12601 "2023-07-13T14:27:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vivanov](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/vivanov/32/4455_2.png) [@vivanov](https://discuss.ocaml.org/u/vivanov)\
**Post date:** [July 13, 2023, 2:27pm UTC](https://discuss.ocaml.org/t/ocaml-counterpart-for-continue-and-return-in-programming-workflow/12601/1 "2023-07-13T14:27:04Z")

</div>

Hey, in python I would have the following:

```auto
def f(x): 
  fn = get_fn(x)
  if fn is None: 
    return

  df = read_csv(fn)
  if df is None: 
    return 

  x = df.mean()
  return x

```

I guess the corresponding approach in Ocaml would be:

```auto
let f x = 
  get_fn x
  >>|
  (
    fun fn -> read_csv fn
  )
  >>|
  (
    fun df -> calc_mean df
  )

```

I encounter issues here when the last pipe depends on the output from the first pipe: I end up packing the output into a tuple:

```auto
def f(x): 
  fn = get_fn(x)
  if fn is None: 
    return

  df = read_csv(fn)
  if df is None: 
    return 

  if 'mean' in fn: 
    x = df.mean()
    return x
  else:
    x = df.std()
    return x

```

In ocaml:

```auto
let f x = 
  get_fn x
  >>|
  (
    fun fn -> 
      (read_csv fn, fn)
  )
  >>|
  (
    fun (df, fn) ->
    match String.find fn "mean" with 
    | true -> calc_mean df
    | false -> calc_std df
  )

```

Is this how things are supposed to be?

---

<div class="post-metadata">

**Author:** ![SkySkimmer](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/skyskimmer/32/1672_2.png) [@SkySkimmer](https://discuss.ocaml.org/u/SkySkimmer)\
**Post date:** [July 13, 2023, 2:44pm UTC](https://discuss.ocaml.org/t/ocaml-counterpart-for-continue-and-return-in-programming-workflow/12601/2 "2023-07-13T14:44:12Z")

</div>

Assuming `(>>|)` is `Option.bind`, I think this would be more idiomatic

```auto
let f x =
  get_fn x >>| fun fn ->
  read_csv fn >>| fun df ->
  match String.find fn "mean" with
  | true -> calc_mean df
  | false -> calc_std df

```

(this is

```auto
let f x =
  get_fn x >>| (fun fn ->
  read_csv fn >>| (fun df ->
  match String.find fn "mean" with
  | true -> calc_mean df
  | false -> calc_std df))

```

but writing those parentheses is useless and would make auto-indenters drift to the right)

---

<div class="post-metadata">

**Author:** ![bennos](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bennos/32/3748_2.png) [@bennos](https://discuss.ocaml.org/u/bennos)\
**Post date:** [July 13, 2023, 3:16pm UTC](https://discuss.ocaml.org/t/ocaml-counterpart-for-continue-and-return-in-programming-workflow/12601/3 "2023-07-13T15:16:38Z")

</div>

If you parenthesize less aggressively, you should still have `fn` in scope when you need it, without constructing that tuple.

Also, your boolean pattern-match could instead be an if-then-else.

e.g.

```auto
let f x =
  get_fn x >>= fun fn ->
  read_csv fn >>| fun df ->
  if String.find fn "mean" then
    calc_mean df
  else
    calc_std df

```

This could equivalently be written with let-ops, e.g.

```auto
let f x =
  let* fn = get_fn x in
  let+ df = read_csv fn in
  if String.find fn "mean" then
    calc_mean df
  else
    calc_std df

```

(That is all without seeing your actual definitions/types, of course – I’m assuming the standard `(>>|)` = `let+` = `Option.map` and `(>>=)` = `let*` = `Option.bind`)

---

<div class="post-metadata">

**Author:** ![vivanov](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/vivanov/32/4455_2.png) [@vivanov](https://discuss.ocaml.org/u/vivanov)\
**Post date:** [July 13, 2023, 4:08pm UTC](https://discuss.ocaml.org/t/ocaml-counterpart-for-continue-and-return-in-programming-workflow/12601/4 "2023-07-13T16:08:53Z")

</div>

Thanks, that makes sense. Looks way cleaner now 🙂

---

<div class="post-metadata">

**Author:** ![vivanov](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/vivanov/32/4455_2.png) [@vivanov](https://discuss.ocaml.org/u/vivanov)\
**Post date:** [July 13, 2023, 4:18pm UTC](https://discuss.ocaml.org/t/ocaml-counterpart-for-continue-and-return-in-programming-workflow/12601/5 "2023-07-13T16:18:18Z")

</div>

Hey @bennos , how do I install the let\* and let+ support? I presume this is some ppx package.

---

<div class="post-metadata">

**Author:** ![bennos](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bennos/32/3748_2.png) [@bennos](https://discuss.ocaml.org/u/bennos)\
**Post date:** [July 13, 2023, 4:46pm UTC](https://discuss.ocaml.org/t/ocaml-counterpart-for-continue-and-return-in-programming-workflow/12601/6 "2023-07-13T16:46:56Z")

</div>

those are “[let operator](https://v2.ocaml.org/manual/bindingops.html#let-operator)” syntax actually, not ppx. Depending on your standard library choices they may be available with `open Option.Let_syntax` or you can set up your project with something along the lines of `let (let*) = Option.bind` and `let (let+) x f = Option.map f x`
