miso
Copyright(C) 2016-2026 David M. Johnson
LicenseBSD3-style (see the file LICENSE)
MaintainerDavid M. Johnson <code@dmj.io>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Miso.Data.Array

Description

Overview

Miso.Data.Array is a Haskell wrapper around the JavaScript Array object. Values of type Array a live in JavaScript memory; all operations run in IO and mutate the underlying JS array in place.

Use this module when you need to pass a JS-native array to a browser API or a third-party JavaScript library. For pure Haskell data processing, prefer ordinary lists or Map.

Import qualified to avoid clashing with Prelude:

import qualified Miso.Data.Array as A

Quick start

import qualified Miso.Data.Array as A

example :: IO ()
example = do
  arr <- A.fromList [10, 20, 30 :: Int]
  A.push 40 arr
  v   <- A.lookup 2 arr    -- Just 30
  n   <- A.size arr        -- 4
  xs  <- A.toList arr      -- [10, 20, 30, 40]
  pure ()

Operations

See also

Synopsis

Type

data Array value Source #

Instances

Instances details
FromJSVal (Array value) Source # 
Instance details

Defined in Miso.Data.Array

Methods

fromJSVal :: JSVal -> IO (Maybe (Array value)) Source #

fromJSValUnchecked :: JSVal -> IO (Array value) Source #

ToJSVal (Array value) Source # 
Instance details

Defined in Miso.Data.Array

Methods

toJSVal :: Array value -> IO JSVal Source #

ToObject (Array value) Source # 
Instance details

Defined in Miso.Data.Array

Methods

toObject :: Array value -> IO Object Source #

Construction

new :: IO (Array value) Source #

Constructs a new JS Array in IO.

fromList Source #

Arguments

:: ToJSVal value 
=> [value]

Elements to populate the new array with (in order)

-> IO (Array value) 

Construct a Array from a list of values.

Deconstruction

toList :: FromJSVal value => Array value -> IO [value] Source #

Converts an Array to a list.

Operations

insert Source #

Arguments

:: ToJSVal value 
=> Int

Index at which to insert the value (0-based)

-> value

Value to store at that index

-> Array value

Array to mutate

-> IO () 

Inserts a value into the Array by value.

push Source #

Arguments

:: ToJSVal value 
=> value

Value to append

-> Array value

Array to mutate

-> IO () 

Appends a value to the end of the Array.

member Source #

Arguments

:: ToJSVal value 
=> value

Value to search for (uses JavaScript SameValueZero equality)

-> Array value

Array to search

-> IO Bool 

Checks existence of value in Array, returns t'Bool.

size :: Array value -> IO Int Source #

Return the size of Array.

splice Source #

Arguments

:: ToJSVal value 
=> Int

Start index (0-based) at which to begin the splice

-> Int

Number of elements to remove starting at start

-> [value]

Elements to insert at start after the removal

-> Array value

Array to mutate in place

-> IO (Array value) 

Splices an array. See splice.

singleton Source #

Arguments

:: ToJSVal a 
=> a

The single element for the new array

-> IO (Array a) 

Creates a new Array with a single element.

pop :: FromJSVal a => Array a -> IO (Maybe a) Source #

Removes the last element from an array and returns it.

Returns Nothing if the Array is empty.

shift :: FromJSVal a => Array a -> IO (Maybe a) Source #

Removes the first element from an array and returns it.

unshift Source #

Arguments

:: ToJSVal a 
=> a

Element to prepend at index 0

-> Array a

Array to mutate

-> IO Int 

Adds one or more elements to the beginning of an array.

null :: Array value -> IO Bool Source #

Return the null of Array.

lookup Source #

Arguments

:: FromJSVal value 
=> Int

0-based index to look up

-> Array value

Array to query

-> IO (Maybe value) 

Look up a value in the array by key.

(!?) Source #

Arguments

:: FromJSVal value 
=> Int

0-based index to look up

-> Array value

Array to query

-> IO value 

Look up a value in the array by index, throwing if out of bounds.

reverse :: Array a -> IO () Source #

Reverses an array in-place.