Lazy.st
author Claus Gittinger <cg@exept.de>
Mon, 18 Mar 2019 10:59:35 +0100
changeset 4878 5d852ae34bc9
parent 4573 7033574e421a
child 5033 256f0b1cdd22
permissions -rw-r--r--
#FEATURE by cg class: SftpURI comment/format in: #readStreamDo: changed: #connectThenDo: return the value of the block
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     1
"
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     2
 This is a Manchester Goodie protected by copyright.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     3
 These conditions are imposed on the whole Goodie, and on any significant
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     4
 part of it which is separately transmitted or stored:
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     5
	* You must ensure that every copy includes this notice, and that
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     6
	  source and author(s) of the material are acknowledged.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     7
	* These conditions must be imposed on anyone who receives a copy.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     8
	* The material shall not be used for commercial gain without the prior
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
     9
	  written consent of the author(s).
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    10
 Further information on the copyright conditions may be obtained by
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    11
 sending electronic mail:
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    12
	To: goodies-lib@cs.man.ac.uk
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    13
	Subject: copyright
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    14
 or by writing to The Smalltalk Goodies Library Manager, Dept of
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    15
 Computer Science, The University, Manchester M13 9PL, UK
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    16
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    17
 (C) Copyright 1992 University of Manchester
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1515
diff changeset
    18
 For more information about the Manchester Goodies Library (from which
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    19
 this file was distributed) send e-mail:
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    20
	To: goodies-lib@cs.man.ac.uk
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1515
diff changeset
    21
	Subject: help
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    22
"
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1515
diff changeset
    23
"{ Package: 'stx:libbasic2' }"
457
c862c91716b6 packages
Claus Gittinger <cg@exept.de>
parents: 351
diff changeset
    24
3849
62f298788374 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3063
diff changeset
    25
"{ NameSpace: Smalltalk }"
62f298788374 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3063
diff changeset
    26
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    27
ProtoObject subclass:#Lazy
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    28
	instanceVariableNames:'result startSemaphore endSemaphore'
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    29
	classVariableNames:''
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    30
	poolDictionaries:''
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    31
	category:'Kernel-Processes'
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    32
!
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    33
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    34
!Lazy class methodsFor:'documentation'!
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    35
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    36
copyright
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    37
"
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    38
 This is a Manchester Goodie protected by copyright.
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    39
 These conditions are imposed on the whole Goodie, and on any significant
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    40
 part of it which is separately transmitted or stored:
67
6d8a403eff0a *** empty log message ***
claus
parents: 62
diff changeset
    41
	* You must ensure that every copy includes this notice, and that
6d8a403eff0a *** empty log message ***
claus
parents: 62
diff changeset
    42
	  source and author(s) of the material are acknowledged.
6d8a403eff0a *** empty log message ***
claus
parents: 62
diff changeset
    43
	* These conditions must be imposed on anyone who receives a copy.
6d8a403eff0a *** empty log message ***
claus
parents: 62
diff changeset
    44
	* The material shall not be used for commercial gain without the prior
6d8a403eff0a *** empty log message ***
claus
parents: 62
diff changeset
    45
	  written consent of the author(s).
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    46
 Further information on the copyright conditions may be obtained by
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    47
 sending electronic mail:
67
6d8a403eff0a *** empty log message ***
claus
parents: 62
diff changeset
    48
	To: goodies-lib@cs.man.ac.uk
6d8a403eff0a *** empty log message ***
claus
parents: 62
diff changeset
    49
	Subject: copyright
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    50
 or by writing to The Smalltalk Goodies Library Manager, Dept of
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    51
 Computer Science, The University, Manchester M13 9PL, UK
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    52
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    53
 (C) Copyright 1992 University of Manchester
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1515
diff changeset
    54
 For more information about the Manchester Goodies Library (from which
62
a759b5c72c98 Initial revision
claus
parents:
diff changeset
    55
 this file was distributed) send e-mail:
67
6d8a403eff0a *** empty log message ***
claus
parents: 62
diff changeset
    56
	To: goodies-lib@cs.man.ac.uk
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1515
diff changeset
    57
	Subject: help
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    58
"
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    59
!
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    60
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    61
documentation
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    62
"
3063
b4af2ee4ed93 class: Lazy
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
    63
    I represent an execution which may or may not be required.
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1515
diff changeset
    64
    I will not start execution until at least one message has been received.
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    65
    The messages sent to me are delayed until execution has completed.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    66
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1515
diff changeset
    67
    [author:]
3063
b4af2ee4ed93 class: Lazy
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
    68
        tph@cs.man.ac.uk
1515
006cd21bbc25 Inherit from ProtoObject.
Stefan Vogel <sv@exept.de>
parents: 1326
diff changeset
    69
006cd21bbc25 Inherit from ProtoObject.
Stefan Vogel <sv@exept.de>
parents: 1326
diff changeset
    70
    [see also:]
3063
b4af2ee4ed93 class: Lazy
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
    71
        Block LazyValue Future
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    72
"
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    73
!
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    74
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    75
examples
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    76
"
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    77
  Evaluates the factorial, starting only when the
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    78
  result is actually required (when printString is sent).
3977
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
    79
                                                [exBegin]
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    80
    | fac |
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    81
    fac := [100 factorial] lazyValue.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    82
    Transcript showCR: 'Doing nothing. '.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    83
    (Delay forSeconds: 2) wait.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    84
    Transcript showCR: fac printString.
3977
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
    85
                                                [exEnd]
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    86
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    87
3977
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
    88
  Starts evaluating both factorials only when required (by the value),
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    89
  and waits until both blocks have finished before continuing.
3977
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
    90
                                                [exBegin]
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    91
    | fac1 fac2 |
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    92
    fac1 := [Transcript showCR: 'Starting fac1.. '. 100 factorial] lazyValue.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    93
    fac2 := [Transcript showCR: 'Starting fac2.. '. 120 factorial] lazyValue.
3977
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
    94
    fac2 value.
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
    95
    fac1 value.
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    96
    Transcript showCR: 'both completed.'.
3977
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
    97
                                                [exEnd]
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    98
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
    99
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   100
  Demonstrates how to pass arguments to a lazy evaluation block.
3977
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   101
                                                [exBegin]
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   102
    | temp |
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   103
    temp := [:x :y :z | x * y * z] lazyValueWithArguments: #(2 3 4).
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   104
    Transcript  showCR: temp printString.
