Skip to content

Commit a9003ba

Browse files
authored
Merge pull request #10 from cratelyn/add-increment
here's a small monadic verb, whose implementation will be described in the readme. ``` ; git show --oneline --quiet 62edb1f (HEAD -> add-increment, origin/add-increment) 🤠 `>:` increment operator ; cargo test --quiet running 92 tests ........................................................................................ 88/92 .... test result: ok. 92 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s running 63 tests ........................ii..................................... test result: ok. 61 passed; 0 failed; 2 ignored; 0 measured; 0 filtered out; finished in 0.01s running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s ``` ``` ; cargo run >: 1 2 >: 1 2 3 2 3 4 >: i. 3 3 1 2 3 4 5 6 7 8 9 ```
2 parents 545ca84 + 62edb1f commit a9003ba

File tree

4 files changed

+10
-5
lines changed

4 files changed

+10
-5
lines changed

src/a.rs

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ use super::*; use std::marker::PhantomData as PD;
138138
pub fn m_tally(self)->R<A>{let A{m,n,..}=self;let(i)=I::try_from(m*n)?;A::from_i(i)}
139139
pub fn m_trans(self)->R<A>{let(i@A{m:m_i,n:n_i,..})=self;let(m_o,n_o)=(n_i,m_i);
140140
let(f)=|i_o,j_o|{i.get(j_o,i_o)};A::new(m_o,n_o)?.init_with(f)}
141+
pub fn m_inc(self)->R<A>{let(a@A{m,n,..})=self;A::new(m,n)?.init_with(|i,j|a.get(i,j).map(|x|x+1))}
141142
}
142143

143144
/**dyadic verbs*/impl D{

src/j.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ fn eval_(ast:B<N>,st:&mut ST)->R<O<A>>{use{M::*,D::*};
99
Ok(None)=>Err(err!("expression did not result in a value"))}};
1010
match *ast{
1111
N::A{a} =>Ok(a),
12-
N::M{m,o} =>{let(a)=rec(o)?; match m{Idot=>a.m_idot(), Shape=>a.m_shape(), Same=>a.m_same(),
13-
Tally=>a.m_tally(),Transpose=>a.m_trans()}}
12+
N::M{m,o} =>{let(a)=rec(o)?; match m{Idot=>a.m_idot(), Shape=>a.m_shape(), Same=>a.m_same(),
13+
Tally=>a.m_tally(),Transpose=>a.m_trans(), Inc=>a.m_inc()}}
1414
N::D{d,l,r}=>{let(l,r)=(rec(l)?,rec(r)?);match d{Plus=>l.d_plus(r), Mul=>l.d_mul(r),
1515
Left=>l.d_left(r), Right=>l.d_right(r)}}
1616
N::Ym{ym,d,o}=>{rec(o).and_then(|a|ym.apply(d,a))}

src/r.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ mod lex{use crate::*;
3232
t!(lex_assign, "a =: 1", v![sy!("a"), T::E, TA(v![1])] );
3333
}
3434
}/**input parsing*/mod parse{use {crate::*,super::lex::{T,lex}};
35-
/**dyadic verb */ #[derive(DBG,PE,PO)] pub enum D {Plus,Mul, Left, Right }
36-
/**monadic verb */ #[derive(DBG,PE,PO)] pub enum M {Idot,Shape,Tally,Transpose,Same}
35+
/**dyadic verb */ #[derive(DBG,PE,PO)] pub enum D {Plus,Mul, Left, Right }
36+
/**monadic verb */ #[derive(DBG,PE,PO)] pub enum M {Idot,Shape,Tally,Transpose,Same,Inc}
3737
/**dyadic adverb */ #[derive(DBG )] pub enum Yd{/**dyadic `/` */ Table ,
3838
/**dyadic `\` */ Infix }
3939
/**monadic adverb */ #[derive(DBG )] pub enum Ym{/**monadic `/`*/ Insert,
@@ -104,7 +104,7 @@ mod lex{use crate::*;
104104

105105
impl M {fn new(s:&str)->O<M> {use M::*; Some(match s{"i."=>Idot ,"$" =>Shape ,"|:"=>Transpose ,
106106
"#" =>Tally ,"[" =>Same ,"]" =>Same ,
107-
_=>r!(None)})}}
107+
">:"=>Inc, _=>r!(None)})}}
108108
impl D {fn new(s:&str)->O<D> {use D::*; Some(match s{"+" =>Plus ,"*" =>Mul ,"[" =>Left ,
109109
"]" =>Right , _=>r!(None)})}}
110110
impl Ym{fn new(s:&str)->O<Ym>{use Ym::*;Some(match s{"/" =>Insert,"\\"=>Prefix, _=>r!(None)})}}

tests/t.rs

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
#[test]fn add_slice_to_rotated_slice()->R<()>{
1616
let(a@A{m:4,n:1,..})=eval_s("1 2 3 4 + i. 4 1")? else{bail!("bad dims")};eq!(a.into_matrix()?,&[&[1],&[3],&[5],&[7]]);ok!()}
1717
#[test]fn other_add_slice_to_rotated_slice_is_length_error()->R<()>{is!(eval_s("i. 4 1 + 1 2 3 4").is_err());ok!()}
18+
} #[cfg(test)]mod increment{use super::*;
19+
#[test]fn increment_scalar()->R<()>{let(i)=eval_s(">: 1")?; eq!(i.as_i()?, 2); ok!()}
20+
#[test]fn increment_slice ()->R<()>{let(i)=eval_s(">: 1 2 3")?; eq!(i.as_slice()?, &[2,3,4]); ok!()}
21+
#[test]fn increment_matrix()->R<()>{let(i)=eval_s(">: i. 2 2")?;eq!(i.into_matrix()?,&[&[1,2],&[3,4]]);ok!()}
1822
} #[cfg(test)]mod tally{use super::*;
1923
macro_rules! t{($f:ident,$i:literal,$o:literal)=>{
2024
#[test]fn $f()->R<()>{let(a@A{m:1,n:1,..})=eval_s($i)? else{bail!("bad dims")};eq!(a.as_slice()?,&[$o]);ok!()}}}

0 commit comments

Comments
 (0)