From c2b4a2d9b40efbe4f6cb6fd04f6cb90639f4985f Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 12 May 2019 18:44:28 +0200 Subject: Implement binary encoding Closes #39 --- dhall_syntax/src/core/text.rs | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) (limited to 'dhall_syntax/src/core/text.rs') diff --git a/dhall_syntax/src/core/text.rs b/dhall_syntax/src/core/text.rs index e79a86b..0ce1e6f 100644 --- a/dhall_syntax/src/core/text.rs +++ b/dhall_syntax/src/core/text.rs @@ -43,6 +43,10 @@ impl InterpolatedTextContents { } impl InterpolatedText { + pub fn len(&self) -> usize { + 1 + 2 * self.tail.len() + } + pub fn head(&self) -> &str { &self.head } @@ -74,17 +78,12 @@ impl InterpolatedText { pub fn iter<'a>( &'a self, - ) -> impl Iterator> + 'a - where - SubExpr: Clone, - { + ) -> impl Iterator> + 'a { use std::iter::once; use InterpolatedTextContents::{Expr, Text}; - once(Text(self.head.clone())) - .chain(self.tail.iter().flat_map(|(e, s)| { - once(Expr(SubExpr::clone(e))).chain(once(Text(s.clone()))) - })) - .filter(|c| !c.is_empty()) + let exprs = self.tail.iter().map(|(e, _)| Expr(e)); + let texts = self.tail.iter().map(|(_, s)| Text(s.clone())); + once(Text(self.head.clone())).chain(itertools::interleave(exprs, texts)) } pub fn into_iter( @@ -92,13 +91,11 @@ impl InterpolatedText { ) -> impl Iterator> { use std::iter::once; use InterpolatedTextContents::{Expr, Text}; - once(Text(self.head)) - .chain( - self.tail - .into_iter() - .flat_map(|(e, s)| once(Expr(e)).chain(once(Text(s)))), - ) - .filter(|c| !c.is_empty()) + once(Text(self.head)).chain( + self.tail + .into_iter() + .flat_map(|(e, s)| once(Expr(e)).chain(once(Text(s)))), + ) } } -- cgit v1.2.3