preview
I’ve been enjoying learning ocaml, but I found it very easy to write code riddled with side effects and imperative-ness.
Take this example:
let some_func arg =
let input = format_input_from_stdin ()
let read_content = read_file "some/file/path.txt"
let to_write = get_write_file_content input read_content
let () = write_file "some/other/path.txt" to_write
let output = run_external_command_with_output
(output, read_content)
As you can see, many side effects and imperative steps in the code. Is there a better practice for coding this in a functional manner?
I’ve been enjoying learning ocaml, but I found it very easy to write code riddled with side effects and imperative-ness.
Take this example:
let some_func arg =
let input = format_input_from_stdin ()
let read_content = read_file "some/file/path.txt"
let to_write = get_write_file_content input read_content
let () = write_file "some/other/path.txt" to_write
let output = run_external_command_with_output
(output, read_content)
As you can see, many side effects and imperative steps in the code. Is there a better practice for coding this in a functional manner?
preview
I just started to learn Ocaml to learn functional programming. I will use it to build a CLI that’s mostly orchestrating other programs.
My experience is mostly in JS / TS, but I’ve also coded a good bit in Python and Lua.
Below, I provided a list of things I learned or focused on while using OCaml. But I feel like I must be missing something. This is only moderately different from what I’m used to in JS. I expected something more radical. Moreover, I constantly hear a lot of FP jargon (like “highly kinded types”, monads, etc) that I feel am still missing.
So far, here’s what I studied:
- immutability
- avoid side affects
- static typing
- recursion instead of loops
- option / maybe
- higher order functions
- conditionals and other constructs as expressions, when they’re statements in other languages
- pipelines and functions as input —> output
- currying
- scoping with let
What am I missing?
I just started to learn Ocaml to learn functional programming. I will use it to build a CLI that’s mostly orchestrating other programs.
My experience is mostly in JS / TS, but I’ve also coded a good bit in Python and Lua.
Below, I provided a list of things I learned or focused on while using OCaml. But I feel like I must be missing something. This is only moderately different from what I’m used to in JS. I expected something more radical. Moreover, I constantly hear a lot of FP jargon (like “highly kinded types”, monads, etc) that I feel am still missing.
So far, here’s what I studied:
- immutability
- avoid side affects
- static typing
- recursion instead of loops
- option / maybe
- higher order functions
- conditionals and other constructs as expressions, when they’re statements in other languages
- pipelines and functions as input —> output
- currying
- scoping with let
What am I missing?