| Copyright | (C) 2016-2026 David M. Johnson |
|---|---|
| License | BSD3-style (see the file LICENSE) |
| Maintainer | David M. Johnson <code@dmj.io> |
| Stability | experimental |
| Portability | non-portable |
| Safe Haskell | None |
| Language | Haskell2010 |
Miso.Data.Array
Description
Overview
Miso.Data.Array is a Haskell wrapper around the JavaScript
Array
object. Values of type live in JavaScript memory; all
operations run in Array aIO 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.push40 arr v <- A.lookup2 arr -- Just 30 n <- A.sizearr -- 4 xs <- A.toListarr -- [10, 20, 30, 40] pure ()
Operations
- Construction:
new,fromList,singleton - Deconstruction:
toList - Access:
lookup,(!?),member,size,null - Mutation:
insert,push,pop,shift,unshift,splice,reverse
See also
- Miso.Data.Map — mutable JS
Map - Miso.Data.Set — mutable JS
Set - Miso.DSL —
ToJSVal/FromJSValused by element types
Synopsis
- data Array value
- new :: IO (Array value)
- fromList :: ToJSVal value => [value] -> IO (Array value)
- toList :: FromJSVal value => Array value -> IO [value]
- insert :: ToJSVal value => Int -> value -> Array value -> IO ()
- push :: ToJSVal value => value -> Array value -> IO ()
- member :: ToJSVal value => value -> Array value -> IO Bool
- size :: Array value -> IO Int
- splice :: ToJSVal value => Int -> Int -> [value] -> Array value -> IO (Array value)
- singleton :: ToJSVal a => a -> IO (Array a)
- pop :: FromJSVal a => Array a -> IO (Maybe a)
- shift :: FromJSVal a => Array a -> IO (Maybe a)
- unshift :: ToJSVal a => a -> Array a -> IO Int
- null :: Array value -> IO Bool
- lookup :: FromJSVal value => Int -> Array value -> IO (Maybe value)
- (!?) :: FromJSVal value => Int -> Array value -> IO value
- reverse :: Array a -> IO ()
Type
Construction
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
Operations
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.
Appends a value to the end of the Array.
Arguments
| :: ToJSVal value | |
| => value | Value to search for (uses JavaScript |
| -> Array value | Array to search |
| -> IO Bool |
Checks existence of value in Array, returns t'Bool.
Arguments
| :: ToJSVal value | |
| => Int | Start index (0-based) at which to begin the splice |
| -> Int | Number of elements to remove starting at |
| -> [value] | Elements to insert at |
| -> Array value | Array to mutate in place |
| -> IO (Array value) |
Splices an array. See splice.
Creates a new Array with a single element.
shift :: FromJSVal a => Array a -> IO (Maybe a) Source #
Removes the first element from an array and returns it.
Adds one or more elements to the beginning of an array.
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.
Look up a value in the array by index, throwing if out of bounds.