Bloc of code in tiers programming language

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
29 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Bloc of code in tiers programming language

HilaireFernandes
Hi,

We, Smalltalkers, use bloc of code as easily as we breathe air.

I am writing an article on Smalltalk programming for a French
mathematics teachers magazine.

To illustrate the simplicity of Smalltalk, I would like to compare how
the bloc of code 'f' and 'df' below will be implemented in Javascript
and Python:


f := [ :x | x cos + x ].
df := [ :x | (f value: x + 1e-8) - (f value: x) * 1e8].

Here f is a way to implement a function and df its derivate.

Do some of you knows how it will be written in Javascript and Python
with their own ad-hoc anonymous function?

Thanks

Hilaire

--
Dr. Geo
http://drgeo.eu



Reply | Threaded
Open this post in threaded view
|

Re: Bloc of code in tiers programming language

Atharva Khare
Hey,

I think in python, you use Lambda Expressions. Here is how I would do it in python3:
import math
f = lambda x: math.cos(x) + x
d_f = lambda x: (f(x + 1e-8) - f(x)) * 1e8



On Wed, May 15, 2019 at 6:33 PM Hilaire <[hidden email]> wrote:
Hi,

We, Smalltalkers, use bloc of code as easily as we breathe air.

I am writing an article on Smalltalk programming for a French
mathematics teachers magazine.

To illustrate the simplicity of Smalltalk, I would like to compare how
the bloc of code 'f' and 'df' below will be implemented in Javascript
and Python:


f := [ :x | x cos + x ].
df := [ :x | (f value: x + 1e-8) - (f value: x) * 1e8].

Here f is a way to implement a function and df its derivate.

Do some of you knows how it will be written in Javascript and Python
with their own ad-hoc anonymous function?

Thanks

Hilaire

--
Dr. Geo
http://drgeo.eu



Reply | Threaded
Open this post in threaded view
|

Re: Bloc of code in tiers programming language

eftomi
In javascript I believe is

var f = function(x) { return Math.cos(x) + x; }
var df = function(x) { return f(x + 1e-8) - f(x) * 1e8; } 

Best wishes,
Tomaz

------ Original Message ------
From: "Atharva Khare" <[hidden email]>
To: "Any question about pharo is welcome" <[hidden email]>
Sent: 15.5.2019 15:26:11
Subject: Re: [Pharo-users] Bloc of code in tiers programming language

Hey,

I think in python, you use Lambda Expressions. Here is how I would do it in python3:
import math
f = lambda x: math.cos(x) + x
d_f = lambda x: (f(x + 1e-8) - f(x)) * 1e8



On Wed, May 15, 2019 at 6:33 PM Hilaire <[hidden email]> wrote:
Hi,

We, Smalltalkers, use bloc of code as easily as we breathe air.

I am writing an article on Smalltalk programming for a French
mathematics teachers magazine.

To illustrate the simplicity of Smalltalk, I would like to compare how
the bloc of code 'f' and 'df' below will be implemented in Javascript
and Python:


f := [ :x | x cos + x ].
df := [ :x | (f value: x + 1e-8) - (f value: x) * 1e8].

Here f is a way to implement a function and df its derivate.

Do some of you knows how it will be written in Javascript and Python
with their own ad-hoc anonymous function?

Thanks

Hilaire

--
Dr. Geo
http://drgeo.eu



Reply | Threaded
Open this post in threaded view
|

Re: Bloc of code in tiers programming language

Richard O'Keefe
In reply to this post by HilaireFernandes
One point worth making is that Python lambdas are artificially restricted:
the body of a Python lambda may only be a single expression, not a sequence
of statements.  This restriction is for ideological reasons (the BDFL does
not *want* you to do that) not for technical reasons.  Lisp and Algol 68
were doing lambda expressions in the 60s.

As well as function (x, y) { ... },
modern Javascript has (x, y) => ...
with subtly different semantics.



On Thu, 16 May 2019 at 01:03, Hilaire <[hidden email]> wrote:
Hi,

We, Smalltalkers, use bloc of code as easily as we breathe air.

