Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating vectors with external blocks #2616

Open
ldci opened this issue Aug 15, 2024 · 3 comments
Open

Creating vectors with external blocks #2616

ldci opened this issue Aug 15, 2024 · 3 comments

Comments

@ldci
Copy link

ldci commented Aug 15, 2024

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
Copy link
Owner

Oldes commented Aug 16, 2024

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
Copy link
Owner

Oldes commented Aug 16, 2024

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
Copy link
Author

ldci commented Aug 16, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants