Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

A hard question

 [1/21] from: lmecir::mbox::vol::cz at: 3-Feb-2001 20:03


The task is to create a function HARD, that is a Normal Argument Passing function (i.e. nor a Fetched Argument Function, neither an Unevaluated Argument Function) and takes two arguments, ARG1 and ARG2. HARD is not required to do any argument checking, moreover, you can assume, that the supplied arguments are really the data they are supposed to be. ARG1 is a Normal Argument Passing Function taking exactly one argument. ARG2 is a block of length 1. The function HARD shall do exactly the same thing, as the function ARG1 would do if used with the first element of ARG2 as its argument. Moreover, you can assume, that the first element of ARG2 is a correct argument to ARG1. Any trials? Regards Ladislav

 [2/21] from: larry:ecotope at: 3-Feb-2001 12:45


Hi Ladislav Sounds like fun! Will this solve the puzzle?
>> hard: func [x y][do reduce [x first y]] >> hard :sine [52]
== 0.788010753606722
>> hard func [x][x * x] [52]
== 2704
>>
Cheers -Larry

 [3/21] from: lmecir:mbox:vol:cz at: 3-Feb-2001 22:07


Hi Larry, to be a solution, HARD must pass any test. Let's try: arg1: func [x] [return make error! x] arg2: "some error" probe disarm arg1 arg2 The result is: make object! [ code: 800 type: 'user id: 'message arg1: "some error" arg2: none arg3: none near: [return make error! x] where: none ] while the result of HARD is:
>> probe disarm hard :arg1 arg2
** Script Error: Invalid argument: s ** Where: x ** Near: return make error! x The question is, if HARD could really meet the requirements.

 [4/21] from: lmecir:mbox:vol:cz at: 3-Feb-2001 22:36


Correcting myself:
> to be a solution, HARD must pass any test. Let's try: >
arg1: func [x] [return make error! x] arg2: ["some error"] probe disarm arg1 first arg2 The result is: make object! [ code: 800 type: 'user id: 'message arg1: "some error" arg2: none arg3: none near: [return make error! x] where: none ]
> while the result of HARD is: >
probe disarm hard :arg1 arg2 ** User Error: some error ** Near: return make error! x

 [5/21] from: g:santilli:tiscalinet:it at: 6-Feb-2001 12:47


Ladislav Mecir wrote:
> Frank solved my problem, and that is why I am able to generalize his > solution and have:
So, SMC isn't that bad, is it? ;-) <grin>, Gabriele. -- Gabriele Santilli <[giesse--writeme--com]> - Amigan - REBOL programmer Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/

 [6/21] from: lmecir:mbox:vol:cz at: 6-Feb-2001 19:01


Hi all, I would like to continue this thread, because I see, that it didn't solve my original trouble with SELECT-PROP. Am I wrong stating, that the SELECT-PROP cannot be completely repaired? Regards Ladislav

 [7/21] from: lmecir:mbox:vol:cz at: 6-Feb-2001 19:43


