Determine if a string is collapsible: Difference between revisions

Add Factor
(→‎{{header|zkl}}: added code)
(Add Factor)
Line 87:
 
__TOC__
 
=={{header|Factor}}==
<lang factor>USING: formatting io kernel sbufs sequences strings ;
IN: rosetta-code.string-collapse
 
: (collapse) ( str -- str )
unclip-slice 1string >sbuf
[ over last over = [ drop ] [ suffix! ] if ] reduce >string ;
 
: collapse ( str -- str ) [ "" ] [ (collapse) ] if-empty ;
 
: .str ( str -- ) dup length "«««%s»»» (length %d)\n" printf ;
 
: show-collapse ( str -- )
[ "Before collapse: " write .str ]
[ "After collapse: " write collapse .str ] bi nl ;
 
: collapse-demo ( -- )
{
""
"\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln "
"..1111111111111111111111111111111111111111111111111111111111111117777888"
"I never give 'em hell, I just tell the truth, and they think it's hell. "
" --- Harry S Truman "
"The better the 4-wheel drive, the further you'll be from help when ya get stuck!"
"headmistressship"
"aardvark"
"😍😀🙌💃😍😍😍🙌"
} [ show-collapse ] each ;
 
MAIN: collapse-demo</lang>
{{out}}
<pre>
Before collapse: «««»»» (length 0)
After collapse: «««»»» (length 0)
 
Before collapse: «««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»» (length 72)
After collapse: «««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»» (length 70)
 
Before collapse: «««..1111111111111111111111111111111111111111111111111111111111111117777888»»» (length 72)
After collapse: «««.178»»» (length 4)
 
Before collapse: «««I never give 'em hell, I just tell the truth, and they think it's hell. »»» (length 72)
After collapse: «««I never give 'em hel, I just tel the truth, and they think it's hel. »»» (length 69)
 
Before collapse: ««« --- Harry S Truman »»» (length 72)
After collapse: ««« - Hary S Truman »»» (length 17)
 
Before collapse: «««The better the 4-wheel drive, the further you'll be from help when ya get stuck!»»» (length 80)
After collapse: «««The beter the 4-whel drive, the further you'l be from help when ya get stuck!»»» (length 77)
 
Before collapse: «««headmistressship»»» (length 16)
After collapse: «««headmistreship»»» (length 14)
 
Before collapse: «««aardvark»»» (length 8)
After collapse: «««ardvark»»» (length 7)
 
Before collapse: «««😍😀🙌💃😍😍😍🙌»»» (length 8)
After collapse: «««😍😀🙌💃😍🙌»»» (length 6)
</pre>
 
=={{header|Go}}==
1,827

edits