Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Oct 20 2019 22:59
    @dockimbel banned @SmackMacDougal
  • Dec 03 2017 05:53
    @PeterWAWood banned @matrixbot
  • Sep 28 2016 12:19
    @PeterWAWood banned @TimeSeriesLord
  • Aug 13 2016 03:23
    @PeterWAWood banned @Vexercizer
GiuseppeChillemi
@GiuseppeChillemi

@gurzgri

Even if should-be-localwould've been declared in the function's context, bind 'should-be-local 'test would rebind it to the context thetest word is bound to. With the context of the testword being the global context rather than the function's context (as you seem to assume).

If I had a way to express bind this word to test function inner context and not to the outher (global) one. But SELFseems it doesn't exists and this is now blocking.

Christian Ensel
@gurzgri
@GiuseppeChillemi There may be other ways, e.g. something like
spec: [add-table delete-table [db table] add-row [db table row]] ;-- function specs 
code: [add-table [a: 1] delete-table [b: 2] add-row [c: 3]]      ;-- function bodies

db: object collect [foreach name spec [all [
    word? name
    keep reduce [to set-word! name 'function first find find spec name block! select code name]
]]]

? db
GiuseppeChillemi
@GiuseppeChillemi
@gurzgri Yes, it is another way.
But a question remains: why SELF isn't available for functions ?
Is it planned ?
Christian Ensel
@gurzgri
@GiuseppeChillemi , Sorry, I can't make any statements on Red features planned/not planned. I know of no use cases which would require it.
GiuseppeChillemi
@GiuseppeChillemi
@gurzgri extending the words of a function and binding them to the function inner context actually seems an use case.
Christian Ensel
@gurzgri
@GiuseppeChillemi With extend on objects being reserved for future use (or not yet implemented) it seems having selfin functions wouldn't gain you anything for now (I may be missing something here). Why not just have a function local word set to a map! and extend that if you think that's the way to go?
GiuseppeChillemi
@GiuseppeChillemi
@gurzgri Christian, I feel that something is missing not having the ability to access SELF for functions and if you want dynamically add some words from the inner of it, you simply can't do it. But maybe I am still to "young" to express a more argumented opinion.
About EXTEND, I know you can't actually extend an object and I'll wait with curiosity in mind how it will be implemented.
hiiamboris
@hiiamboris

But a question remains: why SELF isn't available for functions ?

@GiuseppeChillemi It's easy: self is used mostly inside object's functions to address that object. If it was overridden inside a function, you wouldn't be able reference the object anymore.

GiuseppeChillemi
@GiuseppeChillemi
@hiiamboris what you mean for overriding It in a function as It does not exists ?
Christian Ensel
@gurzgri
@GiuseppeChillemi Given selfwould be available in functions to access a function's context, self would no longer be available to access the context of a function's outer (object) context. Which is an ubiquitous pattern with OOP, and no doubt used in large codebases.
JLCyclo
@JLCyclo
yep! About github and repository red/community... I would like to add a script in the repository, but I can't make the pull request.. I click "create pull request" on github but nothing appears...
I made a fork: cyclo07/community and I woul like to create a pull request for red/community
GiuseppeChillemi
@GiuseppeChillemi

@gurzgri

@GiuseppeChillemi Given selfwould be available in functions to access a function's context, self would no longer be available to access the context of a function's outer (object) context. Which is an ubiquitous pattern with OOP, and no doubt used in large codebases.

I have read the article. In my scenario, a function is like an encapsulating object for words and functions but it has no way to access extend itself using SELF. It is somehow more limited than an object. While the encapsulator object method to have local variables and access them from the inner function solves thing, it forces to use a OBJECT-NAME/FUNCTION-NAME notation where FUNCTION-NAME is shorter and more readable.

GiuseppeChillemi
@GiuseppeChillemi
@gurzgri
I can see why function SELF could override SELF of the encapsulator object. Also like a newbye I could suggest having SELF with parents to navigate SELVES backwards if possible but I imagine is not so easy. Regarding this topic I have read there is no backlink in blocks and @9214 once "suggested" to read the code and try to write a double linked table (or something like this). Maybe it is the same reason why we have not nested SELVES which are backward accesible.
nedzadarek
@nedzadarek

@GiuseppeChillemi about "unnecessary" lines:
You can always edit it in the browser. Choice is yours.

About local words:
There are probably many ways to simulate it. You can keep local words in a map (1) or bind body words to some context (2).
1) f: function [/local local-words] [local-words: #() local-words/should-be-local: 42]
2) g: function [] bind [should-be-local] context [should-be-local: 42]

GiuseppeChillemi
@GiuseppeChillemi
@nedzadarek What you mean for "you can always edit in the browser" ?
About solution number 2: I do not understand it ;-)
Semseddin Moldibi
@endo64
@JLCyclo You should click on the Pull request link on your own fork: https://github.com/JLCyclo/community
Here you can see:
image.png
nedzadarek
@nedzadarek

When you paste your code into gitter ("Click here to type chat message...") you can delete/remove (or add) some characters.
For example this:

>> 7 * 6
== 42

I will change to this:

7 * 6
; == 42

(no >> and added comment - ;)

@GiuseppeChillemi

About solution number 2: I do not understand it ;-)

You know what binding is?

nedzadarek
@nedzadarek
ps. It might not work with recursive functions.
Greg T
@gltewalt
What is this strangeness about?
>> n: now
== 23-Feb-2019/23:48:26-07:00