3977
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   105
                                                [exEnd]
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   106
"
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   107
! !
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   108
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   109
!Lazy methodsFor:'evaluating'!
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   110
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   111
block: aBlock
3977
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   112
    "Execute aBlock in parallel, but ensure that any messages sent
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   113
     to me before execution of the block has terminated are
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   114
     suspended until it has terminated. Do not start the evaluation
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   115
     until at least one message has been sent to the receiver."
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   116
3977
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   117
    startSemaphore := Semaphore new.
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   118
    endSemaphore := Semaphore new.
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   119
    [
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   120
        startSemaphore wait.
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   121
        result := aBlock value.
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   122
        endSemaphore signal
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   123
    ] fork.
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   124
!
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   125
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   126
block: aBlock value: aValue
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   127
	"Execute aBlock in parallel, but ensure that any messages sent
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   128
	 to me before execution of the block has terminated are
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   129
	 suspended until it has terminated. Do not start the evaluation
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   130
	 until at least one message has been sent to the receiver."
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   131
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   132
	startSemaphore := Semaphore new.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   133
	endSemaphore := Semaphore new.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   134
	[startSemaphore wait.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   135
	 result := aBlock value: aValue.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   136
	 endSemaphore signal] fork
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   137
!
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   138
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   139
block: aBlock value: value1 value: value2
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   140
	"Execute aBlock in parallel, but ensure that any messages sent
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   141
	 to me before execution of the block has terminated are
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   142
	 suspended until it has terminated. Do not start the evaluation
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   143
	 until at least one message has been sent to the receiver."
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   144
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   145
	startSemaphore := Semaphore new.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   146
	endSemaphore := Semaphore new.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   147
	[startSemaphore wait.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   148
	 result := aBlock value: value1 value: value2.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   149
	 endSemaphore signal] fork
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   150
!
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   151
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   152
block: aBlock value: value1 value: value2 value: value3
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   153
	"Execute aBlock in parallel, but ensure that any messages sent
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   154
	 to me before execution of the block has terminated are
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   155
	 suspended until it has terminated. Do not start the evaluation
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   156
	 until at least one message has been sent to the receiver."
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   157
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   158
	startSemaphore := Semaphore new.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   159
	endSemaphore := Semaphore new.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   160
	[startSemaphore wait.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   161
	 result := aBlock value: value1 value: value2 value: value3.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   162
	 endSemaphore signal] fork
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   163
!
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   164
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   165
block: aBlock valueWithArguments: anArray
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   166
	"Execute aBlock in parallel, but ensure that any messages sent
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   167
	 to me before execution of the block has terminated are
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   168
	 suspended until it has terminated. Do not start the evaluation
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   169
	 until at least one message has been sent to the receiver."
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   170
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   171
	startSemaphore := Semaphore new.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   172
	endSemaphore := Semaphore new.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   173
	[startSemaphore wait.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   174
	 result := aBlock valueWithArguments: anArray.
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   175
	 endSemaphore signal] fork
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   176
! !
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   177
4007
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   178
!Lazy methodsFor:'printing'!
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   179
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   180
displayOn:aGCOrStream
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   181
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   182
    "/ what a kludge - Dolphin and Squeak mean: printOn: a stream;
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   183
    "/ ST/X (and some old ST80's) mean: draw-yourself on a GC.
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   184
    (aGCOrStream isStream) ifFalse:[
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   185
        ^ self _evaluate_ displayOn:aGCOrStream
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   186
    ].
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   187
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   188
    startSemaphore isNil ifTrue:[
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   189
        result displayOn:aGCOrStream.
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   190
        aGCOrStream nextPutAll:' (Lazy evaluated)'.
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   191
        ^ self.
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   192
    ].    
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   193
    aGCOrStream nextPutAll:'Lazy (unevaluated)'
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   194
!
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   195
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   196
displayString
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   197
    |s|
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   198
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   199
    "/ attention: TextStream is not present in ultra-mini standalone apps
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   200
    s := TextStream isNil
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   201
            ifTrue:['' writeStream]
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   202
            ifFalse:[TextStream on:(String new:32)].
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   203
    self displayOn:s.
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   204
    ^ s contents
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   205
! !
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   206
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   207
!Lazy methodsFor:'synchronising'!
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   208
3977
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   209
_evaluate_
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   210
    "answer the computed value"
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   211
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   212
    |startSema endSema|
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   213
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   214
    startSema := startSemaphore.
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   215
    startSema notNil ifTrue:[
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   216
        startSema signal.          "Start the evaluation."
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   217
        endSema := endSemaphore.
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   218
        endSema notNil ifTrue:[
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   219
            endSema waitUncounted.     "Wait until evaluation completed."
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   220
        ].
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   221
        startSemaphore := endSemaphore := nil.
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   222
    ].
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   223
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   224
    ^ result
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   225
!
e913680689bf #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3849
diff changeset
   226
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   227
doesNotUnderstand: aMessage
3849
62f298788374 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3063
diff changeset
   228
    "Any message to a Lazy will end up here."
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   229
4007
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   230
    startSemaphore notNil ifTrue:[
4573
7033574e421a #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4226
diff changeset
   231
        Processor activeProcess isDebuggerProcess ifTrue:[
4007
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   232
            "enable debugging / inspecting without evaluating"
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   233
            ^ aMessage sendTo:self usingClass:Object.
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   234
        ].
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   235
        self _evaluate_.
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   236
    ].
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   237
da947b2313c9 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 3977
diff changeset
   238
    ^ aMessage sendTo:result.
4573
7033574e421a #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4226
diff changeset
   239
7033574e421a #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4226
diff changeset
   240
    "Modified: / 01-02-2018 / 10:17:27 / stefan"
4226
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   241
!
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   242
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   243
perform:aSelector withArguments:argArray
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   244
    "send the message aSelector with all args taken from argArray
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   245
     to the receiver."
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   246
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   247
    startSemaphore notNil ifTrue:[
4573
7033574e421a #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4226
diff changeset
   248
        Processor activeProcess isDebuggerProcess ifTrue:[
4226
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   249
            "enable debugging / inspecting"
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   250
            ^ super perform:aSelector withArguments:argArray.
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   251
        ].
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   252
        self _evaluate_.
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   253
    ].
e914ce1c6859 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4007
diff changeset
   254
    ^ result perform:aSelector withArguments:argArray.
4573
7033574e421a #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4226
diff changeset
   255
7033574e421a #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4226
diff changeset
   256
    "Modified: / 01-02-2018 / 10:17:08 / stefan"
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   257
! !
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   258
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   259
!Lazy methodsFor:'testing'!
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   260
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   261
isLazyValue
2341
36e455705ee6 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1515
diff changeset
   262
    ^ endSemaphore wouldBlock
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   263
! !
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   264
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   265
!Lazy class methodsFor:'documentation'!
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   266
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   267
version
3849
62f298788374 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3063
diff changeset
   268
    ^ '$Header$'
1326
b8e168f120ce comments
Claus Gittinger <cg@exept.de>
parents: 885
diff changeset
   269
! !
3063
b4af2ee4ed93 class: Lazy
Claus Gittinger <cg@exept.de>
parents: 2341
diff changeset
   270