I am writing an article on Smalltalk programming for a French
mathematics teachers magazine.

To illustrate the simplicity of Smalltalk, I would like to compare how
the bloc of code 'f' and 'df' below will be implemented in Javascript
and Python:


f := [ :x | x cos + x ].
df := [ :x | (f value: x + 1e-8) - (f value: x) * 1e8].

Here f is a way to implement a function and df its derivate.

Do some of you knows how it will be written in Javascript and Python
with their own ad-hoc anonymous function?

Thanks

Hilaire

--
Dr. Geo
http://drgeo.eu



Reply | Threaded
Open this post in threaded view
|

Re: Bloc of code in tiers programming language

HilaireFernandes
In reply to this post by eftomi
Thanks guys.

--
Dr. Geo
http://drgeo.eu



Reply | Threaded
Open this post in threaded view
|

Re: Bloc of code in tiers programming language

HilaireFernandes
In reply to this post by Richard O'Keefe
Hi,

It is an important restriction on Python. So Javasctip has several way
of doing lambda, correct?

Thanks

Hilaire

Le 15/05/2019 à 16:19, Richard O'Keefe a écrit :

> One point worth making is that Python lambdas are artificially restricted:
> the body of a Python lambda may only be a single expression, not a
> sequence
> of statements.  This restriction is for ideological reasons (the BDFL does
> not *want* you to do that) not for technical reasons.  Lisp and Algol 68
> were doing lambda expressions in the 60s.
>
> As well as function (x, y) { ... },
> modern Javascript has (x, y) => ....
> with subtly different semantics.
>
--
Dr. Geo
http://drgeo.eu



Reply | Threaded
Open this post in threaded view
|

Re: Bloc of code in tiers programming language

eftomi

Best wishes,
Tomaz

------ Original Message ------
From: "Hilaire" <[hidden email]>
Sent: 15.5.2019 18:54:58
Subject: Re: [Pharo-users] Bloc of code in tiers programming language

Hi,
 
It is an important restriction on Python. So Javasctip has several way
of doing lambda, correct?
 
Thanks
 
Hilaire
 
Le 15/05/2019 à 16:19, Richard O'Keefe a écrit :
One point worth making is that Python lambdas are artificially restricted:
the body of a Python lambda may only be a single expression, not a
sequence
of statements.  This restriction is for ideological reasons (the BDFL does
not *want* you to do that) not for technical reasons.  Lisp and Algol 68
were doing lambda expressions in the 60s.
 
As well as function (x, y) { ... },
modern Javascript has (x, y) => ....
with subtly different semantics.
 
--
Dr. Geo
http://drgeo.eu
 
 
 
Reply | Threaded
Open this post in threaded view
|

Re: Bloc of code in tiers programming language

khinsen
In reply to this post by Atharva Khare
Am 15.05.19 um 15:26 schrieb Atharva Khare:

> I think in python, you use Lambda Expressions. Here is how I would do it
> in python3:
> import math
> f = lambda x: math.cos(x) + x
> d_f = lambda x: (f(x + 1e-8) - f(x)) * 1e8

Lambda expressions are indeed Python's anonymous functions, but no
Python programmer would create a lambda expression only to assign it to
a variable. Doing this in an article to "sell" Smalltalk might well have
the opposite effect.

Konrad.

Reply | Threaded
Open this post in threaded view
|

Re: Bloc of code in tiers programming language

HilaireFernandes
Le 15/05/2019 à 20:37, Konrad Hinsen a écrit :
> Lambda expressions are indeed Python's anonymous functions, but no
> Python programmer would create a lambda expression only to assign it
> to a variable. Doing this in an article to "sell" Smalltalk might well
> have the opposite effect.

Nor a Smalltalk programmer.

Bellow the context for FYI.

