String Formatting in PureScript

module Main where

import Prelude

import Data.Int (binary, hexadecimal)
import Data.Number.Format (toStringWith, fixed)
import Effect (Effect)
import Effect.Console (log)

type Point = { x :: Int, y :: Int }

main :: Effect Unit
main = do
  let p = { x: 1, y: 2 }

  -- PureScript doesn't have a direct equivalent to Go's fmt.Printf,
  -- so we'll use string interpolation and custom formatting functions

  log $ "struct1: " <> show p
  log $ "struct2: " <> "{ x: " <> show p.x <> ", y: " <> show p.y <> " }"
  log $ "struct3: " <> "{ x: " <> show p.x <> ", y: " <> show p.y <> " }"
  log $ "type: Point"

  log $ "bool: " <> show true
  log $ "int: " <> show 123
  log $ "bin: " <> binary 14
  log $ "char: " <> show '!'
  log $ "hex: " <> hexadecimal 456

  log $ "float1: " <> toStringWith (fixed 6) 78.9
  log $ "float2: " <> show 123400000.0
  log $ "float3: " <> show 123400000.0

  log $ "str1: \"string\""
  log $ "str2: \"\\\"string\\\"\""
  log $ "str3: " <> hexadecimal (charCodeAt 0 'h') <> hexadecimal (charCodeAt 0 'e') <> hexadecimal (charCodeAt 0 'x')

  -- PureScript doesn't have a direct equivalent to Go's pointer printing

  -- Width formatting in PureScript requires custom functions
  log $ "width1: |" <> padLeft 6 ' ' (show 12) <> "|" <> padLeft 6 ' ' (show 345) <> "|"
  log $ "width2: |" <> padLeft 6 ' ' (toStringWith (fixed 2) 1.2) <> "|" <> padLeft 6 ' ' (toStringWith (fixed 2) 3.45) <> "|"
  log $ "width3: |" <> padRight 6 ' ' (toStringWith (fixed 2) 1.2) <> "|" <> padRight 6 ' ' (toStringWith (fixed 2) 3.45) <> "|"
  log $ "width4: |" <> padLeft 6 ' ' "foo" <> "|" <> padLeft 6 ' ' "b" <> "|"
  log $ "width5: |" <> padRight 6 ' ' "foo" <> "|" <> padRight 6 ' ' "b" <> "|"

  let s = "sprintf: a " <> "string"
  log s

  -- PureScript doesn't have a direct equivalent to Go's fmt.Fprintf
  log "io: an error"

-- Helper functions for padding
padLeft :: Int -> Char -> String -> String
padLeft n c s = let padding = fromCharArray $ replicate (n - length s) c
                in padding <> s

padRight :: Int -> Char -> String -> String
padRight n c s = let padding = fromCharArray $ replicate (n - length s) c
                 in s <> padding

-- Helper function to get char code
charCodeAt :: Int -> Char -> Int
charCodeAt _ c = toCharCode c

This PureScript code demonstrates various string formatting techniques, attempting to replicate the functionality of the original Go example as closely as possible. Here are some key points:

  1. PureScript doesn’t have a direct equivalent to Go’s fmt.Printf, so we use string interpolation and custom formatting functions.

  2. For numeric formatting, we use functions from the Data.Int and Data.Number.Format modules.

  3. PureScript doesn’t have built-in padding functions for strings, so we’ve implemented custom padLeft and padRight functions.

  4. Some Go-specific concepts (like pointer printing) don’t have direct equivalents in PureScript and are omitted or simplified.

  5. For hexadecimal representation of strings, we convert each character to its character code and then to hexadecimal.

  6. PureScript uses log from Effect.Console for printing to the console.

To run this program, you would typically compile it with the PureScript compiler and then run it with Node.js:

$ pulp run

This assumes you have the PureScript toolchain and necessary dependencies set up in your project.