From 07f0fd1d3f24c88ce6e87936c4dd91ed885f9cbd Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Fri, 12 Jul 2024 18:29:55 -0400 Subject: [PATCH] allow models to also be executables --- README.md | 33 ++++++++++++++++++++++++++++----- fermi.go | 17 +++++++++++++++-- more/executable-model.fermi | 10 ++++++++++ 3 files changed, 53 insertions(+), 7 deletions(-) create mode 100755 more/executable-model.fermi diff --git a/README.md b/README.md index 520962a..2f2181e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# A minimalist calculator for fermi estimation +# A calculator for distributions, for Fermi estimation -This project is a minimalist, calculator-style DSL for fermi estimation. It can multiply, divide, add and substract scalars, lognormals and beta distributions. +This project is a minimalist, calculator-style DSL for fermi estimation. It can multiply, divide, add and substract scalars, lognormals and beta distributions, and supports variables. ## Motivation @@ -101,8 +101,31 @@ $ fermi -n 1_000_000 - It's conceptually clearer to have all the multiplications first and then all the divisions - For things between 0 and 1, consider using a beta distribution -- Because the model reads from standard input, you can pipe a model to it. For instance, try cat more/piano-tuners.f | fermi -- You can also save a session to a logfile with tee. Try fermi | tee -a fermi.log + + +### Integrations with linux utilities + +Because the model reads from standard input, you can pipe a model to it: + +``` +$ cat more/piano-tuners.f | fermi +``` + +You can also + +...which means a model an executable file by adding the following at the top! + +``` +#!/bin/usr/fermi -f +``` + + +You can save a session to a logfile with tee: + +``` +fermi | tee -a fermi.log + +``` ## Different levels of complexity @@ -143,10 +166,10 @@ Done: - [x] Allow input with K/M/T - [x] Document parenthesis syntax - [x] Specify number of samples as a command line option +- [x] Figure out how to make models executable, by adding a #!/bin/bash-style command at the top? To (possibly) do: -- [ ] Figure out how to make models executable, by adding a #!/bin/bash-style command at the top? - [ ] Add functions. Now easier to do with an explicit representation of the stakc - [ ] Think about how to draw a histogram from samples - [ ] Dump samples to file diff --git a/fermi.go b/fermi.go index c24cdbe..c84c7c3 100644 --- a/fermi.go +++ b/fermi.go @@ -411,10 +411,23 @@ replForLoop: func main() { num_samples_flag := flag.Int("n", N_SAMPLES, "Specifies the number of samples to draw when using samples") - flag.Parse() N_SAMPLES = *num_samples_flag + filename := flag.String("f", "", "Specifies a file with a model to run") + flag.Parse() - reader := bufio.NewReader(os.Stdin) + var reader *bufio.Reader = nil + if *filename != "" { + file, err := os.Open(*filename) + if err == nil { + reader = bufio.NewReader(file) + } else { + fmt.Printf("Error opening filename; reading from stdin instead\n") + } + } + if reader == nil { + reader = bufio.NewReader(os.Stdin) + } stack := Stack{old_dist: INIT_DIST, vars: make(map[string]Dist)} runRepl(stack, reader) + } diff --git a/more/executable-model.fermi b/more/executable-model.fermi new file mode 100755 index 0000000..1562834 --- /dev/null +++ b/more/executable-model.fermi @@ -0,0 +1,10 @@ +#!/bin/fermi -f +5M 12M # number of people living in Chicago +beta 1 200 # fraction of people that have a piano +30 180 # minutes it takes to tune a piano, including travel time +/ 48 52 # weeks a year that piano tuners work for +/ 5 6 # days a week in which piano tuners work +/ 6 8 # hours a day in which piano tuners work +/ 60 # minutes to an hour +=: piano_tuners +exit