Oldes / Rebol-issues

Issue tracker for https://github.com/oldes/Rebol3
4 stars 0 forks source link

Creating vectors with external blocks #2616

Open ldci opened 4 weeks ago

ldci commented 4 weeks ago

With R3 it's easy to use blocks when creating vectors when blocks are internal.

print "With internal block"
print make vector! [integer! 8  [#"A" #"B" #"a" #"b"]]
print make vector! [integer! 8  [1 2 3 4]]
print make vector! [decimal! 64 [1.0 2.0 3.0 4.0]]

Now the question is how to use external blocks? In this case R3 returns an error. We have the possibility with such as function:

block2Vector: func [
    b   [block!]
    vtype   [integer!] ; 0 1 2 : char! integer! decimal!
    bitSize [integer!]
][
    n: length? b
    case  [
        vtype = 0 [
            repeat i n [b/:i: to integer! b/:i] ;--avoid errors
            v: make vector! compose [integer! (bitSize) (n)]
        ]
        vtype = 1 [v: make vector! compose [integer! (bitSize) (n)]]
        vtype = 2 [v: make vector! compose [decimal! (bitSize) (n)]]
    ]
    repeat i n [v/:i: b/:i]
    v
]

And for test, some examples:

b: [#"A" #"B" #"a" #"b"]
print ["Char:  " block2Vector b 0 8]
b: [0 1 2 3 4 5 6 7 8 9]
print ["Integer:" block2Vector b 1 64]
b: [1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9]
print ["Decimal:" block2Vector b 2 64]
Oldes commented 4 weeks ago

Why you don't use the block directly? You should not copy values one by one as you do in your code. It is better to use something like:

block2Vector: function[type [word!] block [block!]][
    make vector! compose/only select make map! [
        int8    [integer! 8  (block)]
        int16   [integer! 16 (block)]
        int32   [integer! 32 (block)]
        int64   [integer! 64 (block)]
        uint8   [unsigned integer! 8  (block)]
        uint16  [unsigned integer! 16 (block)]
        uint32  [unsigned integer! 32 (block)]
        uint64  [unsigned integer! 64 (block)]
        float32 [decimal! 32 (block)]
        float64 [decimal! 64 (block)]
    ] type
]

Then:

>> b: [#"A" #"B" #"a" #"b"]
== [#"A" #"B" #"a" #"b"]

>> block2vector 'float32 b
== make vector! [decimal! 32 4 [65.0 66.0 97.0 98.0]]

>> block2vector 'uint8 b   
== make vector! [unsigned integer! 8 4 [65 66 97 98]]
Oldes commented 4 weeks ago

Maybe the make vector dialect could be smarter and accept the types as used in the construction syntax already. And resolve the data from a variable without need to use compose/only.. This could be implemented:

 make vector! [uint16! :data]

Which would be same like:

make vector! compose/only [unsigned integer! 16 (data)] 
ldci commented 4 weeks ago

Elegant :) [image001.png][image002.png]

François Jouen https://univ-psl.fr/ École Pratique des Hautes Études Doyen Honoraire de la section des Sciences de la Vie et de la Terre Chaire de Psychobiologie du Développement Plateforme Réanimation Pédiatrique Raymond Poincaré. Hôpital de Garches Laboratoire Cognitions Humaine et Artificielle Campus Condorcet 14 cours des Humanités 15 rue Waldeck Rochet 93322 Aubervilliers cedex 06 87 13 76 81 @.**@.> www.ephe.frhttp://www.ephe.fr/

Le 16 août 2024 à 13:47, Oldes Huhuman @.***> a écrit :

Why you don't use the block directly? You should not copy values one by one as you do in your code. It is better to use something like:

block2Vector: function[type [word!] block [block!]][ make vector! compose/only select make map! [ int8 [integer! 8 (block)] int16 [integer! 16 (block)] int32 [integer! 32 (block)] int64 [integer! 64 (block)] uint8 [unsigned integer! 8 (block)] uint16 [unsigned integer! 16 (block)] uint32 [unsigned integer! 32 (block)] uint64 [unsigned integer! 64 (block)] float32 [decimal! 32 (block)] float64 [decimal! 64 (block)] ] type ]

Then:

b: [#"A" #"B" #"a" #"b"] == [#"A" #"B" #"a" #"b"]

block2vector 'float32 b == make vector! [decimal! 32 4 [65.0 66.0 97.0 98.0]]

block2vector 'uint8 b == make vector! [unsigned integer! 8 4 [65 66 97 98]]

— Reply to this email directly, view it on GitHubhttps://github.com/Oldes/Rebol-issues/issues/2616#issuecomment-2293359930, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAXI3IODYVIGVRCEFKCQAA3ZRXREFAVCNFSM6AAAAABMR24CGWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEOJTGM2TSOJTGA. You are receiving this because you authored the thread.Message ID: @.***>