>> repeat i 15 [print ["picking" i "from n returns:" pick n i]]
picking 1 from n returns: 23-Feb-2019
picking 2 from n returns: 2019
picking 3 from n returns: 2
picking 4 from n returns: 23
picking 5 from n returns: -7:00:00
picking 6 from n returns: 23:48:26
picking 7 from n returns: 23
picking 8 from n returns: 48
picking 9 from n returns: 26.0
picking 10 from n returns: 6
picking 11 from n returns: 54
picking 12 from n returns: -7:00:00
picking 13 from n returns: 8
picking 14 from n returns: 8
*** Script Error: value out of range: 15
*** Where: pick
*** Stack:
And with time!
>> pick 12:23 1
== 12
>> pick 12:23 2
== 23
>> pick 12:23 3
== 0.0
>> pick 12:23 4
*** Script Error: value out of range: 4
*** Where: pick
*** Stack:
Greg T
@gltewalt
Something I don't understand at the moment, or bugs?
Boleslav Březovský
@rebolek
see system/catalog/accessors
nedzadarek
@nedzadarek
@gltewalt I think time! is fine -> 1st, 2nd and 3rd values are hour, minutes and seconds with milliseconds (in float).
Toomas Vooglaid
@toomasv
@gltewalt Not bug. There are 15 accessors but 14 fields. 15 is not defined in date.reds:
>> pick system/catalog/accessors/date! 15
== julian
>> n/julian
== 55
>> n/15
*** Script Error: cannot access 15 in path n/15
And here's why there are 14 fields only:
sym = words/yearday     [field: 11]
sym = words/julian [field: 11]
hiiamboris
@hiiamboris
@toomasv but this (2 symbols mapped to one field) is a bug
Toomas Vooglaid
@toomasv
@hiiamboris Isn't it just an alias?
hiiamboris
@hiiamboris

That's what I just read on google:

The Julian day or Julian day number (JDN) is the number of days that have passed since the initial epoch defined as noon Universal Time (UT) Monday, January 1, 4713 BC in the Julian calendar

Toomas Vooglaid
@toomasv
Oh, then it is not yet implemented.
GiuseppeChillemi
@GiuseppeChillemi

@nedzadarek

@GiuseppeChillemi

About solution number 2: I do not understand it ;-)

You know what binding is?

I am learning it..

GiuseppeChillemi
@GiuseppeChillemi

@nedzadarek

Here:

2) g: function [] bind [should-be-local] context [should-be-local: 42]

g: function []

Your are defining the first part of the function

 bind [should-be-local]

Here I don't understand why [should-be-local] instead of 'should-be-local

 context [should-be-local: 42]

Here you create and object which is the body of the function.

I am missing the connection between the head of the function and its body with bind.

Greg T
@gltewalt
Got it
nedzadarek
@nedzadarek

@GiuseppeChillemi

Here I don't understand why [should-be-local] instead of 'should-be-local

Because I want to create a block not just a word (block? (bind [a] context[]) ; == true). function takes 2 arguments: 2 blocks (spec and body).

spec: [] 
body: [] 
function spec body

Let me group function's arguments using parens: function ( [] ) (bind [should-be-local] context [should-be-local: 42])
function's body is an empty block ([]) and body is bind [should-be-local] context [should-be-local: 42]
bind BLOCK context [] will return a block - this is what function needs. I couldn't use words (bind 'word context[]) because it wouldn't return a block.

JLCyclo
@JLCyclo
blob
@endo64 thanks, but it does not seem to work...
GiuseppeChillemi
@GiuseppeChillemi

@nedzadarek
Tried on the console

>> g: function [] bind [should-be-local] context [should-be-local: 42]
== func [][should-be-local]

What I has still not encountered is binding on a block.
I'll try to understand what we are doing, context and binding.
Here you are creating the second block of function. The second block consist of only a word which is should-be-local.
You have then created a context where should-be-local value is 42.
Then you have "linked" (bound) the context to the second function block, so should-be-local value is 42 in that context.
Function, which has become FUNC (why?) has now 2 block. An empty one, and the newly defined.

nedzadarek
@nedzadarek

@GiuseppeChillemi

function, which has become func (why?)

In the red you have "function constructors" (not sure if this is the right term). They just modifies spec and/or body argument (both are blocks) and "send" them to the func.

>> has [][]
== func [/local][]
>> does[]
== func [][]
>> func [] []
== func [][]
>> function [] []
== func [][]

Here is a little example where my newly created function constructor makes all function prints "Answer: 42":

answer-func: func [spec body] [
  insert body [print "Answer: 42"] 
  body: back body 
  func spec body
]
f: answer-func [a] [print a * 10]
f 3
; Answer: 42
; 30

g: answer-func [] [print "Hello world"]
g
; Answer: 42
; Hello world
nedzadarek
@nedzadarek

has now 2 block. An empty one, and the newly defined.

The empty one - specification (argument list, its types etc)
The newly defined - body of the function, what it does
^^ I think you understand this. What else you don't understand?

Semseddin Moldibi
@endo64
@JLCyclo Didn't work when you click on Create pull request button?
Anyway, I made a PR on behalf of you, from your fork to base: red/community#13
Semseddin Moldibi
@endo64
@JLCyclo Nice script by the way
Greg T
@gltewalt
If I feed it to itself it seems to step over all the code with step
JLCyclo
@JLCyclo
@endo64 Thanks for the PR
input "n(ext) | s(tep) | <enter>" at the prompt line runs 1 do/next command... (disadvantages: overall loops and functions)