From 8cbc141a94ad94d9d85044362644e93cf1c4f6a7 Mon Sep 17 00:00:00 2001 From: Nathan Daly Date: Sat, 26 Jan 2019 19:21:24 -0500 Subject: [PATCH] Add S35_Reformat by reusing S33_DISASSEMBLE and S34_ASSEMBLE --- src/CspExamples.jl | 17 +++++++++++++++++ test/CspExamples.jl | 9 ++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/CspExamples.jl b/src/CspExamples.jl index 46b8c50..54ffae3 100644 --- a/src/CspExamples.jl +++ b/src/CspExamples.jl @@ -127,4 +127,21 @@ function S34_ASSEMBLE(X::Channel{Char}, lineprinter::Channel{>:String}, lineleng end end +""" + S35_Reformat + +3.5 Reformaeast "Problem: Read a sequence of cards of 80 characters each, and print +the characters on a lineprinter at 125 characters per line. Every card +should be followed by an extra space, and the last line should be +completed with spaces if necessary." + +This one's fun! We can reuse the existing functions by creating an intermediate Channel and +Task (equivalent to a Process in Hoare's paper) to act as the output and then input. +""" +function S35_Reformat(west::Channel{String}, east::Channel{>:String}, linelength=125) + S34_ASSEMBLE(Channel(ctype=Char) do ch + S33_DISASSEMBLE(west, ch) + end, east, linelength) +end + end diff --git a/test/CspExamples.jl b/test/CspExamples.jl index 87671f4..c69c0e4 100644 --- a/test/CspExamples.jl +++ b/test/CspExamples.jl @@ -40,7 +40,7 @@ end @testset "S32_SQUASH_EXT" begin # Test 1 * at end west = make_filled_channel("hello**world*") - east = Channel(ch->CspExamples.S32_SQUASH_EXT(west, ch), ctype=Char) + east = Channel(ch->CspExamples.S32_SQUASH_EXT(west, ch),ctype=Char) @test String(collect(east)) == "hello↑world*" # Test 3 *s at end @@ -74,3 +74,10 @@ end expected = [String(expected[1+(i-1)*linelength:i*linelength]) for i in 1:2] @test collect(lineprinter) == expected end + +@testset "S35_Reformat" begin + reformatted = Channel() do ch + CspExamples.S35_Reformat(make_filled_channel(["hello", "world"]), ch, 4) + end + @test collect(reformatted) == ["hell", "o wo", "rld "] +end