named-tuples: experiment to treat some tuples as sequences/assocs.

windows-high-dpi
John Benediktsson 2018-02-17 10:00:34 -08:00
parent 52de821e19
commit 0ea0285e33
5 changed files with 69 additions and 0 deletions

View File

@ -0,0 +1 @@
John Benediktsson

View File

@ -0,0 +1,5 @@
USING: assocs classes.mixin help.markup help.syntax named-tuples
sequences ;
HELP: named-tuple
{ $class-description "A " { $link mixin-class } " that allows you to treat a tuple as both a " { $link sequence } " and an " { $link assoc } "." } ;

View File

@ -0,0 +1,29 @@
USING: arrays assocs named-tuples sequences tools.test ;
IN: named-tuples.tests
TUPLE: foo x y z ;
INSTANCE: foo named-tuple
{ { f f f } } [ T{ foo } >array ] unit-test
{ { 1 f f } } [ T{ foo f 1 } >array ] unit-test
{ { 1 2 f } } [ T{ foo f 1 2 } >array ] unit-test
{ { 1 2 3 } } [ T{ foo f 1 2 3 } >array ] unit-test
{ T{ foo } } [ { } T{ foo } like ] unit-test
{ T{ foo f 1 } } [ { 1 } T{ foo } like ] unit-test
{ T{ foo f 1 2 } } [ { 1 2 } T{ foo } like ] unit-test
{ T{ foo f 1 2 3 } } [ { 1 2 3 } T{ foo } like ] unit-test
{ { { "x" f } { "y" f } { "z" f } } } [ T{ foo } >alist ] unit-test
{ { { "x" 1 } { "y" f } { "z" f } } } [ T{ foo f 1 } >alist ] unit-test
{ { { "x" 1 } { "y" 2 } { "z" f } } } [ T{ foo f 1 2 } >alist ] unit-test
{ { { "x" 1 } { "y" 2 } { "z" 3 } } } [ T{ foo f 1 2 3 } >alist ] unit-test
{ f } [ T{ foo } "x" of ] unit-test
{ f } [ T{ foo } "y" of ] unit-test
{ f } [ T{ foo } "z" of ] unit-test
{ 1 } [ T{ foo f 1 2 3 } "x" of ] unit-test
{ 2 } [ T{ foo f 1 2 3 } "y" of ] unit-test
{ 3 } [ T{ foo f 1 2 3 } "z" of ] unit-test

View File

@ -0,0 +1,33 @@
! Copyright (C) 2018 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: accessors assocs classes classes.tuple
classes.tuple.private kernel sequences sequences.private
slots.private ;
IN: named-tuples
MIXIN: named-tuple
M: named-tuple assoc-size tuple-size ;
M: named-tuple at* get-slot-named t ;
M: named-tuple set-at set-slot-named ;
M: named-tuple >alist
dup class-of all-slots
[ [ offset>> slot ] [ name>> ] bi swap ] with { } map>assoc ;
INSTANCE: named-tuple assoc
M: named-tuple length tuple-size ;
M: named-tuple nth-unsafe array-nth ;
M: named-tuple set-nth-unsafe set-array-nth ;
M: named-tuple like class-of slots>tuple ;
INSTANCE: named-tuple sequence

View File

@ -0,0 +1 @@
Named tuples are "tuples as sequences"