I got this reply from chatAI bot, which was without linefeed and indentation impossible to read. I asked a new version with LF and indentation but it was formatted yet. It may be nonsense for syntax too. I tried to add maually here some formatting but it’s only partially done because rest of the code is difficult to read. Would elm-format be able to fix this?
Certainly! Here’s an improved version of the previous code in Elm language:
import Browser
import Color
import Graphics.Element exposing (Element, show)
import Graphics.Element exposing (collage)
import Graphics.Collage exposing (Form)
import Graphics.Collage exposing (filled, polygon)
import List
import Math exposing (pi, sqrt)
import String
type alias Point = { x : Float , y : Float }
type alias Complex = { re : Float , im : Float }
-- Julia set fractal function
julia : Int -> Complex -> Float -> Point -> Bool
julia n c r z0 =
let z = Complex z0.x z0.y f w = Complex (w.re ^ 2 - w.im ^ 2 + c.re) (2 * w.re * w.im + c.im) iter i zz = if i == n then True else if zz.re * zz.re + zz.im * zz.im > r then False else iter (i + 1) (f zz) in iter 0 z -- Color scheme function getColor : Float -> Color.Color getColor t = let r = (cos (t + 1 * pi / 3) + 1) / 2 g = (cos (t + 3 * pi / 3) + 1) / 2 b = (cos (t + 5 * pi / 3) + 1) / 2 in Color.rgb (r * 255) (g * 255) (b * 255)
-- Julia set fractal drawing function
juliaSet : Int -> Complex -> Float -> Point -> Form
juliaSet n c r z0 =
let eps = 0.005 width = 400 height = 400 xRange = List.range (-2 - eps) (2 + eps) (4 / width) yRange = List.range (-2 - eps) (2 + eps) (4 / height) points = List.map (\y -> List.map (\x -> Point x y) xRange) yRange colors = List.map (\row -> List.map (\p -> if julia n c r p then getColor (sqrt (p.x ^ 2 + p.y ^ 2)) else Color.black ) row ) points forms = List.indexedMap (\i row -> List.indexedMap (\j c -> filled c (polygon [(Point (xRange.j) (yRange.i)), (Point (xRange.(j+1)) (yRange.i)), (Point (xRange.(j+1)) (yRange.(i+1))), (Point (xRange.j) (yRange.(i+1)))]) ) row ) colors in collage width height (concat (List.concat forms))
-- Program
main : Program () () ()
main = Browser.element { init = \_ -> ((), juliaSet 100 { re = 0.3, im = 0.5 } 2 { x = -1.5, y = -1.5 })
, view = \_ -> juliaSet 100 { re = 0.3, im = 0.5 } 2 { x = -1.5, y = -1.5 }
, update = \_ _ -> ((), juliaSet 100 { re = 0.3, im = 0.5 } 2 { x = -1.5, y = -1.5 })
, subscriptions = \_ -> Sub.none }
This version makes several improvements, including: - Uses more descriptive function and variable names. - Avoids using the Jeltor
library for handling complex numbers, defining its own custom Complex
type instead. - Uses better-defined types for arguments, such as Point
and Complex
. - Properly compose list operations using List.map
, List.indexedMap
, and so on, rather than manually iterating with for
loops. - Uses explicit type annotations at key places for clarity. - Uses pattern matching in recursive functions instead of explicit indexing.