Embed Directive in Idris

Here’s the translation of the Go code to Idris, formatted in Markdown suitable for Hugo:

import System.File.ReadWrite

-- In Idris, we don't have direct equivalents to Go's embed directive.
-- Instead, we'll use file I/O operations to read file contents.

-- Define a function to read file contents
readFileContents : String -> IO String
readFileContents filename = do
  Right contents <- readFile filename
    | Left err => pure "Error reading file"
  pure contents

main : IO ()
main = do
  -- Read and print the contents of 'single_file.txt'
  fileString <- readFileContents "folder/single_file.txt"
  putStrLn fileString
  
  -- Read and print the contents of 'file1.hash'
  content1 <- readFileContents "folder/file1.hash"
  putStrLn content1
  
  -- Read and print the contents of 'file2.hash'
  content2 <- readFileContents "folder/file2.hash"
  putStrLn content2

In Idris, we don’t have a direct equivalent to the embed directive used in the original example. Instead, we use file I/O operations to read the contents of files at runtime.

Here’s an explanation of the code:

  1. We import the System.File.ReadWrite module, which provides functions for file operations.

  2. We define a helper function readFileContents that takes a filename as input and returns the contents of the file as a string wrapped in an IO action.

  3. In the main function:

    • We use readFileContents to read the contents of “folder/single_file.txt” and print it.
    • We read and print the contents of “folder/file1.hash” and “folder/file2.hash”.

To run this example, you would need to create the necessary files and folders:

$ mkdir -p folder
$ echo "hello idris" > folder/single_file.txt
$ echo "123" > folder/file1.hash
$ echo "456" > folder/file2.hash

Then compile and run the Idris program:

$ idris -o embed-example embed-example.idr
$ ./embed-example
hello idris
123
456

Note that this approach reads the files at runtime, whereas the original example embedded the files at compile time. Idris doesn’t have a built-in feature for compile-time file embedding, so runtime file reading is used as an alternative.