squiggle/packages/website/docs/Guides/Language.mdx

108 lines
2.3 KiB
Plaintext
Raw Permalink Normal View History

2022-03-25 02:08:46 +00:00
---
sidebar_position: 1
2022-04-27 20:28:10 +00:00
title: Language Basics
2022-03-25 02:08:46 +00:00
---
2022-04-11 00:13:11 +00:00
import { SquiggleEditor } from "../../src/components/SquiggleEditor";
2022-03-25 02:08:46 +00:00
2022-07-28 04:08:10 +00:00
Squiggle supports some simple types and language features.
2022-07-28 04:08:10 +00:00
## Numbers
<SquiggleEditor defaultCode="4.32" />
2022-07-28 04:08:10 +00:00
## Distributions
2022-07-29 00:00:06 +00:00
2022-07-28 04:08:10 +00:00
There are several ways of easily entering distributions. See the [documentation](/docs/Api/Dist/) on distributions for a complete API.
2022-07-23 18:41:12 +00:00
<SquiggleEditor
defaultCode={`a = normal(4,2)
b = 30 to 50
c = lognormal({mean:90, stdev: 7})
2022-07-28 04:08:10 +00:00
d = mixture(a,b,c, [.3, .3, .4])
d`}
2022-07-23 18:41:12 +00:00
/>
2022-07-28 04:08:10 +00:00
## Lists
2022-07-29 00:00:06 +00:00
2022-07-28 04:08:10 +00:00
Squiggle lists can accept items of any type, similar to those in Python. [API](/docs/Api/List).
2022-03-25 02:08:46 +00:00
2022-04-10 23:15:46 +00:00
<SquiggleEditor
defaultCode={`[beta(1,10), 4, isNormalized(SampleSet.fromDist(1 to 2))]`}
2022-04-10 23:15:46 +00:00
/>
2022-03-25 02:08:46 +00:00
2022-07-28 04:08:10 +00:00
## Dictionaries
2022-07-29 00:00:06 +00:00
2022-07-28 04:08:10 +00:00
Squiggle dictionaries work similarly to Python dictionaries. [API](/docs/Api/Dictionary).
2022-03-25 02:08:46 +00:00
<SquiggleEditor
defaultCode={`d = {dist: triangular(0, 1, 2), weight: 0.25}
d.dist`}
/>
2022-03-25 02:08:46 +00:00
2022-07-28 04:08:10 +00:00
## Functions
2022-03-25 02:08:46 +00:00
<SquiggleEditor
defaultCode={`f(t) = normal(t^2, t^1.2+.01)
f`}
/>
2022-07-28 04:08:10 +00:00
## Anonymous Functions
2022-03-25 02:08:46 +00:00
2022-07-23 18:41:12 +00:00
<SquiggleEditor defaultCode={`{|t| normal(t^2, t^1.2+.01)}`} />
2022-07-28 04:08:10 +00:00
## Comments
2022-07-27 23:54:25 +00:00
2022-07-29 00:00:06 +00:00
<SquiggleEditor
defaultCode={`// This is a single-line comment\n
2022-07-27 23:54:25 +00:00
/*
This is a multiple
-line comment.
*/
2022-07-28 04:08:10 +00:00
""
2022-07-29 00:00:06 +00:00
`}
/>
2022-07-27 23:54:25 +00:00
2022-07-28 04:08:10 +00:00
## Pipes
Squiggle features [data-first](https://www.javierchavarri.com/data-first-and-data-last-a-comparison/) pipes. Functions in the standard library are organized to make this convenient.
2022-07-29 00:00:06 +00:00
<SquiggleEditor
defaultCode={`normal(5,2) |> truncateLeft(3) |> SampleSet.fromDist |> SampleSet.map({|r| r + 10})`}
/>
2022-07-28 04:08:10 +00:00
## Standard Library
Squiggle features a simple [standard libary](/docs/Api/Dist).
Most functions are namespaced under their respective types to keep functionality distinct. Certain popular functions are usable without their namespaces.
For example,
2022-07-29 00:00:06 +00:00
<SquiggleEditor
defaultCode={`a = List.upTo(0, 5000) |> SampleSet.fromList // namespaces required
2022-07-28 04:08:10 +00:00
b = normal(5,2) // namespace not required
c = 5 to 10 // namespace not required
2022-07-29 00:00:06 +00:00
""`}
/>
2022-07-28 04:08:10 +00:00
## Number Prefixes
2022-07-29 00:00:06 +00:00
2022-07-28 04:08:10 +00:00
Numbers support a few scientific notation prefixes.
2022-07-29 00:00:06 +00:00
| prefix | multiplier |
| ------ | ---------- |
| n | 10^-9 |
| m | 10^-3 |
| k | 10^3 |
| M | 10^6 |
| B,G | 10^9 |
| T | 10^12 |
| P | 10^15 |
<SquiggleEditor
defaultCode={`simpleNumber = 4.32k
2022-07-28 04:08:10 +00:00
distribution = 40M to 50M
2022-07-29 00:00:06 +00:00
distribution`}
/>