Skip to content

Instantly share code, notes, and snippets.

@ToJans
Last active August 29, 2015 14:13
Show Gist options
  • Save ToJans/66c3e4278677d1e0178b to your computer and use it in GitHub Desktop.
Save ToJans/66c3e4278677d1e0178b to your computer and use it in GitHub Desktop.
A proposition to make f# docs a lot easier and testable, like in @elixirlang : http://elixir-lang.org/docs/stable/ex_unit/ExUnit.DocTest.html
(**
### Evaluation demo
The following is a simple calculation: *)
let test = 40 + 2
(** We can print it as follows: *)
(*** define-output:test ***)
printf "Result is: %d" test
(** The result of the previous snippet is: *)
(*** include-output:test ***)
(** And the variable `test` has the following value: *)
(*** include-value: test ***)
(** Sums a list of integers.
## Remarks
- Produces an `ArgumentException` for an empty list.
- It would be better if we'd use `List.fold` with a `0`, so it doesn't throw upon an empty list.
## Examples
```F#
> let somelist = [1;2;3;4];;
> listsum somelist;;
val it : int = 10
> listsum [];;
System.ArgumentException...
> listsum [4;5];;
val it : int = 9
```
In a codeblock:
- If the source line starts with `>`:
- execute it and store the output lines
- If a source line doesn't start with `>`:
- If the source line ends with `...` compare everything in front of it with the current output line
- If it doesn't, compare the source line with the current output line
- go to the next output line & source line
*)
let listsum = List.reduce (+)

listsum:: int list -> int

Sums a list of integers.

Remarks

  • Produces an ArgumentException for an empty list.
  • It would be better if we'd use List.fold with a 0, so it doesn't throw upon an empty list.

Examples

> let somelist = [1;2;3;4];;
> listsum somelist;;
val it : int = 10
> listsum [];;
System.ArgumentException...
> listsum [4;5];;
val it : int = 9

In a codeblock:

  • If the source line starts with >:
    • execute it and store the output lines
  • If a source line doesn't start with >:
    • If the source line ends with ... compare everything in front of it with the current output line
    • If it doesn't, compare the source line with the current output line
  • go to the next output line & source line
@ToJans
Copy link
Author

ToJans commented Jan 8, 2015

The fsharp code tags could be easily tested like this, and not a lot of extra parsing is needed (as opposed to your code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment