Skip to main content
Tweeted twitter.com/StackCodeReview/status/1542296942994128896
added 110 characters in body
Source Link

I've recently tried to write my functions by using composition. But this one, renderCross' is especially difficult to convert fully. How would I write this composition point free? It's the n that I'm trying to make disappear. I've tried using <*>, and pointfree.io uses liftA2 (but this results in very ugly code) so I would believe I'm at least somewhat close.

Maybe I could use the Reader monad, but would that be too complicated?

renderCross :: Int -> String
renderCross = spread . unlines . flip renderCross' [""]
  where
    renderCross' :: Int -> [String] -> [String]
    renderCross' n = edge n . vert n . horiz n . mid n . horiz n . vert n . edge n

edge :: Int -> [String] -> [String]
edge i s = s ++ [space i ++ "ooo"]

vert :: Int -> [String] -> [String]
vert i s = s ++ replicate (i - 2) (space i ++ "o|o")

horiz :: Int -> [String] -> [String]
horiz i s = s ++ [horiz' ++ "|" ++ horiz']
  where
    horiz' = replicate i 'o'

mid :: Int -> [String] -> [String]
mid i s = s ++ [intercalate (replicate (i - 1) '-') ["o", "o", "o"]]

space :: Int -> String
space n = replicate (n - 1) ' '

EDIT:

-- spread out word, i.e. add a space between each character
spread = intersperse ' '

I've recently tried to write my functions by using composition. But this one, renderCross' is especially difficult to convert fully. How would I write this composition point free? It's the n that I'm trying to make disappear. I've tried using <*>, and pointfree.io uses liftA2 (but this results in very ugly code) so I would believe I'm at least somewhat close.

Maybe I could use the Reader monad, but would that be too complicated?

renderCross :: Int -> String
renderCross = spread . unlines . flip renderCross' [""]
  where
    renderCross' :: Int -> [String] -> [String]
    renderCross' n = edge n . vert n . horiz n . mid n . horiz n . vert n . edge n

edge :: Int -> [String] -> [String]
edge i s = s ++ [space i ++ "ooo"]

vert :: Int -> [String] -> [String]
vert i s = s ++ replicate (i - 2) (space i ++ "o|o")

horiz :: Int -> [String] -> [String]
horiz i s = s ++ [horiz' ++ "|" ++ horiz']
  where
    horiz' = replicate i 'o'

mid :: Int -> [String] -> [String]
mid i s = s ++ [intercalate (replicate (i - 1) '-') ["o", "o", "o"]]

space :: Int -> String
space n = replicate (n - 1) ' '

I've recently tried to write my functions by using composition. But this one, renderCross' is especially difficult to convert fully. How would I write this composition point free? It's the n that I'm trying to make disappear. I've tried using <*>, and pointfree.io uses liftA2 (but this results in very ugly code) so I would believe I'm at least somewhat close.

Maybe I could use the Reader monad, but would that be too complicated?

renderCross :: Int -> String
renderCross = spread . unlines . flip renderCross' [""]
  where
    renderCross' :: Int -> [String] -> [String]
    renderCross' n = edge n . vert n . horiz n . mid n . horiz n . vert n . edge n

edge :: Int -> [String] -> [String]
edge i s = s ++ [space i ++ "ooo"]

vert :: Int -> [String] -> [String]
vert i s = s ++ replicate (i - 2) (space i ++ "o|o")

horiz :: Int -> [String] -> [String]
horiz i s = s ++ [horiz' ++ "|" ++ horiz']
  where
    horiz' = replicate i 'o'

mid :: Int -> [String] -> [String]
mid i s = s ++ [intercalate (replicate (i - 1) '-') ["o", "o", "o"]]

space :: Int -> String
space n = replicate (n - 1) ' '

EDIT:

-- spread out word, i.e. add a space between each character
spread = intersperse ' '
added 48 characters in body; edited tags; edited title
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

How would I write this composition point free? Rendering a cross in ASCII art

I've recently tried to write my functions by using composition. But this one, renderCross' is especially difficult to convert fully. It'sHow would I write this composition point free? It's the n that I'm trying to make disappear. I've tried using <*>, and pointfree.io uses liftA2 (but this results in very ugly code) so I would believe I'm at least somewhat close.

Maybe I could use the Reader monad, but would that be too complicated?