| sketch f df xn ptA ptB|
sketch := DrGeoSketch new axesOn.
xn := 2.
f := [ :x | x cos + x ].
df := [ :x | (f value: x + 1e-8) - (f value: x) * 1e8]. "Derivate number"
sketch plot: f from: -20 to: 20.
ptA := (sketch point: xn@0) large; name: 'Drag me'.
5 timesRepeat: [
    ptB := sketch point: [ :pt | pt point x @ (f value: pt point x)]
parent: ptA.
    ptB hide.
    (sketch segment: ptA to: ptB) dotted forwardArrow .
    ptA := sketch point: [:pt |
        | x |
        x := pt point x.
        x - ( (f value: x) / (df value: x) )  @ 0 ] parent: ptB.
    ptA hide.
    (sketch segment: ptB to: ptA) dotted forwardArrow].


--
Dr. Geo
http://drgeo.eu



Reply | Threaded
Open this post in threaded view
|

Re: Bloc of code in tiers programming language

Tim Mackinnon
In reply to this post by khinsen
On a similar line - I’ve often noticed that an interesting block pattern in Smalltalk which is overlooked in other languages is how we handle errors through them.

We often don’t throw exceptions but instead pass a useful block (and often 2) for what to do instead.

at:ifAbsent: comes to mind or at:ifPreset:ifAbsent: leading you to do interesting things yourself like parseSource:onErrorRecoverWith: (or whatever).

I think this makes an interesting point about our block flexibility.

Tim

Sent from my iPhone



Sent from my iPhone

>> On 15 May 2019, at 19:37, Konrad Hinsen <[hidden email]> wrote:
>>
>> Am 15.05.19 um 15:26 schrieb Atharva Khare:
>>
>> I think in python, you use Lambda Expressions. Here is how I would do it in python3:
>> import math
>> f = lambda x: math.cos(x) + x
>> d_f = lambda x: (f(x + 1e-8) - f(x)) * 1e8
>
> Lambda expressions are indeed Python's anonymous functions, but no Python programmer would create a lambda expression only to assign it to a variable. Doing this in an article to "sell" Smalltalk might well have the opposite effect.
>
> Konrad.
>


Reply | Threaded
Open this post in threaded view
|

Re: Bloc of code in tiers programming language

Esteban A. Maringolo
On Wed, May 15, 2019 at 5:21 PM Tim Mackinnon <[hidden email]> wrote:
>
> On a similar line - I’ve often noticed that an interesting block pattern in Smalltalk which is overlooked in other languages is how we handle errors through them.
>
> We often don’t throw exceptions but instead pass a useful block (and often 2) for what to do instead.
>
> at:ifAbsent: comes to mind or at:ifPreset:ifAbsent: leading you to do interesting things yourself like parseSource:onErrorRecoverWith: (or whatever).
>
> I think this makes an interesting point about our block flexibility.

Although it is not exactly the same, the async style of programming
JavaScript uses similar approaches by passing "callback functions" as
arguments to certain functions. e.g. fopen('/path/to/file',
successFunction, errorHandler).

As said, it is not exactly the same thing, and 100% callback programs
can be a hell to work with, but the idiom of passing "lambdas" around
is similar.

Regards,

Esteban A. Maringolo

Reply | Threaded
Open this post in threaded view
|

Re: Bloc of code in tiers programming language

BrunoBB
In reply to this post by Tim Mackinnon
Hi,

Blocks are very useful when you need to evaluate code from an outside
source.
For example a BPM Process that have gateways with different conditions.

To the Smalltalk system conditions come as Strings and convert them to
Smalltalk objects is very easy:
self evaluate: '[:process | process amount > 1500]'. "condition inside a
gateway of a process"

And we have the block to evaluate and get the Boolean (to see what
transition follow after the gateway).
In other languages you have to process the condition String and do some kind
of interpretation but is much more limited than a block.

Of course there is a security issue here but very easy to overcome with an
AST.
If the conditions is: 'Smalltalk become: nil'.
In my case all message can be sent to the block argument only and some other
simple rules.

regards,
bruno



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: Bloc of code in tiers programming language

Richard O'Keefe
In reply to this post by HilaireFernandes
Suppose you want a pair of mutually recursive functions.
They have to be able to name each other.
In languages like Python and Ruby, you can have
methods AND you can have named functions.  In fact
Python had named functions before it had objects.
But in Smalltalk, you have methods, which cannot be
invoked without mentioning the receiver, and you
have blocks, which do NOT have a receiver of their
own.  So you have to write
   f := [:... | ... g value: ... ].
   g := [:... | ... f value: ... ].
This also applies when f needs to call itself.
At this point, Smalltalk beginners are so used to seeing
blocks used directly as arguments that they use methods
instead.


On Thu, 16 May 2019 at 07:49, Hilaire <[hidden email]> wrote:
Le 15/05/2019 à 20:37, Konrad Hinsen a écrit :
> Lambda expressions are indeed Python's anonymous functions, but no
> Python programmer would create a lambda expression only to assign it
> to a variable. Doing this in an article to "sell" Smalltalk might well
> have the opposite effect.

Nor a Smalltalk programmer.

Bellow the context for FYI.

| sketch f df xn ptA ptB|
sketch := DrGeoSketch new axesOn.
xn := 2.
f := [ :x | x cos + x ].
df := [ :x | (f value: x + 1e-8) - (f value: x) * 1e8]. "Derivate number"
sketch plot: f from: -20 to: 20.
ptA := (sketch point: xn@0) large; name: 'Drag me'.
5 timesRepeat: [
    ptB := sketch point: [ :pt | pt point x @ (f value: pt point x)]
parent: ptA.
    ptB hide.
    (sketch segment: ptA to: ptB) dotted forwardArrow .
    ptA := sketch point: [:pt |
        | x |
        x := pt point x.
        x - ( (f value: x) / (df value: x) )  @ 0 ] parent: ptB.
    ptA hide.
    (sketch segment: ptB to: ptA) dotted forwardArrow].


--
Dr. Geo
http://drgeo.eu



Reply | Threaded
Open this post in threaded view
|

Re: Bloc of code in tiers programming language

tbrunz
Richard,

Question from someone still fairly new to Smalltalk:

To implement the example you gave regarding mutually recursive functions in
Lua, one must write something like this:

   local f, g

   function g ()
     <do something>
     f()
     <do something>
   end

   function f ()
     <do something>
     g()
     <do something>
   end

where the initial declaration of f & g as locals defines an f such that g
will see it (as a local, albeit containing nil) when g is defined.  The
following definition of f doesn't require this, of course.  And g doesn't
care what f contains -- until execution time.

Both Lua and Smalltalk implement full lexical closures, so it begs the
question: Do the blocks defined in your example have this same issue?  What
is 'g' in the block assigned to f?

Is it required that 'g' be lexically defined prior to referencing it in a
block closure, or does Smalltalk have a mechanism to resolve this at
execution time?

Thanks,
-Ted



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: Bloc of code in tiers programming language

Richard O'Keefe
I do wish people wouldn't say "beg the question"
when they mean "invites" or "raises" the question. Sigh.

Yes, Smalltalk is just like Lua here.
  |f g|  "declare f and g as local variables"
  f := [... g value ...]. "f uses g's current value"
  g := [... f value ...]. "g uses f's current value"

You cannot mix declarations and statements in Smalltalk.
Local variables have to be declared before use.


On Thu, 16 May 2019 at 11:31, Brainstorms <[hidden email]> wrote:
Richard,

Question from someone still fairly new to Smalltalk:

To implement the example you gave regarding mutually recursive functions in
Lua, one must write something like this:

   local f, g

   function g ()
     <do something>
     f()
     <do something>
   end

   function f ()
     <do something>
     g()
     <do something>
   end

where the initial declaration of f & g as locals defines an f such that g
will see it (as a local, albeit containing nil) when g is defined.  The
following definition of f doesn't require this, of course.  And g doesn't
care what f contains -- until execution time.

Both Lua and Smalltalk implement full lexical closures, so it begs the
question: Do the blocks defined in your example have this same issue?  What
is 'g' in the block assigned to f?

Is it required that 'g' be lexically defined prior to referencing it in a
block closure, or does Smalltalk have a mechanism to resolve this at
execution time?

Thanks,
-Ted



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: Bloc of code in tiers programming language

