This is the latest docs version
Quick Links
  • -Overview
  • -Language Features
  • -JS Interop
  • -Build System
Documentation
Language Manual
Reference for all language features
ReScript & React
First class bindings for ReactJS
GenType
Seamless TypeScript integration
Reanalyze
Dead Code & Termination analysis
Exploration
Packages
Explore third party libraries and bindings
Syntax Lookup
Discover all syntax constructs
APIPlaygroundBlogCommunity
  • Playground
  • Blog
  • Twitter
  • GitHub
  • Forum
Core Module
Overview
Core
submodules
  • Array
  • ArrayBuffer
  • AsyncIterator
  • BigInt
  • BigInt64Array
    • Constants
    BigUint64Array
    • Constants
  • Console
  • DataView
  • Date
    • UTC
  • Dict
  • Error
    • URIError
    • TypeError
    • SyntaxError
    • ReferenceError
    • RangeError
    • EvalError
  • Exn
  • Float
    • Constants
    Float32Array
    • Constants
    Float64Array
    • Constants
    Int
    • Constants
    Int16Array
    • Constants
    Int32Array
    • Constants
    Int8Array
    • Constants
  • Internal
  • Intl
    • Segments
    • Segmenter
    • RelativeTimeFormat
    • PluralRules
    • NumberFormat
      • Grouping
    • Locale
    • ListFormat
    • DateTimeFormat
    • Collator
    • Common
  • Iterator
    • t
      t
    • t
      value
    • v
      next
    • v
      toArray
    • v
      toArrayWithMapper
    • v
      forEach
  • JSON
    • Decode
    • Encode
    • Classify
  • List
  • Map
  • MapperRt
  • Math
    • Int
    • Constants
  • Null
  • Nullable
  • Object
  • Option
  • Ordering
  • Promise
  • Re
    • Result
    RegExp
    • Result
  • Result
  • Set
  • String
  • Symbol
  • Type
    • Classify
  • TypedArray
  • Uint16Array
    • Constants
    Uint32Array
    • Constants
    Uint8Array
    • Constants
    Uint8ClampedArray
    • Constants
  • WeakMap
  • WeakSet
  • API / Core / Iterator

    Iterator

    Bindings to JavaScript iterators.

    See iterator protocols on MDN.

    t

    RESCRIPT
    type t<'a>

    The type representing an iterator.

    value

    RESCRIPT
    type value<'a> = {done: bool, value: option<'a>}

    The current value of an iterator.

    next

    RESCRIPT
    let next: t<'a> => value<'a>

    Returns the next value of the iterator, if any.

    See iterator protocols on MDN.

    Examples

    RESCRIPT
    @val external someIterator: Iterator.t<int> = "someIterator" // Pulls out the next value of the iterator let {Iterator.done, value} = someIterator->Iterator.next

    toArray

    RESCRIPT
    let toArray: t<'a> => array<'a>

    Turns an iterator into an array of the remaining values. Remember that each invocation of next of an iterator consumes a value. Iterator.toArray will consume all remaining values of the iterator and return them in an array to you.

    See iterator protocols on MDN.

    Examples

    RESCRIPT
    let map = Map.make() map->Map.set("someKey", "someValue") map->Map.set("someKey2", "someValue2") // `Map.keys` returns all keys of the map as an iterator. let mapKeysAsArray = map->Map.keys->Iterator.toArray Console.log(mapKeysAsArray) // Logs ["someKey", "someKey2"] to the console.

    toArrayWithMapper

    RESCRIPT
    let toArrayWithMapper: (t<'a>, 'a => 'b) => array<'b>

    toArray(iterator) turns iterator into an array of its remaining values, applying the provided mapper function on each item. Remember that each invocation of next of an iterator consumes a value. Iterator.toArrayWithMapper will consume all remaining values of the iterator and return them in an array to you.

    See iterator protocols on MDN.

    Examples

    RESCRIPT
    let map = Map.make() map->Map.set("someKey", "someValue") map->Map.set("someKey2", "someValue2") // `Map.keys` returns all keys of the map as an iterator. let mapKeysAsArray = map ->Map.keys ->Iterator.toArrayWithMapper(key => key->String.length) Console.log(mapKeysAsArray) // Logs [7, 8] to the console.

    forEach

    RESCRIPT
    let forEach: (t<'a>, option<'a> => unit) => unit

    forEach(iterator, fn) consumes all values in the iterator and runs the callback fn for each value.

    See iterator protocols on MDN.

    Examples

    RESCRIPT
    @val external someIterator: Iterator.t<int> = "someIterator" someIterator->Iterator.forEach(value => switch value { | Some(value) if value > 10 => Console.log("More than 10!") | _ => () } )
    Types and values
    • t
      t
    • t
      value
    • v
      next
    • v
      toArray
    • v
      toArrayWithMapper
    • v
      forEach

    © 2024 The ReScript Project

    Software and assets distribution powered by KeyCDN.

    About
    • Community
    • ReScript Association
    Find us on