Crate cbqn

source · []
Expand description

cbqn

A crate for running BQN code within a Rust program using CBQN interpreter compiled as a shared object.

Usage

Simple expressions can be run with the BQN! convenience macro. For more advanced use, the methods of BQNValue provide the necessary functionality.

Examples using the BQN! macro

let sum = BQN!("1+1");
assert_eq!(sum.to_f64(), 2.0);
assert_eq!(BQN!("⌽≡⊢", "BQN").to_f64(), 0.0);
let strs = BQN!(' ', "(⊢-˜+`׬)∘=⊔⊢", "Rust ❤️ BQN")
    .to_bqnvalue_vec()
    .iter()
    .map(BQNValue::to_string)
    .collect::<Vec<String>>();
assert_eq!(strs, ["Rust", "❤️", "BQN"]);
let strings = ["join", "these", "please"];
assert_eq!(BQN!("∾", strings).to_string(), "jointheseplease");

Examples using BQNValue

let sum = eval("1+1");
assert_eq!(sum.to_f64(), 2.0);
let is_anagram = eval("⌽≡⊢");
assert_eq!(is_anagram.call1(&"BQN".into()).to_f64(), 0.0);
let split = eval("(⊢-˜+`׬)∘=⊔⊢");
let strs = split.call2(&' '.into(), &"Rust ❤️ BQN".into())
    .to_bqnvalue_vec()
    .iter()
    .map(BQNValue::to_string)
    .collect::<Vec<String>>();
assert_eq!(strs, ["Rust", "❤️", "BQN"]);
let counter = BQN!("{v←0 ⋄ Inc⇐{v+↩𝕩}}");
let increment = counter.get_field("inc").unwrap();
increment.call1(&1.into());
increment.call1(&2.into());
let result = increment.call1(&3.into());
assert_eq!(result.to_f64(), 6.0);

Macros

Convenience macro for running BQN expressions

Structs

Represents a BQN value

Enums

BQN type enumeration

Functions

Evaluates BQN code