Oldes / Rebol-issues

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

Logical and Bitshift operators for vectors #2617

Open ldci opened 3 months ago

ldci commented 3 months ago

In Red logical operators (and, or and xor) can be used with vectors. This is not the case with R3. Here is a general function for R3 and Red:

lbsScalar: func [
    v   [vector!]
    value   [integer!]
    op  [integer!]
][
    _v: make vector! length? v
    case [
        op = 0 [repeat i length? v [_v/:i: v/:i % value]]   ;--remainder
        op = 1 [repeat i length? v [_v/:i: v/:i and value]] ;--logical
        op = 2 [repeat i length? v [_v/:i: v/:i or value]]  ;--logical
        op = 3 [repeat i length? v [_v/:i: v/:i xor value]] ;--logical 
        op = 4 [repeat i length? v [_v/:i: v/:i >> value]]  ;--bitshift
        op = 5 [repeat i length? v [_v/:i: v/:i << value]]  ;--bitshift
    ]
    _v
]

And the samples

print "lbsScalar function test"
v: make vector! [integer! 32 4 [1 2 3 4]]
print ["v      :" v]
print ["%      :" lbsScalar v 2 0]
print ["And    :" lbsScalar v 1 1]
print ["Or     :" lbsScalar v 1 2]
print ["Xor    :" lbsScalar v 1 3]
print [">>     :" lbsScalar v 2 4]
print ["<<     :" lbsScalar v 2 5]
ldci commented 3 months ago

With R3 you can replace length? v with v/length in lbsScalar function.

Oldes commented 3 months ago

related: https://github.com/Oldes/Rebol-issues/issues/2524

Oldes commented 3 months ago

@ldci your code would be more clear, if you would use words instead of integers for ops.

Also I cannot check it myself now, but in Red, does the operation modifies the vector or returns a new one? I would implement it myself by modifying it personally.

ldci commented 3 months ago

Hi David You’re right, better with word In Red x + y returns a new vector without modifying x and y

[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 à 14:20, Oldes Huhuman @.***> a écrit :

@ldcihttps://github.com/ldci your code would be more clear, if you would use words instead of integers for ops.

Also I cannot check it myself now, but in Red, does the operation modifies the vector or returns a new one? I would implemented it as modifying personally.

— Reply to this email directly, view it on GitHubhttps://github.com/Oldes/Rebol-issues/issues/2617#issuecomment-2293408957, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAXI3ILXJH6XFEDQOHR5F2DZRXVADAVCNFSM6AAAAABMSGAVH6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEOJTGQYDQOJVG4. You are receiving this because you were mentioned.Message ID: @.***>

Oldes commented 3 months ago

I'm not sure if the "always copy" is the best solution, because what if you want to modify the vector only?