Paste: Typed and nested sequences

Author: nomennescio
Mode: factor
Date: Tue, 31 Oct 2023 12:25:39
Plain Text |
! copyright 2023 nomennescio

! normal sequences are considered to have elements of type object, which carry almost no type information
! having uniform sequences with elements of the same type would be an opportunity to specialize for more performant code
! without disrupting the current hierarchies and code, one improvement would be to have such a uniform sequence for nested sequences, to allow for "deep" traversals, a common pattern in many generic words.

! error handling non-similar types (i.e. not base-class nor derived, where mixins are considered classes, and instances subclasses)

ERROR: similar-type element index sequence ;

! high-level skeleton, to be refined

MIXIN: nested-sequence
INSTANCE: nested-sequence sequence

TUPLE: nested-array < array
! additional fields, possible a type field
;

INSTANCE: nested-array nested-sequence

! more code
! setting an array to an element of a different type should fail

DEFER: }} ( -- * ) delimiter
SYNTAX: {{ \ }} [ >nested-array ] parse-literal ;

Annotation: Typed and nested sequences

Author: nomennescio
Mode: factor
Date: Tue, 31 Oct 2023 12:43:06
Plain Text |
Usage

{{ { 1 2 3 } { 4 5 6 } { 7 8 9 } }} sum
{{ {{ { 1 2 3 } { 4 5 6 } }} {{ { 7 8 9 } }} }} sum

M: sum nested-sequence [ sum ] map call-next-method ;

Annotation: Typed and nested sequences

Author: nomennescio
Mode: factor
Date: Tue, 31 Oct 2023 12:54:02
Plain Text |
GENERIC: product ( seq -- n )

M: product nested-sequence [ product ] map call-next-method ;

Annotation: Typed and nested sequences

Author: nomennescio
Mode: factor
Date: Tue, 31 Oct 2023 15:45:40
Plain Text |
! for a nested sequence, all elements are of type sequence
! we can't subclass array, as it's final :(

TUPLE: wrapped-array { underlying array read-only } ;
TUPLE: nested-array < wrapped-array ;
INSTANCE: nested-array nested-sequence

! here: code delegating nested arrays

ERROR: not-all-sequences ;

: >nested-array ( seq -- nested-array )
  dup [ sequence? ] all? [ not-all-sequences ] unless
  >array nested-array boa ;

Annotation: Typed and nested sequences

Author: nomennescio
Mode: factor
Date: Tue, 31 Oct 2023 17:18:28
Plain Text |
MIXIN: nested-sequence
INSTANCE: nested-sequence sequence

TUPLE: wrapped-array { underlying array read-only } ;
CONSULT: sequence-protocol wrapped-array underlying>> ;

TUPLE: nested-array < wrapped-array ;
INSTANCE: nested-array nested-sequence

ERROR: not-all-sequences ;

: >nested-array ( seq -- nested-array )
  dup [ sequence? ] all? [ not-all-sequences ] unless
  >array nested-array boa ;

DEFER: }} ( -- * ) delimiter
SYNTAX: {{ \ }} [ >nested-array ] parse-literal ;

Annotation: Typed and nested sequences

Author: nomennescio
Mode: factor
Date: Tue, 31 Oct 2023 17:26:51
Plain Text |
! apparently CONSULT: does not work on subclasses

MIXIN: nested-sequence
INSTANCE: nested-sequence sequence

TUPLE: wrapped-array { underlying array read-only } ;
CONSULT: sequence-protocol wrapped-array underlying>> ;

TUPLE: nested-array < wrapped-array ;
CONSULT: sequence-protocol nested-array underlying>> ;
INSTANCE: nested-array nested-sequence

ERROR: not-all-sequences ;

: >nested-array ( seq -- nested-array )
  dup [ sequence? ] all? [ not-all-sequences ] unless
  >array nested-array boa ;

DEFER: }} ( -- * ) delimiter
SYNTAX: {{ \ }} [ >nested-array ] parse-literal ;

Annotation: Typed and nested sequences

Author: nomennescio
Mode: factor
Date: Tue, 31 Oct 2023 17:29:25
Plain Text |
{{ { 1 2 3 } { 4 5 6 } { 7 8 9 } }} sum
{{ {{ { 1 2 3 } { 4 5 6 } }} {{ { 7 8 9 } }} }} sum

M: sum nested-sequence [ sum ] map sum ;

New Annotation

Summary:
Author:
Mode:
Body: