From 921377c2cc7e0ee036c629442f3ec1075d851ca4 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Mon, 19 Apr 2021 05:02:46 +0200 Subject: [PATCH] some prototype mini utils --- math/fib/Cargo.toml | 9 +++++++ math/fib/src/main.rs | 58 ++++++++++++++++++++++++++++++++++++++++ math/int2str/Cargo.toml | 9 +++++++ math/int2str/src/main.rs | 38 ++++++++++++++++++++++++++ math/str2int/Cargo.toml | 9 +++++++ math/str2int/src/main.rs | 40 +++++++++++++++++++++++++++ 6 files changed, 163 insertions(+) create mode 100644 math/fib/Cargo.toml create mode 100644 math/fib/src/main.rs create mode 100644 math/int2str/Cargo.toml create mode 100644 math/int2str/src/main.rs create mode 100644 math/str2int/Cargo.toml create mode 100644 math/str2int/src/main.rs diff --git a/math/fib/Cargo.toml b/math/fib/Cargo.toml new file mode 100644 index 0000000..7f4efbb --- /dev/null +++ b/math/fib/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "fib" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +nested = { path = "../../nested" } diff --git a/math/fib/src/main.rs b/math/fib/src/main.rs new file mode 100644 index 0000000..ac3d1bc --- /dev/null +++ b/math/fib/src/main.rs @@ -0,0 +1,58 @@ + +use std::{ + fs::File, + io::{Read, Write}, + os::unix::io::FromRawFd +}; + +fn fib(n: u64) -> u64 { + let mut y = 0; + let mut y1 = 1; + let mut y2 = 0; + + for _ in 0 .. n { + y = y1 + y2; + y2 = y1; + y1 = y; + } + + y +} + +fn main() { + nested::magic_header(); + + eprintln!(" Fibonacci Sequence"); + + nested::magic_header(); + + eprintln!(" +interface (Sequence ℕ) 0 1"); + + let mut f0 = unsafe { File::from_raw_fd(0) }; + eprintln!(" +> 0: n + ( ℕ ) + ( MachineInt ) + ( MachineWord ) + ( Pipe Shot MachineWord ) +"); + + let mut f1 = unsafe { File::from_raw_fd(1) }; + eprintln!(" +< 1: n'th fibonacci number + ( ℕ ) + ( MachineInt ) + ( MachineWord ) + ( Pipe Shot MachineWord ) +"); + + nested::magic_header(); + + let mut bytes = [0 as u8; 8]; + f0.read_exact(&mut bytes); + let n = u64::from_le_bytes(bytes); + bytes = fib(n).to_le_bytes(); + f1.write(&bytes); +} + diff --git a/math/int2str/Cargo.toml b/math/int2str/Cargo.toml new file mode 100644 index 0000000..cdac039 --- /dev/null +++ b/math/int2str/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "int2str" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +nested = { path = "../../nested" } diff --git a/math/int2str/src/main.rs b/math/int2str/src/main.rs new file mode 100644 index 0000000..db2b19a --- /dev/null +++ b/math/int2str/src/main.rs @@ -0,0 +1,38 @@ + +use std::{ + fs::File, + io::{Read, Write}, + os::unix::io::FromRawFd +}; + +fn main() { + nested::magic_header(); + eprintln!(" Human-readably Print MachineInt"); + nested::magic_header(); + + let mut f0 = unsafe { File::from_raw_fd(0) }; + eprintln!(" +> 0: + ( ℕ ) + ( MachineInt ) + ( MachineWord ) + ( Array 8 MachineSlab ) + ( Pipe Shot (Array 8 MachineSlab) ) +"); + + eprintln!(" +< 1: + ( ℕ ) + ( Sequence (Digit 10) ) + ( Sequence ASCII ) + ( Sequence MachineSlab ) + ( Pipe Shot (Sequence MachineSlab) ) +"); + + nested::magic_header(); + + let mut bytes = [0 as u8; 8]; + f0.read_exact(&mut bytes); + println!("{}", u64::from_le_bytes(bytes)); +} + diff --git a/math/str2int/Cargo.toml b/math/str2int/Cargo.toml new file mode 100644 index 0000000..b85bed6 --- /dev/null +++ b/math/str2int/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "str2int" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +nested = { path = "../../nested" } diff --git a/math/str2int/src/main.rs b/math/str2int/src/main.rs new file mode 100644 index 0000000..596004c --- /dev/null +++ b/math/str2int/src/main.rs @@ -0,0 +1,40 @@ + +use std::{ + fs::File, + io::{Read, Write}, + os::unix::io::FromRawFd +}; + +fn main() { + nested::magic_header(); + eprintln!(" Parse MachineInt from String"); + nested::magic_header(); + + let mut f0 = unsafe { File::from_raw_fd(0) }; + eprintln!(" +> 0: + ( ℕ ) + ( Sequence (Digit 10) ) + ( Sequence ASCII ) + ( Sequence MachineSlab ) + ( Pipe Shot (Sequence MachineSlab) ) +"); + + let mut f1 = unsafe { File::from_raw_fd(1) }; + eprintln!(" +< 1: + ( ℕ ) + ( MachineInt ) + ( MachineWord ) + ( Array 8 MachineSlab ) + ( Pipe Shot (Array 8 MachineSlab) ) +"); + + nested::magic_header(); + + let mut chars = Vec::new(); + f0.read_to_end(&mut chars); + chars.retain(|c| (*c as char).is_numeric()); + f1.write(&u64::from_str_radix(&String::from_utf8_lossy(&chars), 10).unwrap().to_le_bytes()); +} +