BASIC User Group > Code Challenges

String Compression and Decompression

<< < (2/2)

jalih:
Here is 8th version...

Zipping and unzipping file:


--- Code: ---: app:main
  "warpeace.txt" f:size nip "Source file size is: %d bytes\n" s:strfmt .
  f:zipnew "warpeace.txt" "warpeace.txt" f:zip+
  "warpeace.zip" f:zipsave

  "warpeace.zip" f:size nip "Compressed file size is: %d bytes\n" s:strfmt .

  "warpeace.zip" f:zipopen getcwd true f:unzip drop
  "warpeace.txt" f:size nip "Extracted file size is: %d bytes\n" s:strfmt .
  bye ;

--- End code ---

Read text file into buffer, zip it and then unzip data back to buffer:


--- Code: ---: app:main
  f:zipnew "warpeace.txt" f:slurp
  b:len "Source buffer size is: %d bytes\n" s:strfmt .
  "warpeace.txt" swap f:zip+
  "warpeace.zip" f:zipsave

  "warpeace.zip" f:size nip "Compressed file size is: %d bytes\n" s:strfmt .

  "warpeace.zip" f:zipopen
  "warpeace.txt" f:zip@ nip b:len nip "Extracted buffer size was: %d bytes\n" s:strfmt .
  bye ;

--- End code ---

John:
Your avatar says Mac Daddy but I'm seeing GO Daddy.  :)

AIR:

--- Quote from: John on March 07, 2019, 06:38:20 pm ---Your avatar says Mac Daddy but I'm seeing GO Daddy.  :)

--- End quote ---

I'm coding GO on my MAC.... ;)

AIR:
Jalih, thanks for your example, that's pretty cool!

So 8th automatically pipes the output of a command or file to the next command, right?

At first glance it's kind of cryptic, but when I thought about how you can pipe the output of a command to another command in shell scripting or Powershell, your code made perfect sense.

What does the trailing DOT mean, though?  I do like how concise/compact 8th code is, btw!

Without checking the docs, am I correct in thinking that f: is for file related functions and s: is for strings?

AIR.

jalih:

--- Quote from: AIR on March 07, 2019, 09:16:05 pm ---So 8th automatically pipes the output of a command or file to the next command, right?

At first glance it's kind of cryptic, but when I thought about how you can pipe the output of a command to another command in shell scripting or Powershell, your code made perfect sense.

What does the trailing DOT mean, though?  I do like how concise/compact 8th code is, btw!

--- End quote ---

Yes, 8th is a stack based language. You use stack to pass data around. Global variables, word-local and task-local variables are supported but most of the power comes from factoring code into small words, composing them and using stack efficiently.

Dot is a Forth style print word, on my example it just prints the string.


--- Quote from: AIR on March 07, 2019, 09:16:05 pm ---Without checking the docs, am I correct in thinking that f: is for file related functions and s: is for strings?

--- End quote ---

You are right, those are namespaces and help to write more modular code.

Navigation

[0] Message Index

[*] Previous page

Go to full version