Skip to main content
Commonmark migration
Source Link

Forth (gforth), 101 bytes

: f >r dup i mod tuck - dup 3 pick + rot type i - -1 max r> -1 rot ?do over i + over type dup -loop ;

Try it online!

Definitely not the most golfed it could be, but this one requires more stack manipulation than usual, so it's a bit more complicated to golf.

Explanation

  • Get string-length % chunk-size
  • Print string-length % chunk-size characters starting from addr + (string-length - (string-length % chunk-size)) [effectively, we're printing the last chunk separate, since it's size might be smaller than the chunk-size]
  • Loop backwards from the start of the last chunk to the beginning of the string, printing the appropriate chunk for each iteration of the loop

###Code Explanation

Code Explanation

: f            \ start a new word definition
  >r           \ stick chunk-size on the return stack (accessible by i)
  dup i mod    \ duplicate string-length and length%chunk-size (size of last chunk)
  tuck -       \ make copy of last-chunk-size then get last-chunk start position
  dup 3 pick + \ add last-chunk position to string address to get address of last-chunk
  rot type     \ print last-chunk
  i -          \ subtract chunk-size from last-chunk position to get second-to-last chunk position
  -1 max       \ change to -1 in case string is smaller than chunk-size
  r> -1 rot    \ move chunk-size off return stack, end loop at -1, and arrange params for loop
  ?do          \ start loop from last-chunk-pos to -1
    over i +   \ get current chunk start address
    over type  \ print current chunk
    dup        \ duplicate chunk-size
  -loop        \ subtract chunk-size from loop-index and end loop body
;              \ end word definition

Forth (gforth), 101 bytes

: f >r dup i mod tuck - dup 3 pick + rot type i - -1 max r> -1 rot ?do over i + over type dup -loop ;

Try it online!

Definitely not the most golfed it could be, but this one requires more stack manipulation than usual, so it's a bit more complicated to golf.

Explanation

  • Get string-length % chunk-size
  • Print string-length % chunk-size characters starting from addr + (string-length - (string-length % chunk-size)) [effectively, we're printing the last chunk separate, since it's size might be smaller than the chunk-size]
  • Loop backwards from the start of the last chunk to the beginning of the string, printing the appropriate chunk for each iteration of the loop

###Code Explanation

: f            \ start a new word definition
  >r           \ stick chunk-size on the return stack (accessible by i)
  dup i mod    \ duplicate string-length and length%chunk-size (size of last chunk)
  tuck -       \ make copy of last-chunk-size then get last-chunk start position
  dup 3 pick + \ add last-chunk position to string address to get address of last-chunk
  rot type     \ print last-chunk
  i -          \ subtract chunk-size from last-chunk position to get second-to-last chunk position
  -1 max       \ change to -1 in case string is smaller than chunk-size
  r> -1 rot    \ move chunk-size off return stack, end loop at -1, and arrange params for loop
  ?do          \ start loop from last-chunk-pos to -1
    over i +   \ get current chunk start address
    over type  \ print current chunk
    dup        \ duplicate chunk-size
  -loop        \ subtract chunk-size from loop-index and end loop body
;              \ end word definition

Forth (gforth), 101 bytes

: f >r dup i mod tuck - dup 3 pick + rot type i - -1 max r> -1 rot ?do over i + over type dup -loop ;

Try it online!

Definitely not the most golfed it could be, but this one requires more stack manipulation than usual, so it's a bit more complicated to golf.

Explanation

  • Get string-length % chunk-size
  • Print string-length % chunk-size characters starting from addr + (string-length - (string-length % chunk-size)) [effectively, we're printing the last chunk separate, since it's size might be smaller than the chunk-size]
  • Loop backwards from the start of the last chunk to the beginning of the string, printing the appropriate chunk for each iteration of the loop

Code Explanation

: f            \ start a new word definition
  >r           \ stick chunk-size on the return stack (accessible by i)
  dup i mod    \ duplicate string-length and length%chunk-size (size of last chunk)
  tuck -       \ make copy of last-chunk-size then get last-chunk start position
  dup 3 pick + \ add last-chunk position to string address to get address of last-chunk
  rot type     \ print last-chunk
  i -          \ subtract chunk-size from last-chunk position to get second-to-last chunk position
  -1 max       \ change to -1 in case string is smaller than chunk-size
  r> -1 rot    \ move chunk-size off return stack, end loop at -1, and arrange params for loop
  ?do          \ start loop from last-chunk-pos to -1
    over i +   \ get current chunk start address
    over type  \ print current chunk
    dup        \ duplicate chunk-size
  -loop        \ subtract chunk-size from loop-index and end loop body
;              \ end word definition
Source Link
reffu
  • 2k
  • 10
  • 10

Forth (gforth), 101 bytes

: f >r dup i mod tuck - dup 3 pick + rot type i - -1 max r> -1 rot ?do over i + over type dup -loop ;

Try it online!

Definitely not the most golfed it could be, but this one requires more stack manipulation than usual, so it's a bit more complicated to golf.

Explanation

  • Get string-length % chunk-size
  • Print string-length % chunk-size characters starting from addr + (string-length - (string-length % chunk-size)) [effectively, we're printing the last chunk separate, since it's size might be smaller than the chunk-size]
  • Loop backwards from the start of the last chunk to the beginning of the string, printing the appropriate chunk for each iteration of the loop

###Code Explanation

: f            \ start a new word definition
  >r           \ stick chunk-size on the return stack (accessible by i)
  dup i mod    \ duplicate string-length and length%chunk-size (size of last chunk)
  tuck -       \ make copy of last-chunk-size then get last-chunk start position
  dup 3 pick + \ add last-chunk position to string address to get address of last-chunk
  rot type     \ print last-chunk
  i -          \ subtract chunk-size from last-chunk position to get second-to-last chunk position
  -1 max       \ change to -1 in case string is smaller than chunk-size
  r> -1 rot    \ move chunk-size off return stack, end loop at -1, and arrange params for loop
  ?do          \ start loop from last-chunk-pos to -1
    over i +   \ get current chunk start address
    over type  \ print current chunk
    dup        \ duplicate chunk-size
  -loop        \ subtract chunk-size from loop-index and end loop body
;              \ end word definition