Skip to content

Commit

Permalink
Add S35_Reformat by reusing S33_DISASSEMBLE and S34_ASSEMBLE
Browse files Browse the repository at this point in the history
  • Loading branch information
NHDaly committed Jan 27, 2019
1 parent 4e49b60 commit 8cbc141
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/CspExamples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 8 additions & 1 deletion test/CspExamples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 8cbc141

Please sign in to comment.