Hi,
> Ladislav Mecir wrote: > > Frank solved my problem, and that is why I am able to generalize his
<<quoted lines omitted: 3>>
> Gabriele. > --
SMC looks as the only means here. That was originally the case of Curry too, now Curry works as non-SMC (see the latest version of Highfun.r at http://www.sweb.cz/LMecir ), because RT repaired the GC bug. I am still thinking, that the error behaviour is a bug and it should be repaired. I am not able to use this SMC trick to repair my SELECT-PROP function, because it uses even the CATCH attribute and it should always have a normal help string. Am I wrong? Regards Ladislav

 [8/21] from: lmecir:mbox:vol:cz at: 5-Feb-2001 21:54


Hi all, Frank solved my problem, and that is why I am able to generalize his solution and have: soft-func: function [ {Create a "soft return" function} spec [block!] body [block!] ] [modified-spec do-body thr-on fnc-return] [ modified-spec: head insert/only copy spec "Soft" use [thr fnc] [ thr-on: does [thr: on] fnc-return: func [[throw] fnc-result [any-type!]] [ either thr [ change/only third :fnc [throw] ] [ change/only third :fnc "Soft" ] return get/any 'fnc-result ] use [soft-return] [ soft-return: func [[throw] body-result [any-type!]] [ thr: off return get/any 'body-result ] do-body: bind/copy body 'soft-return ] fnc: func modified-spec reduce [ :thr-on :fnc-return :do-body ] do-body: does third second :fnc change skip second :fnc 2 :do-body :fnc ] ] with a help of SOFT-FUNC we are now able to write the HARD function or any other function with similar properties hard: soft-func [u v] [soft-return u first v] ; Error test: arg1: func [x] [return make error! x] arg2: ["some error"] probe disarm hard :arg1 arg2 make object! [ code: 800 type: 'user id: 'message arg1: "some error" arg2: none arg3: none near: [return make error! x] where: none ] arg1: func [[throw] x] [return x] arg2: [1] test: does [hard :arg1 arg2 2] test ; == 1 Regards Ladislav

 [9/21] from: rebol:techscribe at: 3-Feb-2001 19:52


Hi Ladislav, looks like fun. What if we upgrade Larry's function as follows? hard: func [x y][ if error? set/any 'error try [ do reduce [x first y] ][ return :error ] ] That solves the problem at hand. I'm curious where this will take us? Take Care, Elan Ladislav Mecir wrote:

 [10/21] from: lmecir:mbox:vol:cz at: 4-Feb-2001 9:35


Hi Elan, it's fine, that you tried too. Here are my two tests (Larry asked me to post them both someplace): ; Error test: arg1: func [x] [return make error! x] arg2: ["some error"] probe disarm arg1 first arg2 The result is: make object! [ code: 800 type: 'user id: 'message arg1: "some error" arg2: none arg3: none near: [return make error! x] where: none ] ; Return test: arg1: func [[throw] x] [return x] arg2: [1] test1: does [arg1 first arg2 2] test1 ; == 1 test2: does [hard :arg1 arg2 2] test2 ; == 2 Due to the "second class nature" of Rebol errors, HARD cannot pass both tests IMO. (You can have a look at: http://www.sweb.cz/LMecir/rep.html)
> Hi Ladislav, > > looks like fun. > > What if we upgrade Larry's function as follows? >
hard: func [x y][ if error? set/any 'error try [ do reduce [x first y] ][ return :error ] ]

 [11/21] from: lmecir:mbox:vol:cz at: 4-Feb-2001 13:29


Hi Larry, Elan, Jeff & all, I have to apologize in this case. I changed the http://www.sweb.cz/LMecir/rep.html wording to show you a real life code, not a distilled thing, that is too aesotheric to be understandable. Have a look at the sections on simplified error handling and on the SELECT-PROP function. Hope it will be much more understandable now. If not, feel free to ask and give me a hint to improve my wording. Thanks Ladislav

 [12/21] from: g:santilli:tiscalinet:it at: 4-Feb-2001 13:45


Hello Ladislav! On 03-Feb-01, you wrote: LM> Any trials? I don't think it's really hard, but I'm sure I'm missing something.
>> arg1: func [x] [return make error! x] >> arg2: ["some error"] >> hard: func [x y] [return x first y] >> print mold disarm hard :arg1 arg2
make object! [ code: 800 type: 'user id: 'message arg1: "some error" arg2: none arg3: none near: [return make error! x] where: none ]
>> hard :print ["something"]
something Regards, Gabriele. -- Gabriele Santilli <[giesse--writeme--com]> - Amigan - REBOL programmer Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/

 [13/21] from: g:santilli:tiscalinet:it at: 4-Feb-2001 14:01


Hello Ladislav! On 04-Feb-01, you wrote: LM> Due to the "second class nature" of Rebol errors, HARD cannot LM> pass both tests IMO. (You can have a look at: LM> http://www.sweb.cz/LMecir/rep.html)
>> arg1: func [[throw] x] [return x] >> arg2: [1]
== [1]
>> test2: does [hard :arg1 arg2 2] >> hard: func [[throw] x y] [return x first y] >> test2
== 1
>> arg2: ["some error"]
== ["some error"]
>> arg1: func [x] [return make error! x] >> print mold disarm hard :arg1 arg2
** Throw Error: Return or exit not in function. ** Where: return x first y
>> test3: does [print mold disarm hard :arg1 arg2] >> test3
** User Error: some error. ** Where: return make error! x
>> test4: does [hard :arg1 arg2] >> print mold disarm test4
make object! [ code: 800 type: 'user id: 'message arg1: "some error" arg2: none arg3: none near: [return make error! x] where: none ] If you want it to [throw], it will always [throw], so you need to wrap it inside another function when you don't what it too. Let me know what you think... Regards, Gabriele. -- Gabriele Santilli <[giesse--writeme--com]> - Amigan - REBOL programmer Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/

 [14/21] from: fsievert:uos at: 4-Feb-2001 14:54


Hi! You can try this: hard: func [ "Hard function" fun [any-function! path!] args [block!] /local e e2 throw2 ] [ throw2: yes error? set/any 'e2 do does compose/deep [ do [error? set/any 'e fun (args)] throw2: no ] if throw2 [set/any 'e get/any 'e2] if block? first third :hard [change/only third :hard "Hard"] if throw2 [change/only third :hard [throw]] return get/any 'e ] CU, Frank

 [15/21] from: lmecir:mbox:vol:cz at: 4-Feb-2001 14:59


Hi Gabriele, I would like to thank you for your ideas, my real problem is described in the newest wording of http://www.sweb.cz/LMecir/rep.html in the full detail. Have a look at the sections on simplified error handling and on the SELECT-PROP function. Hope it will be much more understandable now.

 [16/21] from: lmecir:mbox:vol:cz at: 4-Feb-2001 15:33


Hi Frank, I would never guess, this could have worked. You really got it!

 [17/21] from: ptretter:norcom2000 at: 4-Feb-2001 9:19


Ladislav, Excellent information on that website. This is a great resource for people since there really isnt this type of information found anywhere else in the detail you have provided. Keep up the great work. Paul Tretter

 [18/21] from: g:santilli:tiscalinet:it at: 8-Feb-2001 14:52


Ladislav Mecir wrote:
> ... > > Does SELECT-PROP really need to handle errors? > ... > > It has to handle all values that can be stored in a series.
Do REBOL mezzanine functions all handle errors correctly? I still think it is not worth the effort, until this sort-of bug is corrected (or Carl says that this is the way it has to go). Just IMHO, Gabriele. -- Gabriele Santilli <[giesse--writeme--com]> - Amigan - REBOL programmer Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/

 [19/21] from: g:santilli:tiscalinet:it at: 7-Feb-2001 13:26


Ladislav Mecir wrote:
> I would like to continue this thread, because I see, that it didn't solve my > original trouble with SELECT-PROP. Am I wrong stating, that the SELECT-PROP > cannot be completely repaired?
Does SELECT-PROP really need to handle errors? I know it should be general, but I think noone really needs that kind of functionality (in that case, one just has to workaroud, maybe creating his own SELECT-PROP). I think Carl made errors Second Class because that's the way they should be --- they are errors after all, and should be handled instead of being passed around (to postpone handling, you have the option to THROW them). If you just want to treat them as values, you really have to DISARM them. It makes sense after all, even if I'd prefer first class errors too, because they're simpler... Regards, Gabriele. -- Gabriele Santilli <[giesse--writeme--com]> - Amigan - REBOL programmer Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/

 [20/21] from: lmecir:mbox:vol:cz at: 7-Feb-2001 20:33


Hi Gabriele, ...
> Does SELECT-PROP really need to handle errors?
... It has to handle all values that can be stored in a series. I think I got it now: soft-func: function [ {Create a "soft return" function} spec [block!] body [block!] ] [thr-on fnc-return] [ spec: copy/deep spec if not string? pick spec 1 [ insert spec "(undocumented)" ] either block? pick spec 2 [ change/only next spec [catch throw] ] [ insert/only next spec [catch throw] ] use [thr fnc] [ thr-on: does [thr: on] fnc-return: func [[throw] fnc-result [any-type!]] [ change/only next third :fnc either thr [[catch throw]] ["Soft"] return get/any 'fnc-result ] use [soft-return] [ soft-return: func [[throw] body-result [any-type!]] [ thr: off return get/any 'body-result ] body: bind/copy body 'soft-return ] fnc: func spec reduce [ :thr-on :fnc-return :body ] body: does third second :fnc change skip second :fnc 2 :body :fnc ] ] select-prop: soft-func [ {Finds a value in the series and returns the value after it.} series [series! port!] value [any-type!] /part "Limits the search to a given length or position." range [number! series! port!] /only "Treats a series value as a single value." /case "Characters are case-sensitive." /any "Enables the * and ? wildcards." /with "Allows custom wildcards." wild [string!] "Specifies alternates for * and ?" /default "The negative result handling" handler [block!] /local path args result1 ] [ path: to path! 'find args: [result1:] append/only args :path append args [:series get/any 'value] if part [ append :path 'part append :args [:range] ] if only [ append :path 'only ] if case [ append :path 'case ] if any [ append :path 'any ] if with [ append :path 'with append/only :args :wild ] do args either system/words/any [ not result1 error? try [ error? set/any 'result1 second :result1 ] ] [ either default [soft-return do handler] [ throw make error! {Cannot select} ] ] [soft-return get/any 'result1] ] ; situation 1: test: does [ select-prop/default [1 a 2 b] 3 [return 4] 5 ] test ; == 4 ; situation 2: print mold disarm select-prop head insert tail copy [1 a 2 b 3] make error! "some-error" 3 make object! [ code: 800 type: 'user id: 'message arg1: "some-error" arg2: none arg3: none near: [print mold disarm select-prop head] where: none ]

 [21/21] from: lmecir:mbox:vol:cz at: 8-Feb-2001 19:22


Hi,
> Ladislav Mecir wrote: > > ...
<<quoted lines omitted: 8>>
> Gabriele. > --
Well, I wrote this to show, that there is a sort-of bug in Rebol regarding errors. OTOH, SELECT-PROP has to handle errors, because SELECT handles them too and SELECT-PROP is meant as a possible replacement for SELECT. Regards Ladislav

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted