{-# LANGUAGE BlockArguments      #-}
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications    #-}
{-# LANGUAGE ViewPatterns        #-}

module Dovetail.Core.Control.Extend where

import Data.Vector (Vector)
import Data.Vector qualified as Vector
import Dovetail
import Dovetail.Evaluate (builtIn)

env :: forall ctx. Env ctx
env :: Env ctx
env = do
  let _ModuleName :: ModuleName
_ModuleName = Text -> ModuleName
ModuleName Text
"Control.Extend"

  -- arrayExtend :: forall a b. (Array a -> b) -> Array a -> Array b
  ModuleName
-> Text
-> ((Vector (Value ctx) -> Eval ctx (Value ctx))
    -> Vector (Value ctx) -> Eval ctx (Vector (Value ctx)))
-> Env ctx
forall ctx a. ToValue ctx a => ModuleName -> Text -> a -> Env ctx
builtIn @ctx @((Vector (Value ctx) -> Eval ctx (Value ctx)) -> Vector (Value ctx) -> Eval ctx (Vector (Value ctx)))
    ModuleName
_ModuleName Text
"arrayExtend"
    \Vector (Value ctx) -> Eval ctx (Value ctx)
f Vector (Value ctx)
xs ->
      Int
-> (Int -> Eval ctx (Value ctx)) -> Eval ctx (Vector (Value ctx))
forall (m :: * -> *) a.
Monad m =>
Int -> (Int -> m a) -> m (Vector a)
Vector.generateM (Vector (Value ctx) -> Int
forall a. Vector a -> Int
Vector.length Vector (Value ctx)
xs) \Int
i ->
        Vector (Value ctx) -> Eval ctx (Value ctx)
f (Int -> Vector (Value ctx) -> Vector (Value ctx)
forall a. Int -> Vector a -> Vector a
Vector.drop Int
i Vector (Value ctx)
xs)