Examples
This page contains some cool code snippets that I wrote whilst developing Menter. They are meant to show off some of the features of Menter and how they can be used.
Most of them are useless without a proper Guide Server, so make sure to set one up!
Derivative
This code snippet defines a function called derivative
which takes two parameters: p
(precision) and f
(the function to differentiate). This function calculates the approximate derivative of the given function using the symmetric difference quotient formula.
When plotting the sin
function, the function itself is passed and then called by the plot
method. Even more interesting is the second part with derivative(0.1, sin)
, which works, since the derivative
function returns a function that takes one parameter.
Table-like structures (Database)
This code snippet generates synthetic data related to card shops, products, and offers, and performs some data filtering and visualization.
It initializes generator functions for creating random data, such as the names of legendary duelists, crystal beasts, and attack styles. It also sets up the producers and shops associated with the card products.
Using these generators, the code creates tables for card shops, products, and offers by generating a specified number of entries for each. The tables are populated with randomly generated data, including unique product identifiers (GTIN), names, producers, prices, and shop names.
After generating the data, the code applies a filter to the offer table, selecting only the offers for products with the name "Pokemon Ultra" and a price less than 5. This filtered data is then processed to retain only specific keys (name, shop, price) and rename them for clarity.
Finally, the filtered data is visualized using the cmdplot
module. The resulting table displays the name, shop, and price of the filtered offers. This allows users to easily analyze and explore the generated data in a structured format.
Some special things to note about this code:
This line defines a function that picks a random element from an array. It uses the |>
operator to pipe the array size into the random
function, which is then piped into the floor
function.
This line creates 300 products by mapping the generator.product
function over a range of 300 numbers. This is a convenient way to do something a specific number of times.
This line uses the retainKey
function to keep only the keys "name", "shop", and "price" in each offer. It uses a lambda function to specify which keys to keep.
This snippet and the one below are equivalent, but differ in the way they are written.
The first one separates the merging of the tables on their "primary key" from the filtering of the data, making it easier to read.
The second one merges the tables and filters the data in a single step, making it more concise.
Normal distribution
This code snippet defines a function normalDistribution
that calculates the probability density function of a normal distribution for a given value x
, mean mu
, and standard deviation sigma
. It is exported as a module under the name normal
. This is so that the other code boxes below can access it.
The next box uses this normalDistribution
function to plot a normal distribution with mean 0 and standard deviation 1.
And another big example. This one generates a large amount of ages from 0 to 30 using the normal distribution function defined above. It then plots the frequency of each age. More details about specific parts can be found below.
A function is defined that generates a random age between min
and max
using the normal distribution function. It has a parameter attempts
that specifies how many times the function should be called to generate a single age, where the one with the highest probability is chosen.
The most interesting part of this code is the frequency
function. It takes an array of values and returns a map that maps each value to its frequency in the array. This is used to plot the frequency of each age.