tbrunz
I beg your pardon.. and thank you for being the first to draw my attention to
the fact that the phrase (a common enough American colloquialism) is
actually a logical fallacy.  Until now, it's been strictly idiomatic to me.

And thank you for your prompt reply.  

Am I safe to assume that blocks in Smalltalk, as with Lua, capture their
locally-scoped variables (referred to in Lua as "non-local variables") for
correct evaluation in other contexts, such as when blocks are passed as
arguments and return values?  I.e., cases where the local variables of a
method have gone out of scope and no longer exist, yet are referenced within
the block at some future time when evaluated.  I expect so; I just haven't
seen it described in this detail.

-t



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: Bloc of code in tiers programming language

Ben Coman


On Fri, 17 May 2019 at 01:21, Brainstorms <[hidden email]> wrote:
I beg your pardon.. and thank you for being the first to draw my attention to
the fact that the phrase (a common enough American colloquialism) is
actually a logical fallacy.  Until now, it's been strictly idiomatic to me.

And thank you for your prompt reply. 

Am I safe to assume that blocks in Smalltalk, as with Lua, capture their
locally-scoped variables (referred to in Lua as "non-local variables") for
correct evaluation in other contexts, such as when blocks are passed as
arguments and return values?  I.e., cases where the local variables of a
method have gone out of scope and no longer exist, yet are referenced within
the block at some future time when evaluated.  I expect so; I just haven't
seen it described in this detail.

You mean like this...

In System Browser...
    Object subclass: #A
instanceVariableNames: ''
classVariableNames: ''
package: 'AA'

    A >> block 
|a|
^ [ a := (a ifNil: [ 0 ]) + 1 ]

In Playground...
    b := A new block inspect.
    { b value. b value. b value . b }  "==> an Array( 1  2  3    [ a := (a ifNil: [ 0 ]) + 1 ]  )"

cheers -ben
 
Reply | Threaded
Open this post in threaded view
|

Re: Bloc of code in tiers programming language

tbrunz
You got it.  Thanks, Ben!

After success with Lua, now I'm thinking about how to get Pharo inserted
into the culture here...


Ben Coman wrote

> You mean like this...
>
> In System Browser...
>     Object subclass: #A
> instanceVariableNames: ''
> classVariableNames: ''
> package: 'AA'
>
>     A >> block
> |a|
> ^ [ a := (a ifNil: [ 0 ]) + 1 ]
>
> In Playground...
>     b := A new block inspect.
>     { b value. b value. b value . b }  "==> an Array( 1  2  3    [ a := (a
> ifNil: [ 0 ]) + 1 ]  )"
>
> cheers -ben





--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: Bloc of code in tiers programming language

Richard O'Keefe
In reply to this post by tbrunz
Blocks in current Smalltalk system are just like lambdas in Scheme.
Pharo even has continuations (see the Continuation class).


On Fri, 17 May 2019 at 05:21, Brainstorms <[hidden email]> wrote:
I beg your pardon.. and thank you for being the first to draw my attention to
the fact that the phrase (a common enough American colloquialism) is
actually a logical fallacy.  Until now, it's been strictly idiomatic to me.

And thank you for your prompt reply. 

Am I safe to assume that blocks in Smalltalk, as with Lua, capture their
locally-scoped variables (referred to in Lua as "non-local variables") for
correct evaluation in other contexts, such as when blocks are passed as
arguments and return values?  I.e., cases where the local variables of a
method have gone out of scope and no longer exist, yet are referenced within
the block at some future time when evaluated.  I expect so; I just haven't
seen it described in this detail.

-t



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: Bloc of code in tiers programming language

tbrunz
Richard O'Keefe wrote
> Blocks in current Smalltalk system are just like lambdas in Scheme.
> Pharo even has continuations (see the Continuation class).

I was going to ask about coroutines and continuations, but I thought maybe
bringing these subjects up in another thread would be more appropriate.
(This forum is also new to me.  More so than Smalltalk & Pharo.)

Is the "Continuation class" part of the MOOC series?  Or a class held at the
ESUG conference?  I don't recall running across anything referring to the
subject.

-t




--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

12