renderCross :: Int -> String
renderCross = spread . unlines . flip renderCross' [""]
  where
    renderCross' :: Int -> [String] -> [String]
    renderCross' n = edge n . vert n . horiz n . mid n . horiz n . vert n . edge n

edge :: Int -> [String] -> [String]
edge i s = s ++ [space i ++ "ooo"]

vert :: Int -> [String] -> [String]
vert i s = s ++ replicate (i - 2) (space i ++ "o|o")

horiz :: Int -> [String] -> [String]
horiz i s = s ++ [horiz' ++ "|" ++ horiz']
  where
    horiz' = replicate i 'o'

mid :: Int -> [String] -> [String]
mid i s = s ++ [intercalate (replicate (i - 1) '-') ["o", "o", "o"]]

space :: Int -> String
space n = replicate (n - 1) ' '

How would I write this composition point free?

I've recently tried to write my functions by using composition. But this one, renderCross' is especially difficult to convert fully. It's the n that I'm trying to make disappear. I've tried using <*>, and pointfree.io uses liftA2 (but this results in very ugly code) so I would believe I'm at least somewhat close.

Maybe I could use the Reader monad, but would that be too complicated?

renderCross :: Int -> String
renderCross = spread . unlines . flip renderCross' [""]
  where
    renderCross' :: Int -> [String] -> [String]
    renderCross' n = edge n . vert n . horiz n . mid n . horiz n . vert n . edge n

edge :: Int -> [String] -> [String]
edge i s = s ++ [space i ++ "ooo"]

vert :: Int -> [String] -> [String]
vert i s = s ++ replicate (i - 2) (space i ++ "o|o")

horiz :: Int -> [String] -> [String]
horiz i s = s ++ [horiz' ++ "|" ++ horiz']
  where
    horiz' = replicate i 'o'

mid :: Int -> [String] -> [String]
mid i s = s ++ [intercalate (replicate (i - 1) '-') ["o", "o", "o"]]

space :: Int -> String
space n = replicate (n - 1) ' '

Rendering a cross in ASCII art

I've recently tried to write my functions by using composition. But this one, renderCross' is especially difficult to convert fully. How would I write this composition point free? It's the n that I'm trying to make disappear. I've tried using <*>, and pointfree.io uses liftA2 (but this results in very ugly code) so I would believe I'm at least somewhat close.

Maybe I could use the Reader monad, but would that be too complicated?

renderCross :: Int -> String
renderCross = spread . unlines . flip renderCross' [""]
  where
    renderCross' :: Int -> [String] -> [String]
    renderCross' n = edge n . vert n . horiz n . mid n . horiz n . vert n . edge n

edge :: Int -> [String] -> [String]
edge i s = s ++ [space i ++ "ooo"]

vert :: Int -> [String] -> [String]
vert i s = s ++ replicate (i - 2) (space i ++ "o|o")

horiz :: Int -> [String] -> [String]
horiz i s = s ++ [horiz' ++ "|" ++ horiz']
  where
    horiz' = replicate i 'o'

mid :: Int -> [String] -> [String]
mid i s = s ++ [intercalate (replicate (i - 1) '-') ["o", "o", "o"]]

space :: Int -> String
space n = replicate (n - 1) ' '
Source Link

How would I write this composition point free?

I've recently tried to write my functions by using composition. But this one, renderCross' is especially difficult to convert fully. It's the n that I'm trying to make disappear. I've tried using <*>, and pointfree.io uses liftA2 (but this results in very ugly code) so I would believe I'm at least somewhat close.

Maybe I could use the Reader monad, but would that be too complicated?

renderCross :: Int -> String
renderCross = spread . unlines . flip renderCross' [""]
  where
    renderCross' :: Int -> [String] -> [String]
    renderCross' n = edge n . vert n . horiz n . mid n . horiz n . vert n . edge n

edge :: Int -> [String] -> [String]
edge i s = s ++ [space i ++ "ooo"]

vert :: Int -> [String] -> [String]
vert i s = s ++ replicate (i - 2) (space i ++ "o|o")

horiz :: Int -> [String] -> [String]
horiz i s = s ++ [horiz' ++ "|" ++ horiz']
  where
    horiz' = replicate i 'o'

mid :: Int -> [String] -> [String]
mid i s = s ++ [intercalate (replicate (i - 1) '-') ["o", "o", "o"]]

space :: Int -> String
space n = replicate (n - 1) ' '