Skip to content

Instantly share code, notes, and snippets.

@pete-murphy
Last active September 11, 2022 17:14
Show Gist options
  • Save pete-murphy/dd9d198c0ab9c0ab5445d0f9f6157f20 to your computer and use it in GitHub Desktop.
Save pete-murphy/dd9d198c0ab9c0ab5445d0f9f6157f20 to your computer and use it in GitHub Desktop.
ST push
module Main where
import Prelude
import Control.Monad.ST as ST
import Data.Array as Array
import Data.Array.ST as Array.ST
import Data.Foldable as Foldable
import Effect (Effect)
import Effect.Console as Console
import TryPureScript (render, withConsole)
type Project = { name :: String }
newtype LessCompletionPercentage a = LessCompletionPercentage a
derive newtype instance Show a => Show (LessCompletionPercentage a)
plan :: { projects :: Array Project }
plan =
{ projects:
[ { name: "a" }
, { name: "b" }
, { name: "c" }
]
}
defaultInteraction ∷ Array (LessCompletionPercentage String)
defaultInteraction = plan.projects
# Array.foldl
( \acc project -> do
Array.snoc acc (LessCompletionPercentage project.name)
)
[]
defaultInteraction' ∷ Array (LessCompletionPercentage String)
defaultInteraction' =
ST.run do
xs <- Array.ST.new
plan.projects
# Array.foldM
( \acc project ->
do
_ <- Array.ST.push (LessCompletionPercentage project.name) acc
pure acc
)
xs
>>= Array.ST.freeze
defaultInteraction'' ∷ Array (LessCompletionPercentage String)
defaultInteraction'' =
ST.run do
xs <- Array.ST.new
plan.projects
# Foldable.traverse_ \project ->
Array.ST.push (LessCompletionPercentage project.name) xs
Array.ST.freeze xs
defaultInteraction''' ∷ Array (LessCompletionPercentage String)
defaultInteraction''' =
plan.projects
<#> \project -> (LessCompletionPercentage project.name)
main :: Effect Unit
main = render =<< withConsole do
Console.logShow defaultInteraction
Console.log "\n----------\n"
Console.logShow defaultInteraction'
Console.log "\n----------\n"
Console.logShow defaultInteraction''
Console.log "\n----------\n"
Console.logShow defaultInteraction'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment