collect
a bit with more magic :) Faster, less allocation-prone. Makes sense only for compiled code though or for small collections (n < 10) https://gist.github.com/hiiamboris/a624c3d767dfe4e4ed20f23bc96433ff
also
. Why not just body': body coll': coll' shoot :keep coll
?owner
the series/object? none
ing old
's owner works fine but when I try to modify
new series I get access violation
at the last line (reverse/part r/a 2
):r: make object! [
a: [1 2 3]
b: [4 5 6]
on-deep-change*: func [owner word target action new index part][
print ['on-deep-change* tab word]
]
on-change*: func [word old new][
print ['on-change* tab word]
unless all [block? :old block? :new same? head :old head :new][
if any [series? :old object? :old][modify old 'owned none]
if any [series? :new object? :new][modify new 'owned reduce [self word]]
]
]
]
reverse/part r/a 2
r/a: [3 2 1] ; <-- set field to a new series which object does not 're-owns'
reverse/part r/a 2
named-func: func [_func-name spec body] [func spec bind body context [func-name: _func-name] ]
foo: named-func 'foo [a b] [probe a + b probe func-name]
foo 2 3
; 5
; foo
set
so you don't have to repeat yourself (foo: named-func 'foo
):named-func2: func [_func-name spec body] [set _func-name func spec bind body context [func-name: _func-name] ]
named-func2 'qux [a b] [probe a + b probe func-name]
qux 2 3
; 5
; qux
>> foo: has [dummy][print pick find body-of system/words context? 'dummy -1]
== func [/local dummy][print pick find body-of system/words context? 'dummy -1]
>> foo
foo
system/words
to be more flexible.
default
function in Rebol, that does exactly this:>> ?? default
default: func [
{Set a word to a default value if it hasn't been set yet.}
'word [word! set-word! lit-word!] "The word (use :var for word! values)"
value "The value"
][
unless all [value? word not none? get word] [set word :value] :value
]
count
will be local, initialized to none
and never set to 0
.
counter: function [/extern count][
if not value? 'count [count: 0]
count: count + 1
]
call does not seem to work e.g call "notepad.exe" , using windows 10. am I calling it correctly ?
'''
red>> call explorer.exe
Script Error: call has no value
Where: catch
red>> call/shell "notepad.exe"
Script Error: call has no value
Where: catch
red>> help call
Word call is not defined
'''
>> profile/count/show [[load replace/all mold 1.2.3 dot space][collect [foreach byte to binary! 1.2.3 [keep byte]]]] 10000
Count: 10000
Time | Time (Per) | Memory | Code
0:00:00.116 | 0:00:00 | 9060352 | [collect [foreach byte to binary! 1.2.3 [keep byte]]]
0:00:00.703 | 0:00:00 | 4448256 | [load replace/all mold 1.2.3 dot space]
>> profile/count/show [[load replace/all mold 1.2.3 dot space][collect [foreach byte to binary! 1.2.3 [keep byte]]]] 10000
Count: 10000
Time | Time (Per) | Memory | Code
0:00:00.114 | 0:00:00 | 9060352 | [collect [foreach byte to binary! 1.2.3 [keep byte]]]
0:00:00.698 | 0:00:00 | 2347008 | [load replace/all mold 1.2.3 dot space]
>> profile/count/show [[load replace/all mold 1.2.3 dot space][collect [foreach byte to binary! 1.2.3 [keep byte]]]] 10000
Count: 10000
Time | Time (Per) | Memory | Code
0:00:00.113 | 0:00:00 | 9060352 | [collect [foreach byte to binary! 1.2.3 [keep byte]]]
0:00:00.725 | 0:00:00 | 2347008 | [load replace/all mold 1.2.3 dot space]
to-block
is even worse memory-wise (to block!
is same):>> profile/count/show [[to-block replace/all mold 1.2.3 dot space][collect [foreach byte to binary! 1.2.3 [keep byte]]]] 10000
Count: 10000
Time | Time (Per) | Memory | Code
0:00:00.112 | 0:00:00 | 9060352 | [collect [foreach byte to binary! 1.2.3 [keep byte]]]
0:00:00.717 | 0:00:00 | 31764480 | [to-block replace/all mold 1.2.3 dot space]
>> profile/count/show [[to-block replace/all mold 1.2.3 dot space][collect [foreach byte to binary! 1.2.3 [keep byte]]]] 10000
Count: 10000
Time | Time (Per) | Memory | Code
0:00:00.111 | 0:00:00 | 9060352 | [collect [foreach byte to binary! 1.2.3 [keep byte]]]
0:00:00.7 | 0:00:00 | 33865728 | [to-block replace/all mold 1.2.3 dot space]
>> profile/count/show [[to-block replace/all mold 1.2.3 dot space][collect [foreach byte to binary! 1.2.3 [keep byte]]]] 10000
Count: 10000
Time | Time (Per) | Memory | Code
0:00:00.112 | 0:00:00 | 9060352 | [collect [foreach byte to binary! 1.2.3 [keep byte]]]
0:00:00.7 | 0:00:00 | 33865728 | [to-block replace/all mold 1.2.3 dot space]
a: [
result: make block! size: length? bytes: to binary! 1.2.3
loop size [append result take bytes]
]
b: [collect [foreach byte to binary! 1.2.3 [keep byte]]]
profile/show/count [a b] 10'000
Count: 10000
Time | Time (Per) | Memory | Code
0:00:00.035 | 0:00:00 | 2510848 | a
0:00:00.125 | 0:00:00 | 9060352 | b